我正在尝试与这个站点一样,stackoverflow,对他们的 URL 做同样的事情。
CakePHP 的工作方式如下:网站/控制器/动作/
我想配置路由来实现这一点:
myWebSite.com/questions/(question_id )/ (问题) /
例如:myWebSite.com/questions/12874722/ cakephp-routing-controller-alias/
我没有弄清楚如何做 URL 的这个粗体部分。
我正在尝试与这个站点一样,stackoverflow,对他们的 URL 做同样的事情。
CakePHP 的工作方式如下:网站/控制器/动作/
我想配置路由来实现这一点:
myWebSite.com/questions/(question_id )/ (问题) /
例如:myWebSite.com/questions/12874722/ cakephp-routing-controller-alias/
我没有弄清楚如何做 URL 的这个粗体部分。
在你的 Config/routes.php
Router::connect('/questions/*', array('controller' => 'questions', 'action' => 'view'));
在 Controller/QuestionsController.php 中
查看操作获取问题 ID 为
public function view() {
$question_id = isset($this->request->params['pass'][0]) ? $this->request->params['pass'][0] : "";
$question = isset($this->request->params['pass'][1]) ? $this->request->params['pass'][1] : "";
if( empty($question_id) ) {
throw new NotFoundException('Could not find that question');
}
// if $question is empty then get slug from database and redirect to /question_id/question
// Get question details and set
}