无法加载第二个控制器

时间:2016-05-30 09:59:17

标签: php codeigniter

在codeIgniter中我创建了从url中删除控制器/方法名的路由,但问题是当我尝试访问第二个控制器索引方法时,它没有加载它正在加载家庭控制器

我输入2个控制器Home.php和Admin.php当我输入localhost / foldername /它正在打开home conrtollers索引方法但是当我输入localhost / foldername / admin时它会重定向到home controller的第二个方法,即页面任何人都可以帮我写出路线。

这是我创建的路线

$route['default_controller'] = 'home';
$route['([^/]+)/?'] = 'home/page/$1'; //If I comment this it is working properly for me 

管理员控制器Admin.php

class admin extends CI_Controller {

    public function index() {
        if($this->session->userdata('is_logged_in') == true) {
            $this->load->view('admin/dashboard');
        } else {
            $this->load->view('admin/login');
        }
    }
}

Home Controller Home.php

class home extends CI_Controller {      
          public function index() {
              $front_page         = $this->get_data->front_page();
              $page_data            = $this->get_data->AllData('pages', $front_page);
              $data['title']        = $page_data->row()->pagetitle;
              $class                = explode("/", $page_data->row()->template);
              $data['body_class'] = $class[1];

              $this->load->view('includes/header.php', $data);

              if($class[1] == 'home') {
                  $this->load->view('templates/slider');
              }

              $this->load->view('templates/navigation.php');

              $page_content = $page_data->row()->template;
              $this->load->view($page_content, $data);
              $this->load->view('templates/footer-form.php');
              $this->load->view('includes/footer.php');
        }
        public function page($id) {
              $page_data = $this->get_data->AllData('pages', $id);
              $data['title'] = $page_data->row()->pagetitle;
              $class = explode("/", $page_data->row()->template);
              $data['body_class'] = $class[1];

              $this->load->view('includes/header.php', $data);

              if($class[1] == 'home') {
                  $this->load->view('templates/slider');
              }

              $this->load->view('templates/navigation.php');

              if($class[1] == 'home') {
                  $data['slider'] = 'templates/slider';
              }

              $data['content'] = $page_data->row()->template;
              $this->load->view('index', $data);
              $this->load->view('templates/footer-form.php');
              $this->load->view('includes/footer.php');
          }
    }

现在任何人都可以帮助我解决这个问题,当我评论它完全正常工作的自定义路线但我尝试删除方法和控制器名称的页面的主控制器它将来的时候没有找到404 < / p>

1 个答案:

答案 0 :(得分:1)

根据我的承诺,设置默认控制器,首先检查您的身份验证:

$route['default_controller'] = 'admin'; 
//so that you can check weather the user is logged in or not.

要访问第二个控制器的索引功能:

$route['Home']  = "home";

要访问第二个控制器的页面($ id)功能:

$route['Home/Page/(:num)']  = "home/page/$1";
// where num id the ID you will be passing to the page function.