Tuesday, August 27, 2013

For ZF1 users there is the convenient init() method that you can use on every controller but in ZF2 the init() does
not exist anymore for design purposes.

There are sometimes advantages though of having an init() type functionality inside the constructor. For example, checking the ACL.

A little background. One of the problems of working directly inside the constructor in ZF2 is because the dispatch event has not
occured yet, you cannot grab important objects like controller plugins so doing the bellow wont work.

// assume we are inside a controller
public function __construct()
{
$this->redirect()->toRoute('someroute'); // I will not work!
}

Solution:
There are tons of ways to do this but I find this a little easier:

Notice that I added a "default" child route so that I will not have to worry if a I added a new action and forget to create a route for it. Also notice the "[/]" which
means that forward slashes are optional now and that's basically it. You just have to add "[/]" in each route of a Segment type.

Thursday, August 22, 2013

For those who are not familiar of ODBC like I was, its basically an API that lets you connect to other databases. PDO has its ODBC driver which in turn ODBC has its own also. To connect to MS SQL Server, we are using FreeTDS which is a library that lets you accomplish this.

This is not the best example but you get the groove? When you mock an object, you set an "expectation" for the object method you are mocking. In the case above, we are checking that add() only accepts numerical inputs. Usually, a dead give away is we are using with() to set our expectation. In contrast to a stub, we are just returning straight the result.

Note that the callback for a mock does not matter. I just use it for verbosity.