Search code examples
phpcodeignitertemplatesphp-include

CodeIgniter parameters - unable to load requested file


I have this controller:

class Work extends MY_Controller {
    public function assign($id) {
        //validation checks
        if ($this->form_validation->run() === FALSE) {
            $this->load->view("work/assign/" . $id);
        } else {
            //success
        }
    }
}

As well as a view at views/work/assign.php I also have the following route: $route['work/assign/(:any)'] = 'work/assign/$1';

When I go to /work/assign/1 in my browser, and I get this error: Unable to load the requested file: work/assign/1.php I want it to render the page with the parameter 1


Solution

  • The error you are getting is due to the wrong parameter in the view call

    $this->load->view("work/assign/" . $id); since your last trailing slash tell CI that this is the .php and in your last trailing slash your $id=1 so it searches to load the 1.php the correct way to load the view and pass your id to view is make an array which contains the data you want to show in view then call view and pass the array, the array keys will converted to variables by CI

    class Work extends MY_Controller {
        public function assign($id) {
            //validation checks
            if ($this->form_validation->run() === FALSE) {
            $data['id']=$id;
                $this->load->view("work/assign" ,$data);
            } else {
                //success
            }
        }
    }
    

    In assign.php just echo $id and you will get desired id