Yii Framework Forum: Dynamic default controller - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Dynamic default controller Rate Topic: ***** 7 Votes

#1 User is offline   datashaman 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 27-September 09
  • Location:Cape Town, South Africa

Posted 28 September 2009 - 02:53 AM

For my app, I needed a dynamic default controller, because the app must display totally different content depending on which URL is being accessed.

Since you cannot assign dynamic values in the config/main.php (Yii has not loaded by that point), I used the following technique to do the job:

class SiteRouter
{
  public static function routeRequest($event)
  {
    $sender = &$event->sender;
    $defaultThemes = param('defaultThemes');
    $uri = empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'];
    if($uri == param('adminUri')) {
      $sender->defaultController = 'admin';
      $sender->theme = $defaultThemes['admin'];
      return;
    } else {
      $site = Site::model()->findByAttributes(compact('uri'));
      if(!empty($site)) {
        $sender->defaultController = 'site';
        $sender->theme = empty($site->theme) ? $defaultThemes['site'] : $site->theme;
        return;
      }

      throw new CHttpException(404, 'Unable to find a site hosted here with that hostname');
    }
  }
}


Then in protected/config/main.php on the top-level of the config array:

...
  'onBeginRequest' => array('SiteRouter', 'routeRequest'),
  'params' => array(
    'defaultThemes' => array(
      'admin' => 'defaultAdminTheme',
      'site' => 'defaultSiteTheme',
    ),
  )
...


If there's an easier way of doing this, please let me know.

BTW, this uses the shortcuts defined elsewhere on the site, such as param("...") for Yii:app()->params->{"..."}

This post has been edited by datashaman: 28 September 2009 - 03:35 AM

2

#2 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 29 September 2009 - 06:05 PM

This looks like a good solution too me. I did something like that in the index.php entry-script (for i18n-subdomaining). Your way seems way better.
0

#3 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,871
  • Joined: 04-October 08
  • Location:DC, USA

Posted 29 September 2009 - 08:56 PM

Very nice!
0

#4 User is offline   acainoks 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 16-November 10
  • Location:Poznań - Poland

Posted 16 November 2010 - 06:59 AM

Hi where I must write class SiteRouter?
0

#5 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 16 November 2010 - 09:08 AM

View Postacainoks, on 16 November 2010 - 06:59 AM, said:

Hi where I must write class SiteRouter?


create a file named "SiteRouter.php" in your application root components folder
--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#6 User is offline   got 2 doodle 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 247
  • Joined: 12-December 08
  • Location:Beautiful Nova Scotia, Canada

Posted 16 November 2010 - 11:24 AM

It's not clear why you need this functionality but this is one approach that I have used.
See Wiki Article

@datashaman I just looked a little closer at your post and I think the wiki example is very limited by comparison.

doodle
Check out myYii powered website
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users