Mohit Bhansali
Mohit Bhansali

Reputation: 1785

Yii: How to use one model in another model via URL/slug, instead of ID?

I am developing an website in which I have event categories and events details. So that means first I want to use event category table for displaying all the events, and when user select an particular event, all events under that category will be displayed. Now if user will select any event for details, the url must be like below:

If user will select any event category say 'music', then:

www.domain.com/category/music/

When user will select event in music category say 'star-night' then:

www.domain.com/category/music/event/star-night

How can I achieve this task?

Upvotes: 1

Views: 229

Answers (1)

Samuel Liew
Samuel Liew

Reputation: 79052

In your config/main.php:

// uncomment the following to enable URLs in path-format
'urlManager' => array(

    'rules' => array(
        // other rules...
        'category/<category>/<controller:\w+>/<slug>' => '<controller>/view',

Event/or whatever controller will handle view of single item.

In each controller, have the view accept a slug/title string instead of the usual id.

public function actionView($slug) {
    $model = $this->loadModel($slug);

public function loadModel($slug) {
    $model = Event::model()->find('slug=:slug', array(':slug' => $slug));

Upvotes: 2

Related Questions