Tuesday, December 4, 2012

Using Sessions

Using sessions in Zend Framework 2 is very easy - probably easier than you would expect. With the lack of official documentation, this short article serves as a quick start.

It will probably not be a surprise to you that sessions in Zend Framework 2 are handled within the Zend\Session namespace. To interact with sessions, we will actually use the Zend\Session\Container class.

Because ZF2 makes use of namespaces, we must add the following at the top of our controller, given that we do not want to use fully qualified classes:

use Zend\Session\Container;

Then, in a controller action, we can save a session like this:

$user_session = new Container('user');
$user_session->isLoggedIn = true;

To retrieve the above value, simply do like this:

$user_session = new Container('user');
$is_logged_in = $user_session->isLoggedIn;

The reason why we can use properties like above is that the Container class extends the ArrayObject class and uses the ArrayObject::ARRAY_AS_PROPS constant. Actually, the $_SESSION superglobal is replaced by a Zend\Session\Storage\SessionStorage object.

This is essentially how you can easily work with sessions in Zend Framework 2. Obviously there are many more aspects to it for more advanced use cases, but that is outside the scope of this quick start guide.

For more detailed information regarding what happens behind the scenes when working with Zend Framework 2 sessions, I encourage you to visit the previous link or wait for the official documentation to be ready.

Saturday, September 29, 2012

Zend Framework 2 Installation on Mac

Since the release of Zend Framework 2, the installation process of the framework is somewhat more complicated. It is still simple, though, but with the introduction of Composer, more complexity has been added. Even though Composer is entirely optional, it is used in the official documentation, so if people do not want to use it, they will either have to find a guide somewhere or figure things out by themselves.

To set up Zend Framework 2 on a Mac, then you have to do a few things. Firstly, you should acquire the Skeleton Application. You do not have to, but it makes things significantly easier. Then you have to decide if you want to use Composer or not. If not, then you can drop the framework library within the vendor folder. Take a look at the Skeleton Application's init_autoloader.php for inspiration on where to place the library. For instance, you could place it in vendor/ZF2/library, or you could set the environment variable ZF2_PATH to where the framework is located. You can even set zf2_path as a PHP configuration option. Alternatively, you can use Composer as discussed in the documentation on the Zend Framework website. As you can see, there are many ways to install Zend Framework 2.

Then, unless you want to type in a long URL every time you want to access your ZF installation (and with the /public folder appended), you should make a new virtual host. This is easily done by editing you httpd-vhosts.conf.

This is all a fairly easy process once you get the hang of it. Do not worry if you face problems the first time - Google is your friend! For instance, the path of your configuration files will often differ depending on which operating system you are using.

I am not going to go into great detail of how to do each step, because it has been written by others before. This is just to give you an idea of what needs to be done to get an installation running. If you want to see a thorough article on this, then I suggest reading the excellent tutorial on how to install Zend Framework 2 on OS X.