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

How can I call another controller/action/params from inside a view?

Hello guys. I have a model AA and another one BB.

From the page https://localhost/AA/view/333, a want to "call" https://localhost/BB/foo/i several times, but not using partials. I want the return from controller BB, method foo, param i.

Is it possible?

I shearched all documentation and couldnt find anything similar =/.

Just for information, I could retrieve BB/foo html content from inside controller AA/view. I just dont know how.

edited Jul '14

+1

It would be cool to see such kind of tools in arsenal.

Why do we need this?

Sometimes (almost always) developer needs to render complex logic in some area in layout (view) and it requires some sort of requests to db and session storage, do some business logic, prepare params for view and render view with this params.

Say we have volt function named component('foo/bar'); This call will invoke FooController::barAction (or FooController::barComponent for clarity).

Here is example of what I mean:

//volt
 {{ component('robots/cool', params) }}
//php, controller
$robots = Robots::findCoolRobost() //do request to db, or session storage, or file
//do business logic
//specify view params
$this->pick('cool_robots'); //render view

Here is some examples of common tasks for such feature:

{{component('user/availableActions', user)}}
{{component('product/similar', product)}}
{{component('product/popular', category)}}
{{component('product/similar', product)}}
{{component('page/submenu', page)}}
...

Is this called HMVC?



40.8k
Accepted
answer

Yes it is HMVC, you could find some information about it on this forum. https://forum.phalcon.io/discussion/6/hmvc-pattern

there is HMVC bootstrap for phalcon on github. https://github.com/phalcon/mvc/blob/master/hmvc/public/index.php

I was upset yesterday... But thanks for the answers. That sounds perfect. I'll try later.