Route all requests through PageController, with the exception of existing controllers (Zend Framework)

For the new CMS, I developed the Pages module, which allows me to manage the structure of the tree structure of the site. Each page is accessible from the URL http://www.example.com/pageslug/ , where pagelug identifies the page being called.

What I want to achieve now is a route that allows me to forward all incoming requests to one PagesController server, if this is not a request to an existing controller (for example, an image).

It is easy enough to catch all requests to the page controller, but how to exclude existing controllers? This is my modular bootstrap. How can I achieve this in the most preferred way.

<?php class Default_Bootstrap extends Zend_Application_Module_Bootstrap { protected function _initRoute() { $this->bootstrap('frontController'); /* @var $frontcontroller Zend_Controller_Front */ $frontcontroller = $this->getResource('frontController'); $router = $frontcontroller->getRouter(); $router->addRoute( 'all', new Zend_Controller_Router_Route('*', array('controller' => 'pages', 'action' => 'view') ) ); } } 
+4
source share
2 answers

Zend routes work in order - if you add a second route after the first, it will take precedence if it matches. In my own Zend project, I have a bunch of routes, the first of which is very similar to yours, to catch the entire route. However, anything below this, which corresponds to the url, cancels it - just try adding a few more specific routes (if all your / user / requests go to your user_controller, add the route / user / *)

+3
source

By default, your controller is the default, and adding a route for each existing controller can be very confusing, and you have to change it every time you add a controller.

An alternative would be to configure ErrorController. Since in the absence of a controller, the structure will throw a Zend_Controller_Dispatcher_Exception , which will be passed to the error handler as EXCEPTION_NO_CONTROLLER , you can simply check this type and forward it to your page controller.

If you feel masochistic, you can also write your own class of routes, which returns false if the controller exists, and processes all routes if not. This is probably the best option in terms of roles and responsibilities, but also the most difficult to implement.

+3
source

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


All Articles