Yii Framework Forum: URL helper and pretty URLs - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

URL helper and pretty URLs

#1 User is offline   phantomlord 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 15-October 17

Posted 09 November 2017 - 02:07 AM

Hello! Is there any way to make pretty URL with HTML::a(), Url::to(), Url::toRoute() functions?
I have the following rule:
'admin/<action:\w+>/<id:\w+>' => 'admin/<action>'

So the path like "admin/edit/5" is equivalent to "admin/edit?id=5".

Then, in a view I have the following call:
 <?= Html::a('edit', ['edit', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>

Which produces the url like "admin/edit?id=5".

Can I force it to produce URL "admin/edit/5"?

I found a workaround:
<?= Html::a('edit', "edit/{$model->id}", ['class' => 'btn btn-primary']) ?> 

But this is not much better than just a plain hardcoded html code.
0

#2 User is offline   umneeq 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 10-October 11
  • Location:Russia

Posted 09 November 2017 - 03:46 AM

<?= Html::a('edit', ['admin/edit', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>

1

#3 User is offline   phantomlord 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 15-October 17

Posted 10 November 2017 - 03:39 AM

umneeq, your suggestion is exactly the same as
<?= Html::a('edit', ['edit', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>

And it doesn't produce a pretty url
0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,370
  • Joined: 16-February 11
  • Location:Japan

Posted 10 November 2017 - 04:50 AM

Another rule in your rules could fetch "admin/<action>" route before the one you are expecting.
Could you show us all the rules?

[EDIT]
If you have the following rules, for example, you won't get the result that you want.
'rules' => [
    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    'admin/<action:\w+>/<id:\w+>' => 'admin/<action>',
    ...
],

Note that the first rule matches "['admin/edit', 'id' => $model->id]", and the 2nd one has no chance to be applied.

So you have to check all the rules and their appearing order, not just the one that you are concerned.
1

#5 User is offline   phantomlord 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 15-October 17

Posted 14 November 2017 - 03:35 AM

softark, great, thanks! I didn't know that the order of rules matters.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users