Zend Framework 2 - 在<home> route </home>之后的路由参数

时间:2013-10-16 14:57:00

标签: php routing routes zend-framework2

我正在尝试解决我现在面临的路由问题,没有运气(谷歌搜索了几个小时,看了几十个例子并解决了问题,但没有一个对我有用 - 最接近的是这个Routing Zend Framework 2 Language in url但是即使这样对我不起作用。)

我已经创建了一个SSO应用程序(我的意思是身份验证部分),现在我将它移植到ZF2应用程序(我几乎正在使用它,因为我工作了路由器,但现在需要完成最终的路由)只有这些类型URL是可能的:

  

HTTP [秒]://domain.com/login/asfgsdgdsfgzsdgdsf/
  HTTP [S]://domain.com/login/tdhjsdgbndfnfgdnhd/
  HTTP [S]://domain.com/logout/asfgsdgdsfgzsdgdsf/
  HTTP [S]://domain.com/info/dthnzdfbdfhgnfsd/

那些loginlogoutinfo部分不是控制器或操作,而是我在IndexController和{{1}内调用SSO服务的方法名称}}。 URL的其余部分是客户端应用程序哈希。

现在这里是我的路由器配置,它只匹配主路由,当提供其他部分(方法名称[和哈希])时,我从ZF2应用程序得到404(所以至少我在应用程序上下文中)。

indexAction()

(仅提供 module.config return array( 'router' => array( 'routes' => array( 'home' => array( 'type' => 'Zend\Mvc\Router\Http\Segment', 'options' => array( 'route' => '/', 'defaults' => array( 'controller' => 'SSO\Controller\Index', 'action' => 'index', 'act' => 'login', // by doing this I am able to workaround the router and work with SSO 'client' => '<my_secret_hash>', // by doing this I am able to workaround the router and work with SSO ), ), 'child_routes' => array( 'action' => array( 'type' => 'Zend\Mvc\Router\Http\Segment', 'options' => array( 'route' => '[/:act]', 'defaults' => array( 'act' => 'login', ), 'constraints' => array( 'act' => '(login|logout|info)?', // only login, logout or info are allowed ), ), 'child_routes' => array( 'client' => array( 'type' => 'Zend\Mvc\Router\Http\Segment', 'options' => array( 'route' => '[/:client]', 'constraints' => array( 'client' => '[a-zA-Z0-9]+', ), 'defaults' => array( 'client' => '<my_secret_hash>', ), 'may_terminate' => true, ), ), ), ), ), ), ), ); 部分...)

现在,该路由器仅匹配router http[s]://domain.com[/]http[s]://domain.com/(login|logout|info)[/]匹配发生了404错误

虽然我尝试将路径部件定义为可选(仅用于测试和开发目的),但它们应该在生产环境中需要。无论如何,我试图定义它们也不是可选的,但也没有用。

问题:如何配置路由器以匹配我在开头定义的路由(URL)?

1 个答案:

答案 0 :(得分:3)

一些事情:

  • 我的猜测是你的斜线太多了 - /的子路径不需要在开头用斜线定义,因为它的父级已经得到它
  • 我没有看到2个嵌套路由的原因(当然我不知道你的配置的其余部分,所以这是猜测)
  • home路线可能应为Literal且能够终止(再次猜测)
  • 有一个不匹配的括号(可能是'misspasted'或其他东西)

我想这对你有用:

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Literal',
            'may_terminate' => true,
            'options'   => array(
                'route'     => '/',
                'defaults'  => array(
                    'controller'    => 'SSO\Controller\Index',
                    'action'        => 'index',
                ),
            ),
            'child_routes' => array(
                'whateva' => array(
                    'type'      => 'Segment',
                    'may_terminate' => true,
                    'options'   => array(
                        'route'     => '[:act[/:client]]', // as Sam suggested
                        'defaults'  => array(
                            'act' => 'login',
                            'client' => 'fsadfasf32454g43g43543',
                        ),
                        'constraints' => array(
                            'act' => '(login|logout|info)',
                            'client'    => '[a-zA-Z0-9]+',
                        ),
                    ),
                ),
            ),
        ),
    ),
),