ZF2 Routing as in ZF1

How can I automatically configure routing for everything in the ZF1 structure?

module / controller / action / par1Name / par1Val / par2Name / par2Val /

I read the routing information, but as I see it, I would have to add all the actions manually, and I see a problem with optional parameters ...

+4
source share
2 answers

You can configure wildcard child_route, at least based on each controller, to get zf1-like routes:

'products' => array( 'type' => 'Zend\Mvc\Router\Http\Segment', 'options' => array( 'route' => '/products[/:action]', 'defaults' => array( 'controller' => 'Application\Controller\Products', 'action' => 'index' ) ), 'may_terminate' => true, 'child_routes' => array( 'wildcard' => array( 'type' => 'Wildcard' ) ) ) 

Then you can use, for example, the view helper url ():

 $this->url('products/wildcard',array('action'=>'edit','id'=>5,'foo'=>'bar'); 

which will create a url like / products / edit / id / 5 / foo / bar

+7
source

Here is the standard router that I use for everything I port from ZF1 -> ZF2, remember that you still need to add the controllers as invocables, which I have at the top of the list. Also keep in mind that I always keep my actual application at the bottom of the list so that all other modules have their own routing defined before it gets to the application. However, this does the routing work just like ZF1 ... I see that many people ask about it, so I decided I would send it! With the setting below, I can simply add my new controller (support below ...) and then open a browser and go to ... / support, and it works fine.

 return array( 'controllers' => array( 'invokables' => array( 'Application\Controller\Index' => 'Application\Controller\IndexController', 'Application\Controller\Support' => 'Application\Controller\SupportController', ), ), 'router' => array( 'routes' => array( '*' => array( 'type' => 'Segment', 'options' => array( 'route' => '/[:controller[/:action]]', /* OR add something like this to include the module path */ // 'route' => '/support/[:controller[/:action]]', 'constraints' => array( 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', ), 'defaults' => array( '__NAMESPACE__' => 'Application\Controller', 'controller' => 'Index', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' => array( 'wildcard' => array( 'type' => 'Wildcard' ) ) ), ), ), 'view_manager' => array( 'display_not_found_reason' => true, 'display_exceptions' => true, 'doctype' => 'HTML5', 'not_found_template' => 'error/404', 'exception_template' => 'error/index', 'template_map' => array( 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 'error/404' => __DIR__ . '/../view/error/404.phtml', 'error/index' => __DIR__ . '/../view/error/index.phtml', ), 'template_path_stack' => array( __DIR__ . '/../view', ), ), /* Service manager / translator etc stuff go HERE as they change less frequently */ ); 

I edited the route above to include the full path to the module as a comment, which I skipped in my original post.

+6
source

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


All Articles