Proper structure for controllers and views.

I have MapsController that uses data from Colonias model (MapsController returns a bunch of vars needed for Maps view to work). Then I render Maps view.

I also have ChartsController that uses data from Colonias model, too. It returns vars needed for Charts view to work.

I want to render Charts view inside Maps view. In order to do it I’d have to call ChartsController action from MapsController.


Yii::$app->runAction('ChartsController/Topten');

So that MapsController would be able to return vars for Maps view plus Charts view vars. Then it is possible to render Charts inside Maps.

But calling actions from other controllers is NOT a GOOD PRACTICE. Both Maps and Charts use data from Colonias model (and some other models). Both use the same view (Charts inside Maps). But they have different Controller.

I decided to have two different controllers because data is extracted and treated differently although it is from the same model.

I have read articles about GOOD PRACTICE but nothing refers to this…

How would you do it??? Would you use only One controller for everything??? Is it correct/Incorrect to have a large controller(joining Maps and Charts controller)? What it is the best way to do it in order to GOOD PRACTICE? Thanks

Hi maurocrispin21,

Probably I would have written in Colonias model almost all the code that you had written in MapsController and ChartsController. Why don’t you consider writing methods that returns the vars for the views in Colonias model instead of MapsController and ChartsController?

I am getting data through models but also by SQL commands like


$command = $connection->createCommand("SELECT colonias.nomasen AS name, count(rv.geom) AS conteo FROM colonias LEFT JOIN rv ON ST_Contains(colonias.geom, rv.geom) WHERE colonias.nomasen!='NINGUNO'  GROUP BY  colonias.nomasen ORDER BY conteo DESC LIMIT 10");

I’m using create commands because I have to do advanced selections using Postgis geometry functions. Would be possible/correct to put the above sql command on any model???

I solved my problem adding a widget. I created ChartsWidget that stores all function for charts. It has its own view. Then in my Maps view I just call Widget’s view like this


<?= ChartsWidget::widget() ?>

.

It also has the advantage that I can call it from everywhere on my app.

Yes, I think so.

A method that uses Postgis functions can be written as a static method in Colonias model, or in any other dedicated class that you put in the model directory.

That’s very nice. ::)