Accessing A Sub Module Without A Sub Module

Hi gang,

I have an unusual situation. I’m have an admin site that needs to modify 2+ identical db’s.

I have a module called ‘podcast.’ I have 2 (maybe more) radio stations that have separate databases that are identical, so the view files used are the same files. All I have to do is change the dbConnection, as needed. My problem is figuring out how to ‘pass in’ that I am modifying StationA and not StationB, or visa-versa.

I would like to use something like localhost/podcast/staa/post.html or localhost/podcast/stab/post.html.

UrlManager/htaccess is already setup for the format above. I’m looking at how to use the ‘staa’ or ‘stab’ to switch the getDbConnection() info.

In the podcast module do I have to add a staa module? If so, how can I redirect EVERYTHING to the Podcast views/controllers/etc? Can I catch the station info in the podcastModule->beforeAction() event? But then how do I generate links with urlManager?

If this is not clear, post question and I will get back. Thanks in advance.

jk

You don’t need submodules, you can use different controllers in the podcast module.

One ‘PodcastBaseController’ placed in podcast/components (not on controllers dir) with all ‘actionXY’.

Override the ‘getViewPath()’ method and set it to for example podcast/views/podcast where you place your viewfiles.

Add a ‘setDB()’ method and switch the db in beforeAction().




class PodcastBaseController extends CController 

{

        public function getViewPath()

	{

		return $this->getModule()->getViewPath().DIRECTORY_SEPARATOR.'podcast';

	}


        public function setDB($name=null) 

	{

	       if($name===null)

                  $name=$this->getId();

  	

               Yii::app()->setComponent('db', array(

                     'connectionString'=>'mysql:host=localhost;dbname='.$name,

                ));

	} 


        public function beforeAction($action) 

	{

		$this->setDb(); //set dbname to the current controller id

	} 


        .... add all necessary actionXY .....


}




Now you can add a station by adding a controller to the podcast module which extentends PodcastBaseController with no method implemented.

Override beforeAction, if the dbname should not be the same as the controller id.




class StaaController extends PodcastBaseController

{    

       /* If the dbname is not the controller id

       public function beforeAction($action) 

	{

		$this->setDb('staadb'); 

	}  

        */

}




That should be all. You don’t have to set extra rules in the urlmanager and you have no problems with creating links with $this->createUrl(…) or Yii::app()->createUrl().

Thanks Joblo. I’ve been away for a while and just got back to this. I was starting to think along these lines, but you indicated somethings to do that I wouldn’t have thought of. So thanks again.

I will try what you suggested and get to back to all.

jk

Jablo…It works like a charm, almost :)

beforeAction($action) needs to return true/false so adding


return parent::beforeAction($action);

as the last line in the function and all is good.

Thanks again, jk