Same Controller, But Multiple Urls?

Hi,

I am building a CMS, where we have pages. Every page can have a different TYPE (Page, Blog, Etc).

The type for a page is 0, but blog has 1 and an custom page will be have something like 101.

I like to have all requests are handled through the same render controller, but with different urls, something like the following;

Page: www.domain.com/page/this-is-my-page => routes to the "page/render" controller

Blog: www.domain.com/blog/this-is-my-blog => routes to the "page/render" controller

Custom page: www.domain.com/custom-page/this-is-my-custom-page => routes to the "page/render" controller.

In my CUrlManager routes i have the following;


'rules' => array(

    'pagina/<slug:.*?>' => 'page/render'

),

This is working well, but how can I add the other two rules? I think it should something like this;


'rules' => array(

    'blog/<slug:.*?>' => array('page/render', 'defaultParams' => array('type' => 1)),

    'custom-page/<slug:.*?>' => array('page/render', 'defaultParams' => array('type' => 101)),

    'pagina/<slug:.*?>' => 'page/render'

),

My render action;


public function actionRender($slug = false, $type = SysPage::TYPE_PAGE) {


        // if specific slug provided, find page

        if($slug) {


            // find page by slug

            $page = SysPage::model()->findByAttributes(array(

                'slug'      => $slug,

                'type'      => $type,

                'status'    => SysPage::STATUS_PUBLISHED,

            ));


        // no specific page slug provided, fetch root page

        } else {


            // find page by slug

            $page = SysPage::model()->findByAttributes(array(

                'site_root' => true,

                'type'      => SysPage::TYPE_PAGE,

                'status'    => SysPage::STATUS_PUBLISHED,

            ));

        }


        if(!$page) {

            throw new CHttpException(404, Yii::t('pageModule.messages', 'The requested page cannot be found.'));

        }


        // render

        $html = $this->_render($page);


        // render view

        $this->render('render', array(

            'html' => $html,

        ));

    }

But this doesn’t work, it always takes the first route.

Can I fix this without creating an controller for each page type?

Hi, configure the rules property of urlManager component (in protected/config/main.php):




'urlManager'=>array(

   // other stuff

   'rules'=>array(

      '<controller:(post|blog|etc)>/<action:>' => 'page/<action>',

      // or if you want to use one action use this

      // '<controller:(post|blog|etc)>/<action:>' => 'page/render',

   )

)



i

Hi Masoud,

Thanks for your reply.

I understand your post, but how can i translate my page type ID, for example 0,1 or 101 to /page/ /blog/ and /custom-post/ ?