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?

1 Answer 1

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>',
     ),
   ),
  ),
);
Sign up to request clarification or add additional context in comments.

6 Comments

Is it not a conventional way to implement this by editing $this-> render in the controller??? Whether routing is a better way???
The config which was shown on this answer work with you? You should not edit render on CController btw.
It dint work actually instead I used: '/<id:\d+>'=>'article/view' which worked
I wanted to make the url like that in blogger so I used the following rule: '/<year:\d{4}>/<month:\d{2}/<vanity:\w+>'=>'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?
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.