ZF2 路由器无法进行分段工作
ZF2 router not working on staging
所以我在本地有一个 ZF2 项目 运行,它运行良好。当我在临时服务器上设置它时,我 运行 遇到了路由问题。它将“/”路由到正确的控制器,但在访问时,说“/blog”,它将查找实际文件夹和 return 以下错误:
The requested URL /blog was not found on this server.
通常,如果它不匹配任何已定义的路由,ZF2 会return 一个错误,指出 URL 无法与路由匹配。但这似乎正在打破整个应用程序。有其他人 运行 有类似的 problems/found 修复吗?
谢谢。
不知道这样能不能解决你的疑惑,以前我也遇到过和你差不多的问题。这是我在 module.config.php:
中使用的路由部分
对于'admin'模块,例如:
<?php
...
'router' => array(
'routes' => array(
'admin' => array(
'type' => 'Literal',
'options' => array(
'route' => '/admin',
'defaults' => array(
'__NAMESPACE__' => 'Admin\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:controller[/:action[/id/:id]]][/]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
或者,对于主模块:
<?php
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
// The following is a route to simplify getting started creating
// new controllers and actions without needing to create a new
// module. Simply drop new controllers in, and you can access them
// using the path /application/:controller/:action
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[:controller[/:action[/id/:id]]][/]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
您的重写规则未被使用。如果您使用的是 Apache,要么您没有将 .htaccess
文件上传到登台服务器,要么您的登台服务器未配置为读取 .htaccess
文件。目前,请求甚至没有到达您的 Zend Framework 应用程序,因为您的 Web 服务器未配置为将它们发送到那里。
尝试用以下代码替换整个 .htaccess 文件。
这肯定有效。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}:: ^(/.+)(.+)::$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
或者您可以访问此 link 以获取更新的详细信息。 Htaccess file for any project
所以我在本地有一个 ZF2 项目 运行,它运行良好。当我在临时服务器上设置它时,我 运行 遇到了路由问题。它将“/”路由到正确的控制器,但在访问时,说“/blog”,它将查找实际文件夹和 return 以下错误:
The requested URL /blog was not found on this server.
通常,如果它不匹配任何已定义的路由,ZF2 会return 一个错误,指出 URL 无法与路由匹配。但这似乎正在打破整个应用程序。有其他人 运行 有类似的 problems/found 修复吗?
谢谢。
不知道这样能不能解决你的疑惑,以前我也遇到过和你差不多的问题。这是我在 module.config.php:
中使用的路由部分对于'admin'模块,例如:
<?php
...
'router' => array(
'routes' => array(
'admin' => array(
'type' => 'Literal',
'options' => array(
'route' => '/admin',
'defaults' => array(
'__NAMESPACE__' => 'Admin\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:controller[/:action[/id/:id]]][/]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
或者,对于主模块:
<?php
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
// The following is a route to simplify getting started creating
// new controllers and actions without needing to create a new
// module. Simply drop new controllers in, and you can access them
// using the path /application/:controller/:action
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[:controller[/:action[/id/:id]]][/]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
您的重写规则未被使用。如果您使用的是 Apache,要么您没有将 .htaccess
文件上传到登台服务器,要么您的登台服务器未配置为读取 .htaccess
文件。目前,请求甚至没有到达您的 Zend Framework 应用程序,因为您的 Web 服务器未配置为将它们发送到那里。
尝试用以下代码替换整个 .htaccess 文件。
这肯定有效。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}:: ^(/.+)(.+)::$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
或者您可以访问此 link 以获取更新的详细信息。 Htaccess file for any project