mut*_*ron 3 zend-framework2 zend-framework-routing
我们刚刚开始使用zf2,所以其他人制作了一个Thumbnail服务器模块,然后我添加了一个Lookup服务器模块.我称之为服务器,因为它们都是RESTful apis.最初它们似乎在一起工作,但有人对我的模块进行了一些更改,现在除非从application.config.php模块列表中删除Lookup,否则缩略图服务器将无法工作.Lookup服务器无论如何都可以.查看代码,我看不到对Lookup所做的更改会如何影响Thumbnail.我得到的错误看起来像这样:
<h1>A 404 error occurred</h1>
<h2>Page not found.</h2>
<p>The requested controller was unable to dispatch the request.</p>
<dl>
<dt>Controller:</dt>
<dd>Lookup\Controller\Lookup</dd>
</dl>
Run Code Online (Sandbox Code Playgroud)
这是application.config.php看起来像:
<?php
return array(
'modules' => array(
'Application',
'SanRestful',
'Album',
'AlbumRest',
'Thumbnail',
'Lookup',
'AP_XmlStrategy',
),
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
'config/autoload/{,*.}' . (getenv('APPLICATION_ENV') ?: 'production') . '.php',
),
'module_paths' => array(
'./module',
'./vendor',
),
),
);
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,最初的专辑模块和其他一些实验模块.My Lookup模块使用Allessandro Pietrobelli提供的优秀AP_XmlStrategy模块.
下面是缩略图module.config.php.它有一个可能没有被使用的约束,因为没有名为"id"的参数,但这不应该搞乱,是吗?
<?php
return array(
'controllers' => array(
'invokables' => array(
'Thumbnail\Controller\Thumbnail' => 'Thumbnail\Controller\ThumbnailController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'thumbnail' => array(
'type' => 'segment',
'options' => array(
'route' => '/thumbnail[/:action][/:output]',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Thumbnail\Controller\Thumbnail',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'thumbnail' => __DIR__ . '/../view',
),
),
);
Run Code Online (Sandbox Code Playgroud)
查找module.config.php,标识符混淆:
<?php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'pgsql:dbname=<dbname>;host=<host>;port=<port>',
'username' => '<username>',
'password' => '<password>',
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
),
'aliases' => array(
'db' => 'Zend\Db\Adapter\Adapter',
),
),
'controllers' => array(
'invokables' => array(
'Lookup\Controller\Lookup' => 'Lookup\Controller\LookupController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'lookup' => array(
'type' => 'segment',
'options' => array(
'route' => '[/:action][/:version][/:resource][/:code][/:resource_xref]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'version' => '[a-zA-Z][a-zA-Z0-9_-]*',
'resource' => '[a-zA-Z][a-zA-Z0-9_-|\.]*',
'code' => '[a-zA-Z][a-zA-Z0-9_-]*',
'resource_xref' => '[a-zA-Z][a-zA-Z0-9_-|\.]*',
),
'defaults' => array(
'controller' => 'Lookup\Controller\Lookup',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'lookup' => __DIR__ . '/../view',
),
'strategies' => array(
'ViewJsonStrategy',
'ViewXmlStrategy',
),
),
);
Run Code Online (Sandbox Code Playgroud)
这里有任何明显的错误,我错过了吗?
请求可以由多个路由匹配.一个例子:url/foo可以通过文字路线/foo和路线匹配[/:action].要区分这两条路线,配置它们的顺序很重要.
会发生什么,路由以LIFO顺序匹配,因此最后一条路线必须是最明确的.在"/ foo"示例中,此配置将与文字匹配:
'router' => array(
'routes' => array(
'test1' => array(
'type' => 'segment',
'options' => array(
'route' => '[/:action]',
'defaults' => array(
//
),
),
),
'test2' => array(
'type' => 'literal',
'options' => array(
'route' => '/foo',
'defaults' => array(
//
),
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
但是,在下面的配置中,文字永远不会匹配,因为[/:action]它可能匹配/foo.
'router' => array(
'routes' => array(
'test2' => array(
'type' => 'literal',
'options' => array(
'route' => '/foo',
'defaults' => array(
//
),
),
),
'test1' => array(
'type' => 'segment',
'options' => array(
'route' => '[/:action]',
'defaults' => array(
//
),
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
现在看看你的两个模块.第一个(缩略图)有一个路线/thumbnail[/:action][/:output].它以文字部分开头.然后你的第二个模块(Lookup)有一条路线[/:action][/:version][/:resource][/:code][/:resource_xref].
现在,如果您回到LIFO订单,任何以路径开头的路线/thumbnail都将在Lookup路线上匹配.
解
有两种选择.首先是交换模块的顺序.如果模块之间存在相互依赖的关系,这始终是一个重要的顺序.首先加载查找,然后加载Thumnnail,以便稍后在配置中放置缩略图路由.因此,它首先匹配.因此,您的应用再次运作.
然后有第二个解决方案(和imho,更好).在Lookup中你有一条"统一所有"的路线,这不是一个很好的做法.你可能会像现在一样陷入困境,不知道出了什么问题.因此,请尽可能在路线中指定.使Lookup的第一部分也是文字(/lookup[/:action][/:version][/:resource][/:code][/:resource_xref]不是一个选项吗?).或者删除动作作为参数并制作这些文字:
'router' => array(
'routes' => array(
'view' => array(
'type' => 'segemnt',
'options' => array(
'route' => '/view[/:version][/:resource][/:code][/:resource_xref]',
'defaults' => array(
'action' => 'view',
//
),
),
),
'create' => array(
'type' => 'segment',
'options' => array(
'route' => '/create[/:version][/:resource][/:code][/:resource_xref]',
'defaults' => array(
'action' => 'create',
//
),
),
),
'update' => array(
'type' => 'segment',
'options' => array(
'route' => '/update[/:version][/:resource][/:code][/:resource_xref]',
'defaults' => array(
'action' => 'update',
//
),
),
),
// And so on
),
),
Run Code Online (Sandbox Code Playgroud)
这样,您的查找模块具有固定的起始点,并且仅当这些第一部分位于请求uri中时才匹配.然后你/thumbnail[/:action][/:output]完全脱离了Lookup路线.
| 归档时间: |
|
| 查看次数: |
6362 次 |
| 最近记录: |