Question

I'm working with the Yii Framework and for our site we are looking to make it more modular. What this means to us is that we want to separate the logic of some of our components into their own component/app folders.

Currently what I am doing is extending the CController on each Controller class with my own.

e.g.

class SiteController extends MyController

In MyController I am going to load components via the actions() method:

public function actions() {
    return array(
        'login'=>array(
            'class'=>'application.components.Auth',
        ),
        'logout'=>array(
            'class'=>'application.components.Auth',
        ),
    );
}

Inside my Auth component I have a run() method:

public function run() {
//  Get the action string from the URL
    $uri = (Yii::app()->getRequest()->getRequestUri());
    $method = 'action'.ucfirst(substr($uri,strrpos($uri,'/')+1));

//  Run the method from the URI
    $this->$method();
}

Which will run the action method I have inside my Auth component. I should also mention that my Auth component doesn't extend CWidget, it extends CAction.

Inside Auth I have my method: actionLogin() and actionLogout() to match the above actions in MyController.

When I try and render my view files which I would like to be retained inside my modular component it won't recognize that there is a view file inside the component folder but rather looks in the default views or themes folders.

I have tried to load the pass from the protected root: '//application/components/auth/login'

Note: What my overall goal with trying to do this is to give me the ability to create multiple modular sections/apps/components of my site where the URL structure doesn't change and based on how I set up the actions method array it will load the specific component for that URL. I may have two forms of authentication on the site, or a future component might be the ability to have a weekly schedule and monthly schedule components that are loaded based on what I put in the actions array.

I have investigated modules, but I found that the URLs would not stay consistent. I welcome any advice on this. Thank you.

Was it helpful?

Solution

Firstly, create a function in your component that lists the local action handlers

public static function actions(){
    return array(
        // name action and point to the location where the action class is
        'GetActivity'       =>'application.components.Auth.login',
        'GetActivitytype'   =>'application.components.Auth.logout',
    );
}

And load your views as follows :-

return $this->renderPartial('application.components.views.auth_login',
                             array('model'    => $userModel),
                             true
                           );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top