URL helper and pretty URLs

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.


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

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

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.

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