Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

struggling with views

Hello,

I've been trying to understand certain things around Phalcon for a website I'm building. Whereas the general notion of MVC is clear, and I understand how routing, controllers and ORM models work, I'm puzzled by views. I did manage to create my index.phtml under app/views/ which I want to be shown in every single page. Now my register view and controller is shown hierarchicaly:

  • first the index stuff, then the register form.

Here's where im puzzled/stuck:

If registration fails, I catch the error in function public signupAction inside class RegisterController. I was intending to use the flash mechanism, but want to know:

  1. can I format the output of the view displaying /register/fail
  2. can I format the output of the view displaying register/success

ATM, I flash those messages, and if I want them formated, my only option seems to write actual html inside the flash message.

I know that the hierarchy for views exists, I just don't get it. Where should the view for those scenarios be? What should it be named? How would it catch the message and display it (inside the view)?



2.4k
Accepted
answer

Hello,

I think you should take a look at the flash documentation: https://docs.phalconphp.com/en/latest/reference/flash.html

Inside your services definition, you should define the FlashDirect to display as you want. And of course on your view you should have:

app/views/register/fail.phtml


<h3>Oh nooo, fails !</h3>
<?php echo $this->getContent(); ?>//Exactly where you want to show the flash messages. 

To change views depending on what happened on controller, I usually use the picking:

https://docs.phalconphp.com/en/latest/reference/views.html#picking-views

You could do, if fails:

use Phalcon\Mvc\Controller;

class RegisterController extends Controller
{
    public function registerAction()
    {
        $this->flash->error("Hey, something bad happened");
        //Failed !
        $this->view->pick("register/fail");
    }
}

Hope it helps,

Reggards, Henrique Otavio.

It actually makes perfect sense and really helps! Many thanks Henrique!!!