📜 ⬆️ ⬇️

Subtleties of routing in the Zend Framework

The goal is to make the user profile available through example.com/user/username and through the username user subdomain username.example.com . The task is simple and with the help of the Zend Framework is solved by a couple of lines of code. Difficulties started when I decided to register actions, confirm registration, reset the password and enter / exit itself into the same controller that displays the user profile. Now there was a task to teach the controller not to treat example.com/user/register as a user.

Following the documentation, when defining a router, we can specify by the third parameter the requirements for variables defined in the route as a regular expression.
To solve the problem, it is necessary to specify the regular expression "/^(?!register$|confirm$|recover$|login$|logout$).+$/" , which translates as "a string with any subsequent text from the beginning of the string that does not contain strings register, confirm, recover, login, logout and then terminate the string ”(I always considered regulars as shamanism). But ZF has already bothered about us, putting down the framing characters, and the characters at the beginning and end of the line.

So let us have a UserController controller. This controller contains an IndexAction action that serves to work with a user profile, as well as actions that we need to run, ignoring routing. Summary Code:


$routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  1. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  2. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  3. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  4. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  5. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  6. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  7. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  8. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  9. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  10. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  11. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  12. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  13. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  14. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  15. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  16. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  17. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  18. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
  19. $routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .
$routeUri = new Zend_Controller_Router_Route( 'user/:username' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!register$|confirm$|recover$|login$|logout$).+' ) ); $router->addRoute( 'useruri' , $routeUri); $hostnameRoute = new Zend_Controller_Router_Route_Hostname( ':username.example.com' , array( 'controller' => 'user' , 'action' => 'index' ), array( 'username' => '(?!www$|register$|confirm$|recover$|login$|logout$).+' ) ); $plainPathRoute = new Zend_Controller_Router_Route_Static( '' ); $router->addRoute( 'userdomain' , $hostnameRoute->chain($plainPathRoute); * This source code was highlighted with Source Code Highlighter .

')
Now, when clicking on the link example.com/user/register, your site will not try to search and download the register user profile

By the way, in the archive of the offline manual I found two files from HtmlHelp, thanks to which I now have a ZF manus in a convenient * .chm format. The only subtlety is that the htmlhelp compiler is not friendly with the terrible representation of Russian strings in the form of HTML entities. :) But this problem is solved by an elementary script:

  1. $ text = file_get_contents ( 'toc.hhc' );
  2. $ dc = Array ();
  3. $ ch = Array ();
  4. for ($ i = 1040; $ i <= 1103; $ i ++)
  5. {
  6. $ dc [] = '& #' . $ i. ';' ;
  7. $ ch [] = chr ($ i-848);
  8. }
  9. $ text = str_replace ($ dc, $ ch, $ text);
  10. $ h = fopen ( '_toc.hhc' , 'w +' );
  11. fputs ($ h, $ text);
  12. fclose ($ h);
* This source code was highlighted with Source Code Highlighter .


Successful routing and compilation :)

Source: https://habr.com/ru/post/48955/


All Articles