Images and redirect

Hi guys!

I have an action in a controller that simply retrieves the filename of an image from an id and then redirects to it.

This is working fine, but I noticed this approach broke almost every lightbox script (redirect I suppose), so I started thinking that maybe (most likely ::) ) this is not the way to go.

Here’s the code:

public function actionView($id)


	if (!is_numeric($id))

		throw new CHttpException(404,'The requested page does not exist.');

	$model = Images::model()->find('p_id = <img src='' class='bbc_emoticon' alt=':P' />_id', array('<img src='' class='bbc_emoticon' alt=':P' />_id'=>$id));


	if($model === null)

		throw new CHttpException(404, 'The requested page does not exist.');

	$this->redirect(Yii::app()->request->baseUrl . '/images/' . $model->filename);


Any ideas about how I can implement this one better?

Thanks in advance.


I don’t think redirect is a good idea. Instead you should display an image in this action. Just render some headers and file contents. Example:

Thanks for the reply.

My problem is that images are not stored in the database and reading them from the disk seems like a massive overhead because they can be quite big sometimes.

Use something like Amazon S3, grab a copy of the Yii S3 Extension and you’re good to go. Then you just need to store the image name and extension in the database and massively reduce the disk load and load times

There is only 1 fast solution: use direct links to images, "/images/image_name.jpg" instead of "image/view/id/5".

A bit too much for this simple task, but this will come usefull in another project. Ty!

Yeah, I think I will end up with this solution. At least I will spare some db queries :)

Since I’m not that familiar with ActiveRecord , is that possible to extract all the values in a column (ie the ids of the posts) from a CActiveDataProvider? This way I’ll just make a SELECT IN in the images table before iterating on the posts.

Thanks guys!

Sounds ugly. Maybe you’ll describe your problem so we can help you to find a good solution ;)

Yeah it does :rolleyes:

Here’s the problem:

I have a list of entries and I usually show n of those in a page. Each entry may have 0 or n images stored in a table like (id, entry_id, url).

I tryed the controller approach because it was handy to just have to call an action named view to show the image.

The entries are displayed using a foreach on the getData method of the data provider.

I may call the Images model inside the foreach but that will make n queries. It seems logical to retrieve a list with all the images before and then make the img tags as needed.

You know when you have an idea you cant see alternatives :lol:

If you need to retrieve records with related records, then use with()! :D


$models = MyClass::model()->with('images')->findAll();

foreach ($models as $model)

    foreach ($model->images as $image)

        echo CHtml::image($image->getFileUrl()); // add a custom method to get image url

More info:

What can I say: if only I could be able to read everything. Guess what: 2 seconds arent enough to read the whole wiki :lol:

Thanks! I’ll try that asap!

EDIT: yeah it was as easy as that! Thank you :)