You can hide or remove controller name from url in Yii 2.0 through configuring rules for urlManager component. You can hide controller name from url even if your pages are generated dynamically using database after reading this article.
First of all you have to enable pretty permalinks. Read how to enable pretty permalinks and create seo friendly url if you didn’t do so yet. After that let’s try to understand how to configure application rules by simple examples.
For the examples, we have login
, logout
and register
actions under account
controller as well as index
, about
and contact
actions under a controller named site
. If you have enabled pretty url then there must be an entry in config/web.php
configuration file in your project as below:
1 2 3 4 5 6 | 'components' => [ 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, ], ... |
By default Yii has url route as controller\action
our application will be accessible using the following urls:
1 2 3 4 5 6 7 | http://www.yourdomain.com/account/login http://www.yourdomain.com/account/logout http://www.yourdomain.com/account/register http://www.yourdomain.com/site/index http://www.yourdomain.com/site/about http://www.yourdomain.com/site/contact |
And we want to display them as:
1 2 3 4 5 6 7 | http://www.yourdomain.com/login http://www.yourdomain.com/logout http://www.yourdomain.com/register http://www.yourdomain.com/index http://www.yourdomain.com/about http://www.yourdomain.com/contact |
We can create parameterized rules using aliasing to remove controller name from url in Yii.
1 2 3 4 5 6 7 8 9 10 | 'components' => [ 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ '<alias:index|about|contact>' => 'site/<alias>', '<alias:login|logout|register>' => 'account/<alias>', ], ], ... |
Here, we are defining an alias parameter that should be specified in URL. It will be passed as $action
parameter to SiteController::actionAbout
for about
page.
Sometimes you might also need to pass the alias as alias, instead of action. This is a common requirement when we create pages dynamically by passing their id or unique slugs to database. For example:
1 | http://www.yourdomain.com/product/unique-product-name |
For this purpose we can specify rule as:
1 | 'product/<alias>' => 'catalog/product' |
Here, to remove controller name from url in Yii 2.0, we are defining an alias parameter that should be specified in URL after /product/
. It can be virtually anything and it will be passed as $alias
parameter to CatalogController::actionProduct($alias)
.
this is the best article I’ve found on the topic. But can we use a wild card in aliases? Something like this:
” => ‘site/’
I know I wouldn’t have access to any other controller, but I don’t need them for now. And I think the aliasing rules can be overriden by the following rules in case I need to use any other controller.
No, it doesn’t work, So the issue is not resolved. Don’t want to use htaccess for it. Hell, yii2 turns out to be lame…
Hi Bogdan,
To be honest, I have not tested or applied it anywhere yet. So not aware much about. This.
Thanks
hii i am using your code for pretty url everything working fine problem with last rule alias and hide controller
http://local.advance.com/programdetail/alias=xyz
my rule is:’programdetail/’ => ‘programs/programdetail’,
but my url is not changed
Hi Rahul,
Yii won’t change url for you. You need to specify http://local.advance.com/programdetail/xyz in href attribute of anchor which will be displayed on brower’s address bar on click.
System will only route the request to correct controlleraction as specified in web.php along with xyz as $alias parameter.
thannx Amit
So how does one go about using unlimited get parameters in a pretty url route because /* does not work neither does ?(/param/)?
Hi Mez,
I had never worked under your requirement still I would like to add something. You have to extend the UrlManager class with help of some regular expression as param as get parameters would never work.
You know I had the thought to do so but at times I dont really like to extend the Factory defaults. Its kind of unfortunate that this is what we have to revert to since it was such a great feature from yii 1.1, very flexible.
I think Yii 2.0 is made more powerful and friendly to developers. You may right at your stand but the requirement is rare so it needs to get hand dirty to revert in newer version.