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

Namespace Model access in volt

I think not yet @baychae but you can inject a component in DI and then use it on volt

Can you do that in Twig? Other way use Twig

Good luck



8.2k
Accepted
answer

You should be doing all your logic inside the controller. The view should only be used to output the data returned from your controller.

class Controller {
    public function viewAction()  {
        $this->view->records = Model::find();
    }
}

and in your view

{% foreach record in records %}
    {{ record.id }}
{% endforeach %}

You will find it to be really hard to update your code if you start doing everything inside the view. Especially when you start splitting your views up into components.



47.7k

I did indeed end up doing this.

Using model fetch in the view can easily lead to a poorly performing view without discipline.

I increased performance by moving business logic to the Model and fetch and transform to the Controller.

You should be doing all your logic inside the controller. The view should only be used to output the data returned from your controller.

class Controller {
   public function viewAction()  {
      $this->view->records = Model::find();
  }
}

and in your view

{% foreach record in records %}
  {{ record.id }}
{% endforeach %}

You will find it to be really hard to update your code if you start doing everything inside the view. Especially when you start splitting your views up into components.