URL Routing in Zend Framework
Clean URLs for search engines are like cookies for babies, when using clean URLs in our applications we say to others we know what we are really do! in addition to many advantages like awesome look for your links .
Im not going to explane building clean URL with PHP & Apache server in this blog post so if you are searching for such thing you will not find your answer here. but we are here to know how to simply configure our Zend Application to work with custom URL routing..
in Zend Framework default application routing is :
it’s clean enough but what if we need to use urls like this :
http://www.example.com/dashboard
http://www.example.com/explore-product-10.html
http://www.example.com/explore/product/10
http://www.example.com/login or logout
or let us say that you want to do some cool stuff and gives all your site members short links like what Facebook and Twitter offers to their members ..
http://www.example.com/john.smith
Ok , let us work …
navigate to your Bootstap class, and create new method ( optional or user any _init method )
public function _initRoutes()
{
// Routes definitions goes here
}
Now open your eyes, i’ll write examples that achieves all required routing in the previous examples ..
Ex1 : routing with variables
public function _initRoute()
{
// get instance of front controller
$frontController = Zend_Controller_Front::getInstance();
// define new route class
// this route with define the route for
// http://www.example.com/explore/product/10
// the id of the product found under variable name ‘id’
// to retrive it $this->getRequest->getParam(‘id)
// in the index action of product controller
$route = new Zend_Controller_Router_Route(
‘explore/product/:id/’,array(
‘controller’ => ‘product’,
‘module’ => ‘default’ ,
‘action’ => ‘index’,
‘id’ => “));
// add this route to the front controller
$frontController->getRouter()->addRoute(‘user’,$route);
}
Now you can request your product page with this linkhttp://www.example.com/explore/product/12 ..
Ex2 : routing without variables ( for login , logout pages )
public function _initRoute()
{
// get instance of front controller
$frontController = Zend_Controller_Front::getInstance();
// define new route class
// this route with define the route for
// http://www.example.com/login
$route = new Zend_Controller_Router_Route(
‘login’,array(
‘controller’ => ‘user’,
‘module’ => ‘default’ ,
‘action’ => ‘login’
));
// add this route to the front controller
$frontController->getRouter()->addRoute(‘login’,$route);
// define new route class
// this route with define the route for
// http://www.example.com/login
$route = new Zend_Controller_Router_Route(
‘logout’,array(
‘controller’ => ‘user’,
‘module’ => ‘default’ ,
‘action’ => ‘logout’
));
// add this route to the front controller
$frontController->getRouter()->addRoute(‘logout’,$route);
}
this will send the link www.example.com/login or logout to your user/login or logout controller/method with clean url, have fun 😉 ..
Now what if we want to give user ability to pick their custom profile links ..
Ex3 : custom profile links
public function _initRoute()
{
// get instance of front controller
$frontController = Zend_Controller_Front::getInstance();
// define new route class
// this route with define the route for
// http://www.example.com/dynamic.profile
// the name of the profile found under variable ‘profile’
// to retrive it $this->getRequest->getParam(‘profile)
// in the index action of profile controller
$route = new Zend_Controller_Router_Route(
’:profile/’,array(
‘controller’ => ‘profiles’,
‘module’ => ‘default’ ,
‘action’ => ‘index’,
‘profile’ => ”));
// add this route to the front controller
$frontController->getRouter()->addRoute(‘profiles’,$route);
}
this will route all links like : www.example.com/xxxxx to profiles controller and index action with variable profile which contain the value of the profile variable , you can query in the database to get user informations using profile variable ..
Final thing i didn’t mention before , when we add route with method addRoute($Route_name , $route ) ,$route_name is identical name for the routing, if you uses ‘default’ this will re-write the default routing rule in the framework.
one more routing method Zend offers is Regex Routing
Ex4 : Routing with RegEx
public function _initRoute()
{
// get instance of front controller
$frontController = Zend_Controller_Front::getInstance();
// define new route class
// this route with define the route for
// http://www.example.com/explore-product-10.html
// the id of the product found under variable name ‘id’
// to retrive it $this->getRequest->getParam(‘id)
// in the index action of product controller
$route = new Zend_Controller_Router_Route_Regex(
’([^-]*)-([^-]*)-([^-]*).html’,array(
‘action’ => ‘explore’,
‘controller’ => ‘product’,
‘module’ => ‘default’),
array(
1 => ‘action’,
2 => ‘controller’,
3 => ‘id’ ));
$frontController->getRouter()->addRoute(‘product’,$route);
}
to read more about Regular expression read this.