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

Can a partial have a controller ?

I cannot find an equivalent of something I used before on Sf1, the component, which is a controler and a partial view bundled as a whole (meaning of "component" differs in Sf1 and Sf2+). Let me explain :

Let suppose you have a global layout with a big side menu on the left. In order to have a clean and easy readable layout code you will use include/partial, for instance with volt :

{% include 'partials/sidemenu.volt' %}

But let suppose sidemenu is a bit complex to build, but never changes according to the context : side menu is always the same, whatever the page.

In this case, the ideal for me is to put logic into a kind of controller to build my menu and then render it into the partial. Can Phalcon do things like that ?



4.0k
Accepted
answer

Finally I managed this issue with a service which acted as a "sideMenuManager", whose role is to compute the right values to put in view thanks to dedicated methods. In the view the service with its methods is reachable by typing inside braces

this.sideMenuManager

So far I consider this issue solved, unless someone has a better answer.

Using service is good idea. this keyword in view is not needed as far as i remember.



4.0k

Indeed, after having tested it, it seems that "this" keyword can be omitted. Thanks for this information :)