mrhigham
mrhigham

Reputation: 264

Load view passing variable to the controller's function - CodeIgniter

I'm using CodeIgniter and want to load a view such as:

$this->load->view('image/delete/' . $_POST['id'], $data);

The $_POST['id'] is a required parameter of my 'delete' function inside my 'image' controller. I would have thought this would work fine, but I'm getting a CI error stating:

"Unable to load the requested file: image/delete/113.php"

I don't understand why CI is adding ".php" to the end of this, no wonder it can't find this file. What am I doing wrong?

Upvotes: 2

Views: 1952

Answers (3)

thomaux
thomaux

Reputation: 19738

In addition to Ivan's answer, what you're trying to do is violating the MVC principle: separation of concern.

Views should just display data, they should not ever gain functionality like deleting resources.

Instead of loading a view, you'd need to use the redirect() function in the URL helper class to redirect the browser the url "image/delete/".$image_id

Then, as Ivan suggested, you will need an Image controller with a delete($id) function. This function will delete your image and afterwards load a view to indicate the resource has been deleted.

Upvotes: 1

Ivan
Ivan

Reputation: 491

The .php file extension does not need to be specified unless you use something other than .php.

Code in "image" controller:

public function delete($parameter=false)
{

some_delete_function($parameter);

//and load view

}

Upvotes: 1

Hans
Hans

Reputation: 3523

It looks like you are trying to use the view to load the URL /image/delete/113. But it is trying to load the view ~application/views/images/delete/113 from your hard drive.

I'd recommend just deleting the image from your code, maybe create a library if it's complex or a helper if it is not, and call it from the controller there.

Upvotes: 0

Related Questions