Zend Route Url regex url

I am trying to create a route that will "span" such URLs:

www.test.com/parent1/parent2/parent3/item

www.test.com/parent1/parent2/parent3/parent4/item1

The number of these parents is in an unspecified state, and this should only provide a better, more intuitive look at the URLs of sites. The main parameter is the "element".

I believe the only way to solve this is to use Route_Regex, and so I tried to accomplish this route task like this:

routes.test.type = "Zend_Controller_Router_Route_Regex" routes.test.route = "test/(?:.*)?([^\/]+)" routes.test.defaults.module = default routes.test.defaults.controller = test routes.test.defaults.action = index routes.test.map.1 = "path" routes.test.map.2 = "item" routes.test.reverse = "test/%s%s" 

I have not tested this a lot because I’m not sure that I am doing the right thing ... I have no idea how this regular expression should look and how I should consider this "path". "

Can you advise what to do to satisfy the demand of such a route? So, I need this path (parent1, parent2, etc.) For appearance only, and the main parameter is "item" ...

+4
source share
3 answers

I know this is an old question, but I had a similar problem, and I thought I should post my solution. Perhaps this can help other people consider this issue.

I wrote my routes in the plugin. Obviously you need to add the plugin to bootstrap for this to work;)

 class Plugin_RoutesPage extends Zend_Controller_Plugin_Abstract { public function routeStartup(Zend_Controller_Request_Abstract $request) { $front_controller = Zend_Controller_Front::getInstance(); $router = $front_controller->getRouter(); // Page SEO friendly hierarchical urls $routePageSeoTree = new Zend_Controller_Router_Route_Regex( '([-a-zA-Z0-9/]+)/([-a-zA-Z0-9]+)', array( // default Route Values 'controller' => 'page', 'action' => 'open', ), array( // regex matched set names 1 => 'parents', 2 => 'item' ) ); $router->addRoute('page-seo-tree',$routePageSeoTree); // only one level $routeSinglePage = new Zend_Controller_Router_Route_Regex( '([-a-zA-Z0-9]+)', array( // default Route Values 'controller' => 'page', 'action' => 'open', ), array( // regex matched set names 1 => 'item' ) ); $router->addRoute('page-single',$routeSinglePage); } } 

Here's how you can use it in your controller action.

 class PageController extends Zend_Controller_Action { public function openAction() { // the part of the uri that you are interested in $item = $this->_request->getParam('item'); } } 

Here is a quick example of how to include it in your bootstrap

 class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { protected function _initPlugins() { $front_controller = Zend_Controller_Front::getInstance(); $front_controller->registerPlugin(new Plugin_RoutesPage(), 1); } } 

I had to use two routes, because the current page that we are trying to view / open may not have parents. I am sure that probably the best way to write a regex, but this is what worked for me. If anyone knows how to improve regex, please let me know.

+7
source

If the parent elements are completely unimportant, a simple solution would be to remove them before they even reach Zend, i.e. rewrite the URLs so that it only sees test/:item

For example, using:

 RewriteEngine On RewriteRule ^(test/).*/([^/]+) $1$2 [L] 

(Obviously, the "test /" part can be changed or made dynamic).

This is included in the Apache configuration file for the site - or in the .htaccess file on shared hosting.

If you are not using Apache, check the URL rewriting functionality for any web server you use - the syntax may be slightly different, but it will be similar.

+1
source

I don't know what the zend route is (something related to php?), But if you can use the usual programming functions, then a much simpler solution than the regular expression is to use the built-in list / split functions, for example:

 end(explode($Url,'/')) Url.split('/').last() ListLast(Url,'/') 

etc.


If you need a regex, you can do this simply with this:

 [^/]+$ 

This will give you everything after the last scroll to the end of the line.
(You only need to avoid / with a backslash if you use it in a context that requires escaping.)


(By the way, in your example, do you have test/ at the beginning - is this intentional / mandatory?)

-2
source

All Articles