Categories
PHP

Zend Framework : Session Handling, URL Routing, URL Helper and Redirecting

Some basics of Zend Framework, I am documenting in case I forget these in the future 😉

Session Handling

URL Routing
Add in Bootstrap:

URL Helper

URL Helper is available via the method – url() in a view object. The method takes an array of options as its first parameter. The second parameter is optional where you can pass a route object name (eg. ‘category’ from the above url routing example). This comes in handy when you want to construct routed URLs.

By default the helper takes the following options: “action”, “controller” and “module”. If anyone of them is not provided, default one is used. It can also take in GET params which will be appended to the URL in a SEO friendly manner. If you pass a route object, you can simply pass the dynamic parts, no need to pass the entire action, controller or anything.

Note that: In the second case, “id” itself is passed to the URL. But in the first case, the URL routing rule has been honored.

Inside a view script, you can directly use:

Since it is a member of the View object, you can also access it from an action like this:

Handling Redirection
Zend has a Redirector helper which provides advanced features when it comes to redirections. But for most cases, the built in _redirect() method of an action will do the job if used wisely with the URL helper.

That’s it! Zend Framework is verbose and usually has more than one way to achieve something. It’s up to you to decide what suits your needs!