简体   繁体   English

Zend Framework 2 - 路由中的斜杠

[英]Zend Framework 2 - trailing slashes in route

Havin in a Zend Module following structure Havin在Zend模块中遵循结构

Name of Module 模块名称

  • Test 测试
    • config 配置
    • src SRC
    • view 视图
      • layout 布局
      • test 测试
        • index 指数
          • index.phtml index.phtml
        • login 登录
          • index.phtml index.phtml
        • register 寄存器
          • index.phtml index.phtml
    • Module.php Module.php

Now when accessing website for example for login file it will be as following http://justsite.something.com/login 现在,当访问网站时,例如登录文件,它将如下http://justsite.something.com/login

Problem Need to change to http://justsite.something.com/login/ for that i added to login folder another folder index and there putted index.phtml file like this 问题需要更改为http://justsite.something.com/login/ ,因为我添加到登录文件夹的另一个文件夹索引和那里推出的index.phtml文件是这样的

  • login 登录
    • index 指数
      • index.phtml index.phtml

But that doesn't seem to work. 但这似乎不起作用。 Should i change in LoginController.php for this to work? 我应该更改LoginController.php以使其工作吗?

module.confing.php module.confing.php

'router' => array(
    'routes' => array(
        'test' => array(
            'type' => 'Literal',
            'options' => array(
                'route' => '/test',
                'defaults' => array(
                    '__NAMESPACE__' =>'Test\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes'  => array(
                'default' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' =>
                        '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' =>
                            '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action' =>
                            '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(),
                    ),
                ),
            ),
        ),
        'login' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/login/', // PUT HERE WHATEVER YOU WANT
                'defaults' => array(  // AND MAP THAT URL TO CONTROLLER/ACTION
                    '__NAMESPACE__' =>'Test\Controller',                    
                    'controller' => 'Login',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),

Fixed Just added 修正刚添加

'may_terminate' => true,
            'child_routes'  => array(
                'default' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' =>
                        '/[:controller[/:action]/]',

Like @tasmaniski wrote you don't need to make changes to LoginController or whatever, all comes down to your route config. 就像@tasmaniski所写,你不需要对LoginController或其他任何东西进行更改,所有这些都归结为你的路由配置。 You are not the only one who wants "trailing slashes" in his routes, here you can read how you can achieve this . 你并不是唯一一个想要在他的路线中使用“斜杠”的人在这里你可以阅读如何实现这一目标

Apparently you need to put it between [] 显然你需要把它放在[]之间

'route'    => '/[:controller[/[:action[/]]]]',

Hope that solves it for you. 希望能为你解决它。

NOTE Some people suggest using a rewrite rule for this, I thought this might be worth mentioning too. 注意 有些人建议使用重写规则,我认为这也值得一提

No matter what is your forlder structure. 无论你的forlder结构是什么。 You need to make a change in the config file module.config.php, section "routes". 您需要在配置文件module.config.php,“routes”部分进行更改。

You only need to put in "route" value with slash at the end and map it to controller/action. 您只需要在末尾放入带有斜杠的“route”值并将其映射到controller / action。

'routes' => array(
    'login' => array(
        'type'    => 'Zend\Mvc\Router\Http\Literal',
        'options' => array(
            'route'    => '/login/', // PUT HERE WHATEVER YOU WANT
            'defaults' => array(     // AND MAP THAT URL TO CONTROLLER/ACTION
                'controller' => 'Application\Controller\Login',
                'action'     => 'index',
            ),
        ),
    ),
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM