2

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?

1
  • Loading a view is not the same as loading a URL. A view is a file stored locally on your hard drive(well the servers), and you pass data to it via $data. You are attempting to pass data via URL and PHP is looking for an actual file of image/delete/113.php (automatically appends .php to views/controllers/models etc) Commented Nov 14, 2011 at 13:35

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that helps - haha, I thought I might be 'violating the MVC principle', that doesn't surprise me. I know it's there for my benefit, just gotta get used to it!
1

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

}

Comments

0

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.

2 Comments

Is it not possible to load a view like: ('controller/function/parameter')?
Not that I'm aware of. I'm not certain why you would want to. If you want to delete an image, just do "unlink(filename)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.