We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

MVC Controller with model directly embedded

Not sure if im missing something obvious but I cant see it on the docs page for the mvc controller: https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Controller.html

In general when using the MVC Controller you use it for one entity. For example:

  • UserController
  • ArticleController
  • etc..

So a controller usually handles 1 model. What I currently do is in my base controller I have a "getEntity()" method which checks for a "getModelName()" method in which I define what model the current controller is associated with.

That way in the getEntity() method I can then load the associated model based on the url "id" attribute. So inside any action in my controller I can use getEntity() to get the model I currently want to work with.

I want to move this down into the zephir MVC controller, anyone else interested in having this? Am I missing anything obvious as to why the MCV controller should not have this?



43.9k

Hi,

that's interesting, and to be honnest, I've already done something similar.

Am I missing anything obvious as to why the MCV controller should not have this

it's too general imho: sometimes you need more in a controller that just basic crud actions

I want to move this down into the zephir MVC controller

does this mean: "I want to code in zephir this all model crud controller" ? If yes, that's a pretty good challenge !

Thanks for the reply.

Yea I already have code ready for zephir for this. Im just checking if there is enough interest to put in a pull request.

In regards to it being too general: You can still override the function manually or completely ignore it if you dont want to use it.

Just out of interest does your implementation also assign the model through the "id" in the route? Im thinking maybe theres even a possiblity to do somethin similar to laravel where they bind the model in the router.

edited Jan '16

Do a PR Request with it. I thought i saw a issue with it, you created it i guess so do pr with implementation, there are still now 2.1.x so maybe you will do it before release.

Jurigag I did open an issue for route model binding which ended up being solved and I have updated the documentation too.

https://mavon.ie/article/phalcon-php-route-model-binding

With that in mind now I am not sure if there is still a need for the "controller" holding onto the entity?