3
votes

In codeIgniter I have created routes to remove the controller/methodname from the url so but the problem is that when I try to access the second controller index method it is not loading it's loading the home controller

I have 2 controller Home.php and Admin.php when i type in localhost/foldername/ it is opeing home conrtollers index method but when I type in localhost/foldername/admin it is redirecting to home controller's second method which is page can any one help me out in writing the routes.

Here is my routes which I have created

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

Admin Controller 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');
          }
    }

Now can anyone help me out to solve this issue one thing more when I comment the custom routes it is working perfectly but the home controller for the page which i tried to remove method and controller name it is coming as 404 not found

1
where is your page function which is you showing in this routes $route['([^/]+)/?'] = 'home/page/$1'; - Yaseen Ahmad
I beleive you did not noticed my Home.php controller - Mark Alan
PLease check my home controller and second method - Mark Alan
Replace your route to this $route['(.*)'] = 'home/page/$1'; - Yaseen Ahmad
$route['([^/home]+)/?'] = 'home/page/$1'; this will redirect to all home controller actions if there is home in url after root , otherwise it wiil keep redirecting any url to your page action - Ashutosh Rai

1 Answers

1
votes

According to what i undertand, Set your default controller, to check your authentication first:

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

To access your second controller's index function:

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

To access your second controller's page($id) function:

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