Hide or remove controller name from url in Yii 2.0

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:

By default Yii has url route as controller\action our application will be accessible using the following urls:

And we want to display them as:

We can create parameterized rules using aliasing to remove controller name from url in Yii.

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:

For this purpose we can specify rule as:

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).

You Might Interested In

  • Mez

    So how does one go about using unlimited get parameters in a pretty url route because /* does not work neither does ?(/param/)?

    • Amit Sonkhiya

      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.

      • Mez

        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.

        • Amit Sonkhiya

          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.

  • Deora Rahul Singh

    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

    • Amit Sonkhiya

      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.

      • Deora Rahul Singh

        thannx Amit

  • Bogdan Transcendentov

    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…

    • Amit Sonkhiya

      Hi Bogdan,

      To be honest, I have not tested or applied it anywhere yet. So not aware much about. This.

      Thanks