简体   繁体   中英

Make a controller indexcontroller in yii

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?

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>',
     ),
   ),
  ),
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM