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:


    the
  1. $routeUri = new Zend_Controller_Router_Route(
  2. 'user/:username'

    array(

    'controller' => 'user'

    'action' => 'index' the

  3. ),
  4. array('username' => '(?!register$|confirm$|recover$|login$|logout$).+') the

  5. );
  6. the
  7. $router->addRoute('useruri parameter', $routeUri);
  8. the

  9. $hostnameRoute = new Zend_Controller_Router_Route_Hostname(
  10. ':username.example.com'

    array(

    'controller' => 'user'

    'action' => 'index' the

  11. ),
  12. array('username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+') the

  13. );
  14. the
  15. $plainPathRoute = new Zend_Controller_Router_Route_Static(");
  16. the
  17. $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
  1. $text=file_get_contents('toc.hhc');
  2. the

  3. $dc=Array();
  4. the
  5. $ch=Array();
  6. for($i=1040; $i<=1103; $i++)

    { the

  7. $dc[]=" . $i . ';';
  8. the
  9. $ch[]=chr($i-848);
  10. the
  11. }
  12. the

  13. $text=str_replace($dc, $ch, $text);
  14. the

  15. $h=fopen('_toc.hhc' 'w+');
  16. the
  17. fputs($h, $text);
  18. the
  19. fclose($h);
* This source code was highlighted with Source Code Highlighter.


A good routing and compile :)
Article based on information from habrahabr.ru

Комментарии

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

Integration of PostgreSQL with MS SQL Server for those who want faster and deeper

Custom database queries in MODx Revolution

Parse URL in Zend Framework 2