在 Zend Framework 中为 SEO 更改路由
Change Routing in Zend Framework for SEO
我是 Zend 框架的新手,使用的是框架 2.0,我希望我的站点用户友好 URL。现在我的 URL 联系我们页面是 http://local.example.com/application/index/contact
但我想要
http://local.example.com/contact
像这样。
我的模块名称是 Application
,控制器名称是 IndexController (\module\Application\src\Application\Controller\IndexController.php)
这是我的 module.config
文件
namespace Application;
return array(
'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' => '/application',
'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]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
'reset' => array(
'type' => 'Segment',
'options' => array(
'route' => '/reset[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Application\Controller\Reset',
'action' => 'change-password',
),
),
),
),
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'factories' => array(
'translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
'Application\Controller\Reset' => 'Application\Controller\ResetController'
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'layout/pagination' => __DIR__ . '/../view/layout/pagination.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'flash/message' => __DIR__ . '/../view/layout/FlashMessage.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
'strategies' => array(
'ViewJsonStrategy',
),
),
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
);
您需要在配置中添加新的 Literal
路由,如下所示:
return array(
'router' => array(
'routes' => array(
// ...
// Put this AFTER "reset" route
'contact' => array(
'type' => 'Literal',
'options' => array(
'route' => '/contact',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
// UPDATE THIS FIELD WITH THE ACTUAL ACTION'S NAME
'action' => 'contact',
),
)
),
),
// ..
),
);
我是 Zend 框架的新手,使用的是框架 2.0,我希望我的站点用户友好 URL。现在我的 URL 联系我们页面是 http://local.example.com/application/index/contact
但我想要
http://local.example.com/contact
像这样。
我的模块名称是 Application
,控制器名称是 IndexController (\module\Application\src\Application\Controller\IndexController.php)
这是我的 module.config
文件
namespace Application;
return array(
'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' => '/application',
'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]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
'reset' => array(
'type' => 'Segment',
'options' => array(
'route' => '/reset[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Application\Controller\Reset',
'action' => 'change-password',
),
),
),
),
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'factories' => array(
'translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
'Application\Controller\Reset' => 'Application\Controller\ResetController'
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'layout/pagination' => __DIR__ . '/../view/layout/pagination.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'flash/message' => __DIR__ . '/../view/layout/FlashMessage.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
'strategies' => array(
'ViewJsonStrategy',
),
),
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
);
您需要在配置中添加新的 Literal
路由,如下所示:
return array(
'router' => array(
'routes' => array(
// ...
// Put this AFTER "reset" route
'contact' => array(
'type' => 'Literal',
'options' => array(
'route' => '/contact',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
// UPDATE THIS FIELD WITH THE ACTUAL ACTION'S NAME
'action' => 'contact',
),
)
),
),
// ..
),
);