php $route ['written-qb/(:any)/(:any)/(:any)']不工作

vdzxcuhz  于 2023-08-02  发布在  PHP
关注(0)|答案(1)|浏览(87)

当我使用uri段2,3时,它工作,但当添加uri段4时,它不工作。
URL应该是这样的…http://localhost/maruf/written-qb/bcs/44th-bcs-english/how-has-the-phrase-digital-detox-been-explained-in-the-passage?
但它表现得像这样... http://localhost/maruf/written-qb/bcs/how-has-the-phrase-digital-detox-been-explained-in-the-passage?并且两个URL都显示为404。
这是我的路线

$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

字符串
我的控制器是...

public function index(){
        
        $data['qb_list'] = $this->Question_bank_model->get_qb_with_category(FALSE);
        //footer data
        $data['main_content'] = 'written_qb';
        $this->load->view('include/template',$data);
    } // works fine
    
    public function written_qb_details($category, $slug = NULL){
        
        $config['uri_segment'] = 2;
        $slug = $this->uri->segment(3);
        //data
        $data['qb_list'] = $this->Question_bank_model->get_qb_with_category(FALSE);
        $data['qb_info'] = $this->Question_bank_model->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->load->view('include/template',$data);
    } // works fine
    
    public function written_qb_answer($slug = NULL, $slug2 = NULL){
        
        $config['uri_segment'] = 2;
        $slug = $this->uri->segment(3);
        $slug2 = $this->uri->segment(4);
        //data
        $data['qb_info'] = $this->Question_bank_model->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->load->view('include/template',$data);
    } // it does not work


我的模特是...

public function get_qb_details($slug = FALSE){
            
            if($slug === FALSE){
                $this->db->order_by('qb_post.qb_exam_slug', 'DESC');
                $this->db->join('qb_category', 'qb_category.qb_category_name_slug = qb_post.qb_category_name_slug');
                $this->db->where('qb_exam_active',1);
                $query = $this->db->get('qb_post');
                return $query->result_array();
            }
        
            $query = $this->db->get_where('qb_post', array('qb_exam_slug' => $slug));
            return $query->row_array();
        }
        public function get_qb_answer_details($slug2 = FALSE){
            
            if($slug2 === FALSE){
                $this->db->where('qb_exam_answer_active',1);
                $query = $this->db->get('qb_exam_ans');
                return $query->result_array();
            }
        
            $query = $this->db->get_where('qb_exam_ans', array('qb_exam_question_slug' => $slug2));
            return $query->row_array();
        }


在控制器“written_qb_answer”和路由器$route ['written-qb/(:any)/(:any)/(:any)']= 'written-qb/written_qb_answer/$1/$2/$3';不起作用。显示404

vsikbqxv

vsikbqxv1#

您的路线重叠:

$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

字符串
检查documentation中的注解
注1:
通配符实际上是正则表达式的别名,其中:any被翻译为[^/]+,:num被翻译为[0-9]+。
注2:
路由将按照定义的顺序运行。较高的路由总是优先于较低的路由。
注3:
路由规则不是过滤器!设置规则,例如'foo/bar/(:num)'不会阻止控制器Foo和方法bar被非数字值调用,如果这是一个有效的路由。
路由不是过滤器,当你使用(:any)时,它意味着任何东西!为什么第一个和第二个有效?因为你首先检查它是否是数字的,并且对于任何在第一个中没有捕获的东西,它都会在第二个中捕获,这意味着第三个永远不会工作。它类似于if... else...而不是if ... else if ... else ...

相关问题