Make a controller indexcontroller in yii

101 views Asked by At

I am planning to make a blog in yii. I have a table named article and its corresponding model,view,contoller is generated using gii. I want the posts to be displayed in the home page so I set

defaultController='article'

Although posts are displayed in the homepage, when I click the title for readmore, the url still has the contoller name in it like

www.yiisite.com/article/1

So I want the url to be like this instead:

www.yiisite.com/1

I want to hide controller name in my URL.

What is the conventional method to implement it?

I wanted to make the url seo friendly so I used the following rule:

'/<year:\d{4}>/<month:\d{2}/<vanity:[\w\W]+>'=>'article/view' 

Now in the loadmodel() in the ArticleController I wish to change findByPk($id) to fetch the data using year,month and a unique vanity url. So I will get the url like www.yiisite.com/2013/07/vanity-url-article.

This approach is fine right?

1

There are 1 answers

6
Telvin Nguyen On

Update urlManager on site config

return array(
 'name'=>'My Project',
 'defaultController'=>'article',
 'components'=>array(
   'urlManager'=>array(
     'urlFormat'=>'path',
     'caseSensitive' =>true,
     'showScriptName'=>false,
     //'useStrictParsing'=>true,
     'rules'=>array(
       '<action:[\w\-]+>' => 'article/<action>',
     ),
   ),
  ),
);