$route['written-qb/(:any)/(:any)/(:any)']不起作用。
P粉155832941
P粉155832941 2023-07-30 19:03:57
[PHP讨论组]
<p>当我使用uri段2和3时,它可以工作,但是当我添加uri段4时,它就不起作用了。</p><p>URL应该像这样... http://localhost/maruf/written-qb/bcs/44th-bcs-english/how-has-the-phrase-digital-detox-been-explained-in-the-passage?</p><p>但是它显示为... http://localhost/maruf/written-qb/bcs/how-has-the-phrase-digital-detox-been-explained-in-the-passage?而且两个URL都显示404。</p><p>这是我的路由设置。</p><p><br /></p> <pre class="brush:php;toolbar:false;">$route['written-qb/(:num)'] = 'written-qb'; //works $route['written-qb/(:any)/(:any)'] = 'written-qb/written_qb_details/$1/$2'; //works $route['written-qb/(:any)/(:any)/(:any)'] = 'written-qb/written_qb_answer/$1/$2/$3'; //does not work</pre> <p>My controller is...</p> <pre class="brush:php;toolbar:false;">public function index(){ $data['qb_list'] = $this-&gt;Question_bank_model-&gt;get_qb_with_category(FALSE); //footer data $data['main_content'] = 'written_qb'; $this-&gt;load-&gt;view('include/template',$data); } // works fine public function written_qb_details($category, $slug = NULL){ $config['uri_segment'] = 2; $slug = $this-&gt;uri-&gt;segment(3); //data $data['qb_list'] = $this-&gt;Question_bank_model-&gt;get_qb_with_category(FALSE); $data['qb_info'] = $this-&gt;Question_bank_model-&gt;get_qb_details($slug, $config['uri_segment']); if(empty($data['qb_info'])){ show_404(); } $data['url_slug'] = $data['qb_info']['qb_exam_slug']; $data['meta_title'] = $data['qb_info']['qb_exam']; $data['meta_description'] = $data['qb_info']['qb_exam_post_meta']; $data['meta_keywords'] = $data['qb_info']['qb_exam_post_tags']; //view $data['main_content'] = 'written_qb_details'; $this-&gt;load-&gt;view('include/template',$data); } // works fine public function written_qb_answer($slug = NULL, $slug2 = NULL){ $config['uri_segment'] = 2; $slug = $this-&gt;uri-&gt;segment(3); $slug2 = $this-&gt;uri-&gt;segment(4); //data $data['qb_info'] = $this-&gt;Question_bank_model-&gt;get_qb_answer_details($slug, $slug2, $config['uri_segment']); if(empty($data['qb_info'])){ show_404(); } $data['url_slug'] = $data['qb_info']['qb_exam_question_slug']; $data['meta_title'] = $data['qb_info']['qb_exam_question']; $data['meta_description'] = $data['qb_info']['qb_exam_answer_meta']; $data['meta_keywords'] = $data['qb_info']['qb_exam_answer_tags']; //view $data['main_content'] = 'answer'; $this-&gt;load-&gt;view('include/template',$data); } // it does not work</pre> <p>而我的模型是...</p> <pre class="brush:php;toolbar:false;">public function get_qb_details($slug = FALSE){ if($slug === FALSE){ $this-&gt;db-&gt;order_by('qb_post.qb_exam_slug', 'DESC'); $this-&gt;db-&gt;join('qb_category', 'qb_category.qb_category_name_slug = qb_post.qb_category_name_slug'); $this-&gt;db-&gt;where('qb_exam_active',1); $query = $this-&gt;db-&gt;get('qb_post'); return $query-&gt;result_array(); } $query = $this-&gt;db-&gt;get_where('qb_post', array('qb_exam_slug' =&gt; $slug)); return $query-&gt;row_array(); } public function get_qb_answer_details($slug2 = FALSE){ if($slug2 === FALSE){ $this-&gt;db-&gt;where('qb_exam_answer_active',1); $query = $this-&gt;db-&gt;get('qb_exam_ans'); return $query-&gt;result_array(); } $query = $this-&gt;db-&gt;get_where('qb_exam_ans', array('qb_exam_question_slug' =&gt; $slug2)); return $query-&gt;row_array(); }</pre> <p>在控制器"written_qb_answer"中,以及在路由$route['written-qb/(:any)/(:any)/(:any)'] = 'written-qb/written_qb_answer/$1/$2/$3';中,不起作用。它显示404错误。</p>
P粉155832941
P粉155832941

全部回复(1)
P粉323224129

您的路由重叠了。

$route['written-qb/(:num)'] = 'written-qb';  //works
$route['written-qb/(:any)/(:any)'] = 'written-qb/written_qb_details/$1/$2';  //works
$route['written-qb/(:any)/(:any)/(:any)'] = 'written-qb/written_qb_answer/$1/$2/$3';  //does not work

请查看文档中的注释:
注释1:
注释2:
注释3:

路由不是过滤器,当您使用(:any)时,它表示任何内容!为什么第一个和第二个有效?因为您首先检查是否为数字,对于第一个未捕获的任何内容都会被第二个捕获,这意味着第三个永远不起作用。这就像是如果...否则...而不是如果...否则如果...否则...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号