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

Phalcon Component using a Custom View (Volt) - how to create Phalcon UI Widgets

On my website i have to write a number of custom UI components and stays independently to the Controllers and Models, to generate views (In common some people call them UI-widgets).

i.e Creating re-usable "site-search" widget.

Currently, my ideal approach is, I create an extended Component under the "Phalcon\Mvc\User\Component"

use Phalcon\Mvc\User\Component as Component; class SiteSearch extends Component {

function doSomething (){ //how to call a custom view for this component? } }

However, I am confused about how we can integrate a view for this extended "Component"? Any idea or different approach we can use?

You can't really - Views are tied to controllers.

You could encapsulate each Widget's UI in a separate partial, then only include the particular partials you want on each page.

To handle each widget (both setup and processing form data), you could then do that work in each Action. That's a lot of duplication though, so I'd look into hooking your components into dispatcher events to have them run on every page load: https://docs.phalcon.io/3.4/en/dispatcher#dispatch-loop-events. Then you don't have to set them up in each Action.


An alternative is to use Ajax and a Micro API.