Search code examples
phpcodeigniterdefault

(Codeigniter) Can I use default controller with parameter?


Hello friends I'm working with codeigniter but I need your help.

I'm using the default controller like:

$route['default_controller'] = 'generals/view/index';

But when I enter to my localhost I see the 404 error.

I show my controller bellow.

public function view($page){
        $this->load->view('templates/header');
        $this->load->view('sections/'.$page);
        $this->load->view('templates/footer');
    }

I would be greatful for your help


Solution

  • According to the Codeigniter Documentation, default_controller is a reserved route:

    This route points to the action that should be executed if the URI contains no data,
    which will be the case when people load your root URL. The setting accepts a 
    controller/method value and index() would be the default method if you don’t specify 
    one. In the above example, it is Welcome::index() that would be called.
    

    Please update your default_controller to:

    $route['default_controller'] = 'generals/view';
    

    and in your controller

    public function view($page = 'index'){
            $this->load->view('templates/header');
            $this->load->view('sections/'.$page);
            $this->load->view('templates/footer');
        }
    

    For the rest of uri's, you need to define another route(s).