Dynamically Loading A Controller

I posted this on Stack Overflow as well.

I have had a good play around with the Yii Framework and now I want to take it a bit deeper and what I want to do is set up an application where several different URLs would point to the same controller.

Normally domain.com/content will point to class ContentController which is standard in MVC.

What I want to do is set up three controllers (maybe more but this will do to start), i.e. ArticlesController, DisplayController and SplashController.

I would then set up what is essentially a CMS for a client, and they would be able to create as many pages as they want and point them to the above three controllers, which I have already set up to handle the data.

So for instance my client could set up the following pages: news, notices, technical and have them all pointed to the ArticlesController, and also set up pages: management, specials, support and have them all pointed to the DisplayController.

I know that all those controllers could be creating using the Gii module, but in this case its not an option as I don’t think that’s suitable for non technical people.

I just want my client to be able to log into the CMS, decide he wants to create a new page called "randompage", point it to the ArticlesController using a drop down menu, then write a bunch of articles for it and now have those articles accessible at domain.com/randompage/article-1 domain.com/randompage/article-2

With the standard set up that would point to site/error because there is no controller RandompageController

What I’ve done so far is create a constructor in Controller class where I can overwrite the controller id

class Controller extends CController {

    function __construct($id) {

        // Code here which successfully pulls from the database

        // which controller the current page should point to.




If I check in the CController class, $this->_id = either articles, display or splash but the application itself stills loads site/error

I’m guessing I must have to set/override the Controller elsewhere. I have tried


but that doesn’t have any effect

Perhaps Yii is set up and must require a specific controller for each URL but that would mean developing rather rigid solutions for clients, and requiring them to call in the developer every time they want to add a new controller.

One other solution I’ve thought if is that I could load all pages and their controller’s from the database when I start up the application and load them into the url of the config/main.php file?

Hope I have explained what I’m trying to do well.


You could call internally Gii methods to generate all nesessar Controllers/models without involves the users.

Also another way: A simple trick to do that is using one controller/action with parameters that those parameters are the real controller action!

for example

public function actionMain($controller='site',$action='index') {

  //make anything you want with $controller and $action


And call this by


I posted this in a different topic a while ago which I can’t find anymore, but you should use custom URL rule classes.

In this class you can query the database and return which controller and action Yii should run. See the example.

Thanks mate, that is exactly what I need.