Routing URLs begin with a special character

In ZendFramework, I want to route URLs starting with ~ with a special controller and action, so that other URLs don't start with ~ correctly.

For example, see two URL routes below:

 mysite.com/~user 

and

 mysite.com/admin 

How can i do this?

+4
source share
2 answers

Try using this in your bootstrap.

 // Get the instance of the router $router = Zend_Controller_Front::getInstance()->getRouter(); // Set up a new regex router to match routes starting with ~ $route = new Zend_Controller_Router_Route_Regex( '(^\~)', //This route should use a 'special' controller array( 'controller' => 'special', 'action' => 'index' ) ); // Add the new route to the router $router->addRoute('archive', $route); 

You will need a controller called Special to respond to requests directed to this router.

+1
source

Note zend specifically, but the best way to do this is to add a hash lookup table before the url router does its business.

So let's say mod_rewrite converts this: mysite.com/~user

to this: mysite.com/index.php?path=~user

then you would do something like this:

 $path = $_GET['path']; $url_mod = array( '~user'=>'my_other_controller', 'admin'=>'my_other_controller', ); if(isset($url_mod[$path)) { $path = $url_mod[$path]; } 
-1
source

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


All Articles