How to overwrite/extend core components

hello,

I’m trying to come up with the best way to extend some core components. The goal is to put my own layer of functionality on top of the Yii framework.

Let’s say I’d create MyController interface that has to have certain functions which extends the standard CController (CModel, CForm etc… ) and all of my controllers would use this:




class SiteController extends MyController

{

  ...

}



Any ideas on this are welcome :)

–iM

Your example is perfectly fine i think, so what is the question actually? :)

As for other core components of the application: You can extend any of them and configure your own class for them in main.php. See here for a list of all components in an application:

http://www.yiiframework.com/doc/guide/basics.application

thanks Mike,

Yeah, my problem was I was trying to force Yii to read outside my protected folder.

Now it works with the following (inside protected) setup:




// in the config/main.php

return array(

...

	'import'=>array(

		'application.models.*',

		'application.components.*',

                'application.mystuff.*',

	),

...

);






// in protected/mystuff I have MyController which looks like this

class MyController extends CController

{

  ...

}






// so now I can extend the original SiteController

class SiteController extends MyController

{

  ...

}



hope it helps others, too :)

thanks again.

–iM

Hi, imehesz!

Actually, you did use on of the fundamental OOP things called inheritance. You wrote that you can force Yii to read components outside the protected folder, so I got some questions about it:

  1. What is it for?

  2. Your example is about importing classes from "mystuff" folder inside the "protected" folder (e.g. application.mystuff)…

  • it’s not really specific to controllers only. i wanna extend models, forms etc. it’s almost like a habit by now ;) the point is, I can keep my code separate in a package or a module, I can put it in a separate SVN branch or even into it’s own SVN repo and implement it in multiple apps (that use the Yii Framework)

  • I leave it in the protected folder and I also use other config files here that specific to my servers and include it in the index.php.

that’s pretty much it :)

–iM

Just a note: you can also add an alias to define some global include path where you can share components between different applications. See here for an example:

http://www.yiiframew…dpost__p__19961