Workshop Zend Framework. Part two: Route and Registry


Will continue the consideration of the practical use of the Zend Framework. In the second part we will focus on routing, that will allow you to create URLs to any required form for you. We also consider the example of the organization of convenient access to system constants.
the
    the
  • Part one
    the

      Authentication — users log on to the system

      ACL — distribution of access rights


  • Part two
    the

      Routing — customize URLs for various system components

      Registry — quick access to system constants

Routing


Routing is a very important topic. As you know, by default, Zend Framework uses url format controller/action/param1/value1/param2/value2. It is through the means of routing, we can arrange the url in this format, which is required in the project. For example, you can organize a simple access to static pages url — site.com/static-page or implement the access to goods through the links of the view — site.com/catalog/category/good-name. This is especially useful for the needs of seo, but also helps the users when retrieving references, immediately understand what is on the page.
Consider the example of creating routes for references of the formats listed above. First you need to create the routes, for this we will write a method in the file bootstrap.php:

the
public function _initRoute(){
// Get route by default
$router = Zend_Controller_Front::getInstance ()- > getRouter();
// create custom itineraries
// route for static pages
$route_static = new Zend_Controller_Router_Route(
'/:page',
array(
'controller' => 'static',
'action' => 'index'
'page' => 'default-page-name'
), 
array(
'page' = > '[\w\-]+'
)
);
$router- > addRoute('static', $route_static);
// route for goods
$route_goods = new Zend_Controller_Router_Route(
'/catalog/:category/:good/*',
array(
'controller' => 'goods',
'action' => 'show'
), 
array(
'category' = > '[\w\-]+',
'good' = > '[\w\-]+'
)
);
$router- > addRoute('goods', $route_goods);
}

First, we get a standard router and add custom routes. In this case, we use the rewrite router Zend_Controller_Router_Route. The first parameter to the constructor of the router is passed a string describing the url format. To specify the dynamic parts used by the IDs to be "variable". Such dynamic parts it is convenient to consider as variables whose values are substituted from the specified site url. The second argument is passed an array of standard values of the router. In this array you need to specify the name of controller and action which handles the request via this route and the values of the variables default to be used if the appropriate value of the url is omitted. Also convenient sometimes to specify variable requirements. For this third parameter, you must pass a pattern that describes the format of valid values for a variable. I recommend to specify this parameter whenever possible. This is useful for security purposes, as in the case of coincidence of the template with the variable value will be initiated and error code action will not receive potentially dangerous values.
After you create a router, we need to add it to a standard router via addRoute method. Each route must have a unique string identifier that will be used in the future.
Later in the code in order to obtain the value of a variable, we need to execute the following code:

the
$page = $this->getRequest()->getParam('page');

Also, we may want to create links using this router. For this we use the standard view helper url.
the
<a href="<?php $this->url(array('page'=>'about'), 'static'); ?>">draft</a>

We have considered only one of the routers used in the Zend Framework. More info can be found in the documentation here.

the

Registry


We will now consider, as in ZF we can arrange a convenient and quick access to the constants section of the application file.ini. To start, set a few constants:

[constants]
paths.photo = "/photo/"
paths.uploads = "/uploads/"
paths.video = "/video/"
secret_code = "key_for_check"

The above code should be placed in the application file.ini. Next, we use the Zend_Registry component, which implements the Registry pattern, in order to make the constants available throughout your project:

the
 public function _initConfig(){
Zend_Registry::set('constants', 
new Zend_Config_Ini(
APPLICATION_PATH . '/configs/application.ini',
'constants')
);
}

The method presented above need to be added to bootstrap.php. Further, to refer to the constant using the following code:

the
$path_photo = Zend_Registry::get('constants')->paths->photo;
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

ODBC Firebird, Postgresql, executing queries in Powershell

Installation LivestreetCMS on MODX Revolution package 10 clicks

The Ministry of communications wants to ban phones without GLONASS