The intricacies of routing in Zend Framework
the Goal is to make the user profile available through the example.com/user/username and using a custom subdomain username.example.com. The task is simple and using the Zend Framework solved with a couple lines of code. The difficulties began when I decided the validity of the registration, confirmation of registration, restoration of the password and the actual login/logout to put in the same controller which displays a user profile. Now came the task to teach the controller to perceive example.com/user/register as a user.
Following the documentation, when you define a router we can set the third parameter requirements defined in the route variables in the regular expression.
To solve this task, you must specify a regular expression "/^(?!register$|confirm$|recover$|login$|logout$).+$/", which translates as "the row with any subsequent row from the beginning of the text string register, confirm, recover, login, logout with the subsequent end of line" (I always thought of the regular season shamanism). But ZF have bothered about us, by framing the characters and symbols of the beginning and end of the string.
So, suppose we have a controller UserController. This controller contains an action IndexAction, which serves to operate the user's profile, as well as actions to run which we need ignore routing. Final code:
Now when you click on the link example.com/user/register your website will not try to search and load user profile register
Incidentally, in the archive of the offline manual found two files from the HtmlHelp, thanks to what I have now man on a ZF in a convenient format *.chm. The only subtlety is that the htmlhelp compiler is not on friendly terms with the terrible performance of the Russian lines in the form of HTML entities. :). But this problem is solved by elementary scription:
A good routing and compile :)
Article based on information from habrahabr.ru
Following the documentation, when you define a router we can set the third parameter requirements defined in the route variables in the regular expression.
To solve this task, you must specify a regular expression "/^(?!register$|confirm$|recover$|login$|logout$).+$/", which translates as "the row with any subsequent row from the beginning of the text string register, confirm, recover, login, logout with the subsequent end of line" (I always thought of the regular season shamanism). But ZF have bothered about us, by framing the characters and symbols of the beginning and end of the string.
So, suppose we have a controller UserController. This controller contains an action IndexAction, which serves to operate the user's profile, as well as actions to run which we need ignore routing. Final code:
the - $routeUri = new Zend_Controller_Router_Route(
'user/:username'
array(
'controller' => 'user'
'action' => 'index'
the
- ),
array('username' => '(?!register$|confirm$|recover$|login$|logout$).+')
the
- );
the - $router->addRoute('useruri parameter', $routeUri);
the
- $hostnameRoute = new Zend_Controller_Router_Route_Hostname(
':username.example.com'
array(
'controller' => 'user'
'action' => 'index'
the
- ),
array('username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+')
the
- );
the - $plainPathRoute = new Zend_Controller_Router_Route_Static(");
the - $router->addRoute('userdomain', $hostnameRoute- > chain($plainPathRoute);
* This source code was highlighted with Source Code Highlighter.
Now when you click on the link example.com/user/register your website will not try to search and load user profile register
Incidentally, in the archive of the offline manual found two files from the HtmlHelp, thanks to what I have now man on a ZF in a convenient format *.chm. The only subtlety is that the htmlhelp compiler is not on friendly terms with the terrible performance of the Russian lines in the form of HTML entities. :). But this problem is solved by elementary scription:
the - $text=file_get_contents('toc.hhc');
the
- $dc=Array();
the - $ch=Array();
for($i=1040; $i<=1103; $i++)
{
the
- $dc[]=" . $i . ';';
the - $ch[]=chr($i-848);
the - }
the
- $text=str_replace($dc, $ch, $text);
the
- $h=fopen('_toc.hhc' 'w+');
the - fputs($h, $text);
the - fclose($h);
* This source code was highlighted with Source Code Highlighter.
A good routing and compile :)
Комментарии
Отправить комментарий