Thank you. So I keep that 1 line in the controller :)
$this->view->->setRenderLevel( \Phalcon\Mvc\View::LEVEL_ACTION_VIEW );
We could argue about your approach and MVC here...
Actually, that could be interesting and useful to me (and perhaps for others who happen to read it). So I know it could go off topic, but if you don't mind the effort, please do say your argument.
My idea: There 2 developers: one frontend, and one backend. And some info needs to be collected from the user, via a web form...
Backend person knows what info he needs from a form, so he creates the form, he passes it to the view, and he tells the frontent person to display it and send it back to a given URL with a POST request. So he writes the following in the controller (simplified):
<?php
// the form
$form = new MyForm();
$this->view->form = $form;
// the form was submitted
if($this->request->isPost()) {
// valid or not
$is_valid = $form->isValid( $this->request->getPos() );
// tell the view that is was submitted, valid or not
$this->view->form_submitted = true;
$this->view->form_isvalid = $is_valid
if($is_valid) {
// save data, send email, etc
}
}
Frontend person works with the view files, so he displays the form. When the form was submitted, he needs a simple JSON response from the server. So at /app/view/controllername/actionname.phtml file he could write this:
// form response
if($this->form_submitted) {
if($form_isvalid) echo '{success:true};
else echo '{success:false'};
// and stop here, do not display the /app/view/layout/controllername.phtml, nor the /app/view/index.html
// display the form and the entire webpage
} else {
// display $form here, submitting with AJAX.
// let the hierarchy go on, so the full page will be displayed
}
And when the frontend person changes the way the form behaves on the frontend, he can implement it by himself. Such as the form will no longer submit with AJAX, but submit "normally" so he does need the full page displayed again when the form was submitted (so he wants all the levels of the hierarchy). Or submit to an <iframe> that will respond with some javascript code and partial HTML.
So idea is that the frontend person doesn't need to write things into the controller. He can just deal with the view, in the view files. And the backend person doesn't need to know anything about the view files. So code for the Controller and View are separated.