我有两个问题与用户友好的URL.
我有一个路由器设置如下:
>实际的网址是http://www.example.com/course/view-batch/course_id/19
>我想要一个更友好的URL http://www.example.com/java-training/19
我在application.ini中设置了以下路由:
resources.router.routes.viewbatchcourse.route = "/:title/:course_id/" resources.router.routes.viewbatchcourse.defaults.controller = course resources.router.routes.viewbatchcourse.defaults.action = view-batch resources.router.routes.viewbatchcourse.reqs.course_id = "\d+"
这样做很好.
>实际的网址是http://www.example.com/course/view-reviews/course_id/19
>我想要一个更友好的URL http://www.example.com/java-reviews/19
我意识到它不可能,因为一个路由已经被设置为匹配该格式.
所以我正在考虑是否可能使用正则表达式,并检查标题是否包含“评论”,然后使用此路由.
resources.router.routes.viewreviews.type = "Zend_Controller_Router_Route_Regex" resources.router.routes.viewreviews.route = "/:title/:course_id" resources.router.routes.viewreviews.defaults.controller = "course" resources.router.routes.viewreviews.defaults.action = "view-reviews" resources.router.routes.viewreviews.reqs.course_id = "\d+" resources.router.routes.viewreviews.reqs.title = "\breviews\b"
最近我有这个工作是
resources.router.routes.viewreviews.route = "/:title/:course_id" resources.router.routes.viewreviews.defaults.controller = "course" resources.router.routes.viewreviews.defaults.action = "view-reviews" resources.router.routes.viewreviews.reqs.course_id = "\d+" resources.router.routes.viewreviews.reqs.title = "reviews"
现在,如果我输入URL http://www.example.com/reviews/19,那么查看评论动作被调用.
是否可能 – 检查标题是否包含单词“评论” – 那么这条路线应该被调用?
回到我之前的工作路线http://www.example.com/java-training/19:
resources.router.routes.viewbatchcourse.route = "/:title/:course_id/" resources.router.routes.viewbatchcourse.defaults.controller = course resources.router.routes.viewbatchcourse.defaults.action = view-batch resources.router.routes.viewbatchcourse.reqs.course_id = "\d+"
我只希望URL为http://www.example.com/java-training
这可能吗?
1)你可以使用Route_Regex来实现你想要的
原文链接:https://www.f2er.com/php/131466.htmlprotected function _initRoutes() { $router = Zend_Controller_Front::getInstance()->getRouter(); $route = new Zend_Controller_Router_Route_Regex( '([a-zA-Z]+)-reviews/(\d+)',array( 'module' => 'default','controller' => 'course','action' => 'view-reviews' ),array( 1 => 'language',2 => 'course_id',) ); $router->addRoute('review',$route); $route = new Zend_Controller_Router_Route_Regex( '([a-zA-Z]+)-training/(\d+)','action' => 'view-batch' ),) ); $router->addRoute('training',$route); }
2)对于第二点,我看不出它是如何可能的.
你可以做的一件事是使用课程的名称,如果你有一个,显示一个url像:
www.xyz.com/java-training/my-awesome-course-19 www.xyz.com/java-training/19/my-awesome-course
这将是很容易使用我上面提到的路线.