ZF2 Routing as in ZF1

后端 未结 2 1408
别跟我提以往
别跟我提以往 2021-02-03 15:25

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

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

I read the informa

相关标签:
2条回答
  • 2021-02-03 16:05

    You can set up a wildcard child_route, at least on a per-controller basis, 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 instance, the url() view helper:

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

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

    0 讨论(0)
  • 2021-02-03 16:26

    Here is the standard router I use for everything I port over from ZF1 -> ZF2 keep in mind that you still need to add the controllers as invocables which i have at the top of the list. Also please keep in mind that I always keep my actual application at the bottom of the list so that all other modules will have their routing defined before it hits the application. However this makes the routing work just like ZF1... I see a lot of people asking about this so I figured I'd post! With the setup below, I can just add my new controller (Support below...) And then open a browser and go to... /support, and it works just 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 a full module path as a comment which i missed in my initial post.

    0 讨论(0)
提交回复
热议问题