0

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?

tereško
  • 58,060
  • 25
  • 98
  • 150

1 Answers1

2

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>',
     ),
   ),
  ),
);
Telvin Nguyen
  • 3,569
  • 4
  • 25
  • 39
  • Is it not a conventional way to implement this by editing $this-> render in the controller??? Whether routing is a better way??? – Vivek Sekhar Aug 13 '13 at 04:23
  • The config which was shown on this answer work with you? You should not edit render on CController btw. – Telvin Nguyen Aug 13 '13 at 04:36
  • It dint work actually instead I used: '/'=>'article/view' which worked – Vivek Sekhar Aug 13 '13 at 04:51
  • I wanted to make the url like that in blogger so I used the following rule: '//'=>'article/view' Now in the load model 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? – Vivek Sekhar Aug 13 '13 at 04:55
  • Yeah, it's fine. Just I couldn't get it since it was out of scope of your question. My answer just focus on how solve original question about hiding the controller name. – Telvin Nguyen Aug 13 '13 at 05:02
  • As I am new to yii I wanted to know the right method to implement it so that the purpose of the mvc framework is not violated. I now understood that rendering a different controller inside a particular controller is not the right approach. – Vivek Sekhar Aug 13 '13 at 05:07