例如,我使用以下 URL 作为用户个人资料/仪表板:
example.com/person/person/profile
example.com/person/person/dashboard
其中人员是模块,第二个人员是 Controller ,个人资料和仪表板是操作。
但我希望如果用户访问以下网址,那么他们应该分别指向上面提到的相同网址:
example.com/person/profile
example.com/person/dashboard
有什么办法可以让我始终从 URL 中删除模块名称吗?请记住我想保留当前的目录结构。
谢谢
最佳答案
您可以使用路由来做到这一点,在您的 Bootstrap 中添加此
protected function _initRoutes()
{
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini');
$router = new Zend_Controller_Router_Rewrite();
$router->addConfig($config, 'routes');
Zend_Controller_Front::getInstance()->setRouter($router);
}
然后在您的配置管理器中创建一个名为 routes.ini
的文件,其中将包含您的自定义路由,请尝试这些
routes.person_profile.route = "person/profile"
routes.person_profile.defaults.controller = person
routes.person_profile.defaults.action = profile
routes.person_profile.defaults.module = person
routes.person_dashboard.route = "person/dashboard"
routes.person_dashboard.defaults.controller = person
routes.person_dashboard.defaults.action = dashboard
routes.person_dashboard.defaults.module = person
这里我们定义了两条路由,请记住给每个路由一个唯一的名称,我将它们称为 person_profile
和 person_dashboard
。
route
值是将被 controller
、action
和 module
值覆盖的 URI,例如,路由 person/profile
将自动分派(dispatch)到 person
Controller 、person
模块和 profile
行动。
希望有帮助。
关于php - 如何在 Zend 中缩短 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5291610/