Sharing An Controller Between 2 Module

Hi,

How to share an controller between several module?

For example:

There is a common ProfileController between user and admin module. how can be written the ProfileController so that each of modules uses this controller without repeating the code(reusability)?

Hi,

The best way is to make a component (like Controller) put the common code and extends this controller to module controllers

Is pleasant in each module exist an controller with the same nameb?

My mainly doubt is, existence two class with the same name in an application! If a time i want to make a class that extends from user/ProfileController or admin/ ProfileController, how i declare which of these classes will be used!?

it is good to avoid it! If no more than one modules run or loaded at once, yes you could make it but be careful with configuration. You cannot import at once controllers or components or models… from two modules. so you have to find a way to import these on the fly

Master Controller with Dynamic Actions

Hi

I don’t know if this is relevant to your question, but all my models use a single master controller containing dynamic actions. With dynamic actions, I mean this:

All our normal CRUD actions do more-or-less the same thing and they don’t use much of the model’s info. For example: for a single table, the Create action would probably need the model-name and maybe the PK-name. But that is about all. The rest is done by the model, not the action.

So if you store the model-names and PK-names of all your tables in a separate meta-data-table, then you can re-use a single dynamic action to create all your models for all your tables.

The actions don’t use your modle’s field names directly, but retrieve them from the meta-data-table:

$idName = $meta_data_model->name_of_table_id;

$model->$idName = 5;

All my controllers are almost empty. They just have enough data to know what meta-data-table records to read. Then, all my controllers extend from the master controller - which contains all the dynamic actions.

All my actions in this master controller can handle ‘single table’, ‘one-to-many’, ‘many-to-many’, and ‘tree’ db structures.

Okay, my master controller has 2000 lines, but if you change one thing, then it works for your entire application. You don’t have to make the changes in all your other crud controllers as well. And you have zero duplicate code in your controllers.

But such a huge controller could load unused actions/functions as well. So an even better idea might be to store these dynamic actions in separate files/widgets/helpers. Then in your controllers, you only import/include the actions you need. But still, if any of these dynamic actions are updated, then they will be updated immediately in all controllers.

Controllers can obviously have their own unique actions as well. But such actions should be very few, because they will quickly become dynamic actions when you need to re-use them in other controllers as well.

This is only an assumption:(don’t perform in act)

I only want know if exist two class with the same name in an application, and we want to make a new class that extends from one of the two class, how should we distinguish between two class?

Excuseme for my bad English.

You could use namespaces

relative path a/AController.php


namespace ver1;

class AController {


public function test(){

	echo 'hi controller a / ControllerA';

}


}

relative path b/AController.php


namespace ver2;

class AController {


public function test(){

	echo 'hi controller b / ControllerA';

}


}

a test.php file


require 'a/AController.php';

require 'b/AController.php';


$obj1 = new ver1\AController();

$obj2 = new ver2\AController();


$obj1->test();

$obj2->test();


class A1Controller extends ver1\AController {

}

class A2Controller extends ver2\AController {

}


$obj3 = new A1Controller();

$obj4 = new A2Controller();


$obj3->test();

$obj4->test();

php version 5.3.0 or above,

the above example-code works fine, so with appropriate changes will be work in Yii :)

Very thanks dear friend, helped you much, i found my answer. Know out of curiosity i want know in versions prior to 5.3 what’s the solution

And existence of several class with the same name in the application is not an issue. OK?

before php version 5.3

I think you can’t load in one request, two or more same name classes (namespaces included in ver 5.3 or above)

if extends the class like that "ver1\AController" there is no problem, but you have to check in fact.

Also I don’t know how Gii manipulates these classes (espcecially if you want to have models with the name same for crud task). May be in Yii2 it will have a proper manipulating

Thanks for your votes :)

:) However thanks, and again, votes are minimal things :)