Tag Archives: zend framework

Zend Framework custom URLs

URL organization in Zend Framework

As you may know in ZF the urls are strictly organized. First you’ve the module, if you use modules. If there is only one module, and it is normally the default one, you omit the module name from the url. Than comes the controller name, the action name in that particular controller and than separate by / the url continues with key value pairs of the get parameters.

Of course to make this all work, you’d need to setup a simple .htaccess file just to rewrite all this to the index.php file which than parses the $_SERVER[‘REQUEST_URI’] to collect all the data he need about which controller, which action and what parameters to call.

That’s pretty simple. If you’ve the following URL /user/profile/name/john.smith you may suppose that Zend Framework calls the “user” controller, “profile” action with get parameter “name” which equals to “john.smith”. In a traditional PHP style old school URL this can look like this:

?controller=user&action=profile&name=john.smith

Why I need such long URL?

The problem is that Zend reads all this, parses it and than returns the correct output. But however not always this is what we want, right? I’d like to have custom url for the profile page. Why should I need both profile and name in my URL, I’d prefer a URL like this: /user/john.smith. Continue reading Zend Framework custom URLs

HTTP_REFERRER in Zend Framework

What is HTTP_REFERRER?

If you need to know how called a PHP script, it is normally setup in the $_SERVER variable of HTTP_REFERRER, or simply

$_SERVER['HTTP_REFERRER'];

but in the same time this does not work the same way in Zend Framework, just because everything is passing by the index.php or a custom bootstrap!

Unfortunately there’s no so many resources online, however there is a solution, which seems to be pretty natural. You can just use this chuck of code:

$this->getRequest()->getHeader('REFERER');

and that’s all!

Strict types in Zend Framework and in PHP

As you may know you can add strict types for a function parameters in PHP i.e.

public function func1(string $test)
{}

and this may be some simple function in some Zend Framework standard model! Now the problem comes with the instance of this function. If you call it like this:

func1('foo bar');

in a ZF controller, you simply will get an error.

It is strange …

isn’t it! But in PHP you can use strict types only for an array, which is built in type. In other words if some object implements the __toString() method than you can use string strict type.

Zend Framework and multiple db connections

The use case

Recently we (Petar Atanasov and me) were working on a Zend Framework project, which requires more than one database connections. The framework as a typical MVC architecture does not allow that functionality forcing the user to use one single connections. However there is the setDefaultAdapter in ZF which is enough to think about switching the default adapter between the different connections.

The detailed tutorial how to make it work you can find on my colleague’s blog here.