【问题标题】:ZF2 Routing works on OS X but fails on linuxZF2 路由在 OS X 上工作,但在 linux 上失败
【发布时间】:2016-07-01 22:16:30
【问题描述】:

我有一个基于 Zend Framework 2.3 的小型站点,它在带有 OS X Server 的 OS X 上运行。我需要做一些开发,所以我把它复制到了 Debian 7 系统上。除了数据库定义之外,两台机器上的代码都是相同的。基于 Linux 的系统适用于大多数功能,但其中一个会导致 404 错误,我不明白为什么会这样。 module.config.php 是 大批 ( 'invokables' => 数组 ( 'LibraryRest\Controller\AuthorRest' => 'LibraryRest\Controller\AuthorRestController', 'LibraryRest\Controller\BookTitleRest' => 'LibraryRest\Controller\BookTitleRestController', 'LibraryRest\Controller\RecentRest' => 'LibraryRest\Controller\RecentRestController' ), '工厂' => 数组 ( 'LibraryRest\Controller\SearchRest' => 'LibraryRest\Factory\SearchRestControllerFactory' ) ), '路由器' => 数组 ( '路线' => 数组 (

                    'author-rest' => array (
                            'type' => 'Segment',
                            'options' => array (
                                    // Change this to something specific to your module
                                    'route' => '/author-rest[/:id]',
                                    'constraints' => array (
                                            'id' => '[0-9]+' 
                                    ),
                                    'defaults' => array (
                                            'controller' => 'LibraryRest\Controller\AuthorRest' 
                                    ) 
                            ) 
                    )
                    ,
                    'booktitle-rest' => array (
                            'type' => 'Segment',
                            'options' => array (
                                    'route' => '/booktitle-rest[/:id]',
                                    'constraints' => array (
                                            'id' => '[0-9]+' 
                                    ),
                                    'defaults' => array (
                                            'controller' => 'LibraryRest\Controller\BookTitleRest' 
                                    ) 
                            ) 
                    ),
                    'recent-rest' => array (
                            'type' => 'Segment',
                            'options' => array (
                                    'route' => '/recent-rest',
                                    'defaults' => array (
                                            'controller' => 'LibraryRest\Controller\RecentRest' 
                                    ) 
                            ) 
                    ),
                    'search-rest' => array (
                            'type' => 'Segment',
                            'options' => array (
                                    'route' => '/search-rest[/:action[/:first][/:last]]',
                                    'constraints' => array (
                                            'first' => '[a-zA-Z0-9_-\s\x40%\.]*',
                                            'last' => '[a-zA-Z0-9_-\s\x40%]*',
                                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*' 
                                    ),
                                    'defaults' => array (
                                            'controller' => 'LibraryRest\Controller\SearchRest' 
                                    ) 
                            ) 
                    ) 
            ) 
    ),
    'view_manager' => array (
            'strategies' => array (
                    'ViewJsonStrategy' 
            ) 
    ) 

); 在 linux 机器上失败的路由是 search-rest,所以http://mysite/search-rest/search/John/Smith 会导致 404。此模块中的所有其他路由在两个系统上都能正常工作。

什么可能导致 Linux 系统上的路由失败?

【问题讨论】:

  • 可能是区分大小写的问题。检查 SearchRest 控制器的文件名。

标签: linux macos zend-framework2 debian


【解决方案1】:

我个人会尝试更改第一个和最后一个参数的正则表达式:

'search-rest' => array (
        'type' => 'Segment',
        'options' => array (
            'route' => '/search-rest[/:action[/:first][/:last]]',
            'constraints' => array (
                'first' => '[a-zA-Z0-9_\-\s\x40%\.]*',
                'last' => '[a-zA-Z0-9_\-\s\x40%]*',
                'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
            ),
            'defaults' => array (
                'controller' => 'LibraryRest\Controller\SearchRest'
                )
            )
        ),

我在“-”之前添加了一个“\”,或者您可以尝试将“-”放在字符类 ([]) 的开头。

因为现在它在内部调用 preg_match() 函数时会在 ZF Segment 类中产生错误。由于存在错误,因此导致404,因为找不到路由。

【讨论】:

  • 太棒了。正则表达式是问题所在,让您的更改解决了它。现在让我感到困惑的是为什么它可以在 OS X 上运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-11
  • 1970-01-01
  • 2013-11-28
  • 2020-01-30
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多