Using the Zend Framework URL rewriting

Today for a good website’s referencement in Google, it’s necessary to have an URL rewriting.

For this implementation, without the Zend Framework classes, you need to have a htaccess file with all URL rewriting rules.

For instance :

RewriteEngine on
RewriteBase /path/website/base/application/
RewriteRule ^([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)\.html$ index.php?arg1=$1&arg2=$2 [L]

If your application is based on ZF, we have a htaccess base file which will redirect all php files to the boostrap (what is the bootstrap ?), and ZF classes will manage all redirection rules.

How to implement the URL rewriting with ZF classes ?

In order to have a better and clearer vision of all our rewriting rules, we are going to use a ini config file.

Here is an example with several rewriting rules :
in a first time in our ini file, we have to define the default route for ZF application.
ZF classes needs a model based rewriting.

[url_rewriting]
;default route
routes.default.route = ":controller/:argument1/:argument2.html"
;argument which will be find as $_GET variable
routes.default.reqs.argument1 = "([A-Za-z0-9-_]+)"
routes.default.reqs.argument2 = "([A-Za-z0-9-_]+)"
;default controller in ZF application
routes.default.reqs.controller = "([A-Za-z0-9]+)"
;default action in ZF application
routes.default.reqs.action = "([A-Za-z0-9]+)"

In this example, we can find the controller argument in addition of our variables. It’s not compulsory, but advised. If you don’t want this value in your URL, you’ll have to precise more rewrite rules.

Now we have our default route and we can add specific rules and routes. It’s possible for the developer to shape as same as a htaccess file.

One instance of rewriting rules :

;We have to specify the kind of rewriting route, here we are going to use the Regex type like in a htaccess file
routes.route1.type = "Zend_Controller_Router_Route_Regex"
;Adding the route structure, with all Regexp pattern
routes.route1.route = "([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)/([0-9-_]+)-([0-9-_]+)\.html"
;for the default controller, we also use a regexp, in order to get directly the URL value.
;you can specify controller's value if the controller name is not in the URL route structure
routes.route1.defaults.controller = "([a-zA-Z0-9-_]+)"
;here we specify the action because it's not present in my URL structure
;Like the controller, you can add the action in the URL
routes.route1.defaults.action = "myactionincontroller"
;Now we specify the mapping of our variable as we can see in a htaccess file : $1, $2 etc... 
routes.route1.map.1 = "controller"
routes.route1.map.2 = "argument1"
routes.route1.map.3 = "argument2"

Our ini config file is done, you just have to add all route needed for your ZF application.

Now we have to load the config file in the bootstrap :

// Getting the Zend Controller Instance
$router = Zend_Controller_Front::getInstance()->getRouter();
// Creating a Zend_Config_Ini instance.
$config = new Zend_Config_Ini(dirname(__FILE__)."/path/to/config/file/rewriting.ini", 'url_rewriting');
// Adding my Zend Config to my Zend Controller instance
$router->addConfig($config, 'routes');

As you can see, it’s very simple to implement the config file to the ZF application.
You can have many possibilities with the ZF Router, you’ll can find all information about classes here. I choose to use the ini file because it’s ligther than a XML file for PHP language.

Of course, you have to enable Apache’s rewrite module and respect the ZF’s htaccess file.


About this entry