Question

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

Was it helpful?

Solution 2

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

OTHER TIPS

The loader doesn't care about your routes, and besides:

$route['work/assign/(:any)'] = 'work/assign/$1';

...doesn't do anything. This code:

$this->load->view("work/assign/" . $id);

...will always look for a file in APPPATH/views/work/assign/$id.php which I assume does not exist.

You want to use the second paramter in view() to load variables, for example:

$this->load->view("work/assign", array('id' => $id));

This will load the view file work/assign.php and import the variable $id.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top