Fittersman
Fittersman

Reputation: 625

Yii - one controller multiple urls

I'm trying to create two Yii models that are very similar and can share the same database table. One is a 'question' and one is an 'article'. They both share a title, body, and author. 'Question' has an additional field in the table that 'article' does not need to interact with called follow_up.

Most methods and validation is the same, but there are some tiny differences that could easily be done with if statements. The main problem I'm seeing is the URL, I want separate URLs like site.com/question and site.com/article but have them both interact with the same model, controller, and views.

How can this be done?

Upvotes: 2

Views: 956

Answers (1)

Ansari
Ansari

Reputation: 8218

Use the urlManager component in the Yii config to set routes for /article and /question to go to the same controller, and then either use different actions or different parameters to differentiate between the two. Since you said they are almost identical, I would suggest different parameters and a single action as follows:

array(
  ...
  'components' => array(
    ...
    'urlManager' => array(
      'question/<\d+:id>' => 'mycontroller/myaction/type/question/id/<id>',
      'article/<\d+:id>' => 'mycontroller/myaction/type/article/id/<id>',
    ),
  ),
);

Of course you'll have to modify that for your needs, but that's the general setup. Look here for more information: http://www.yiiframework.com/doc/guide/1.1/en/topics.url

Upvotes: 5

Related Questions