0

I created some CMS pages for my site.And now I want to make it in pretty way like this eg: domain.in/aboutus

And I created a page alias for each CMS page in my db.
If I call these page alias, I should get the corresponding pages.

I am attching my rules here

     'urlManager' => [               
            'showScriptName' => false,  // Disable index.php
            'enablePrettyUrl' => true,  // Disable r= routes
            //'enableStrictParsing' => true,
            'rules'=>array(
'aboutus'=>'cms/index/1' //I need this line dynamically
        '<siteName:\w+>/<role:(teacher|parent)>' => 'customers/login',
        '<controller:\w+>/<id:\d+>'=>'<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
        '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
       ),
        ],  

Can anybody help to fix this issue?

Phiter
  • 14,570
  • 14
  • 50
  • 84
MIK
  • 876
  • 1
  • 16
  • 37
  • Can you quote two different scenarios , to explain this dynamic feature u intend to achieve ? – ck_arjun Mar 16 '16 at 14:36
  • @ck_arjun hai.. my current URL scenario is https://www.project_name.in/cms/index/1 I need the URL like this http://www.project_name.in/aboutus – MIK Mar 17 '16 at 10:35
  • Check if its working . http://stackoverflow.com/a/36057877/5658658 – ck_arjun Mar 17 '16 at 10:48
  • Thank you arjun,Its really worked...Thank for your time... – MIK Mar 17 '16 at 12:07

1 Answers1

0

You're probably thinking about URL slugs.

You started well anyway, but you have to implement a lot more than just url rules. The proper way to do this is implementing the Yii2 Sluggable Behavior, which is done at the model level, and support the slugs at the controller level. This is not a short task, but it shouldn't take a whole day to implement it.

This was answered before, so you can have a look at that.

A more complete step-by-step tutorial can be found in this tutorial.

In summary, you have to:

  1. Add a slug attribute to your "page" model and in your db.
  2. Add the sluggable behavior to the model.
  3. Define which action in the controller will be dealing with slugs.
  4. Set the url manager to deal with slugs.

Hope this helps.

Community
  • 1
  • 1
XzAeRo
  • 566
  • 5
  • 11
  • Thank you for the answer but I need the url like this project_name.in/page_name.insted of loading the controller I need page name... we can achieve this in Yii 1 ny using the 'ext.DbUrlManager.EDbUrlManager' ..Is there any way to achieve this in Yii2?? – MIK Mar 17 '16 at 10:42