R
R
rumano2019-04-08 08:45:55
htaccess
rumano, 2019-04-08 08:45:55

How to configure urlManager in Yii2 to show image with template "/images/address_city_state_zip_PROPERTY-ID_IMAGE-ID.jpg"?

Good day.
The problem, in general, is this:
The image is loaded by ID from the image controller.
myserver/image/miniature-image/?property_id=44369&...
I want to make the image file name contain an address and an ID at the end, like this:
myserver/images/address_city_state_zip_ PROPERTY-ID _ IMAGE-ID .jpg,
where:

  • address_city_state_zip - address, city, state and postal code, substituted from the database
  • PROPERTY-ID - ID from the database
  • IMAGE-ID - picture number

The rule works for myserver/image/miniature-image/?property_id=44369 with urlManager disabled and adding a rule for Mod_Rewrite to .htaccess:
RewriteRule ^images/([a-zA-Z0-9_-]+)_([0-9 ]+)\.jpg$ /index.php?r=image/miniature-image&pid=$2
or
RewriteRule ^images/([a-zA-Z0-9_-]+)_([0-9]+)\. jpg$ /image/miniature-image&pid=$2
But it doesn't work when urlManager is included.
Can you please tell me how this can be implemented in Yii2?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rumano, 2019-04-17
@rumano

I solved the problem like this: I
added the following rule to the configuration:

'urlManager' => [
  'enablePrettyUrl' => true,
  'showScriptName' => false,
  'rules' =>  [
   'image/<id:[0-9a-zA-Z_-]+_\d{1,}_\d{1,3}_\d{1,3}.jpg>' => 'image/miniature-image'
  ],
],

Created a controller with actionMiniatureImage method
public function actionMiniatureImage()
{
  ... делаем парсинг $_GET
  ... извлекаем данные из БД
  ... генерируем изображение
}

We call from the browser:
we use in HTML:
We do NOT change anything in the .htaccess file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question