Let's continue the consideration of the practical use of the Zend Framework. The second part deals with routing, which will allow you to create url-addresses of any form you need. We will also consider an example of organizing convenient access to system constants.
.
- Part one
- Authentication - user login
- ACL - distribution of access rights
- Part two
- Routing - setting the url 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 the url of the format - controller / action / param1 / value1 / param2 / value2. It is thanks to the routing tools that we can organize the url of such a format that is required in the project. For example, you can organize simple access to static pages through the url of the form - site.com/static-page or implement access to products through links of the form - site.com/catalog/category/good-name. In particular, it is useful for seo needs, but also helps users in getting links to immediately understand what is on the page of the site.
Consider the example of creating routes for the link formats specified above. First you need to create routes, for this we will write a method in the bootstrap.php file:
public function _initRoute(){
First we get the standard router and add custom routes to it. In this case, we are using a Zend_Controller_Router_Route rewriting router. The first parameter to the constructor of the router is a string describing the url format. To specify dynamic parts, the identifiers of the form ": variable" are used. It is convenient to consider such dynamic parts as variables, the values of which are substituted from a specific part of the url. The second parameter is the array of standard values of the router. In this array, you need to specify the values of the name of the controller and the action that will process the request for this route, as well as the default values of variables that will be used if the corresponding url value is omitted. It is also convenient to specify the requirements for variables. For this, the third parameter is to pass a pattern that describes the format of a valid variable value. I recommend specifying this option whenever possible. This is useful for security reasons. if the pattern does not match the value of the variable, an error will be triggered and the action code will not receive a potentially dangerous value.
After creating the router, we need to add it to the standard router using the addRoute method. Each route must have a unique identifier string that will be used by us in the future.
Further, in the action code, in order to get the value of the variable, we need to execute the following code:
$page = $this->getRequest()->getParam('page');
We may also need to create links using this router. To do this, use the standard url view helper.
')
<a href="<?php $this->url(array('page'=>'about'), 'static'); ?>"> </a>
We covered only one of the routers used in the Zend Framework. Read more in the documentation
here .
Registry
Now we will look at how convenient and quick access to constants from the application.ini file section can be organized in ZF. To begin with, let's set several constants:
[constants]
paths.photo = "/ photo /"
paths.uploads = "/ uploads /"
paths.video = "/ video /"
secret_code = "key_for_check"
The code above must be placed in the application.ini file. Next, we use the Zend_Registry component, which implements the Registry pattern, to make our constants available throughout the project:
public function _initConfig(){ Zend_Registry::set('constants', new Zend_Config_Ini( APPLICATION_PATH . '/configs/application.ini', 'constants') ); }
The method presented above should be added to bootstrap.php. Further, in order to address a constant, use the following code:
$path_photo = Zend_Registry::get('constants')->paths->photo;