Search code examples
codeignitercodeigniter-2

Can't access second controller in codeigniter


I have created twe separate controller one for admin and one for front-end i can access front end controller and can access it functions but when i try to access the admin controller i can't i am using .htacess file to rewrite the url .My url looks like that http://localhost/bookstore/index.

Front-end controller:

<?php 
class Bookstore extends CI_Controller {

   function __construct()
    {
        parent::__construct();
        $this->load->helper(array('url','form','file','cookie','captcha'));
        $this->load->library(array('session','pagination','form_validation'));
        $this->load->model('bkmodel');
    }

    public function index()
    {
        $this->load->view('index');
    }
}

Admin-Controller :

class Adminstore extends CI_Controller {

   function __construct()
    {
        parent::__construct();
        $this->load->helper(array('url','form','file','cookie','captcha'));
        $this->load->library(array('session','pagination','form_validation'));
        $this->load->model('admin_model');
    }

   public function index()
    {
        $this->load->view('admin/index');
    }
}

Route file look like this:

$route['default_controller'] = "bookstore";
$route['(:any)'] = "bookstore/$1";
$route['404_override'] = '';

And i am stuck here that why the second controller is not working please point out my mistake so i can procced further one more thing when i want to access admin my url is like this http://localhost/bookstore/adminstore/index and it say 404 page not found


Solution

  • You need another routing rule before that "any" rule which redirects pretty much everything. So, something like this should work:

    $route['default_controller'] = "bookstore";
    $route['adminstore/(:any)'] = "adminstore/$1";
    $route['(:any)'] = "bookstore/$1";
    $route['404_override'] = '';
    

    Remember, rules are checked in order, from top to bottom, so anything less specific/global should go at the top. "any" rules should be at the very bottom.