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

Calling View from an Action

Hi there,

I have an indexAction and I have added a line of code. With the line of code, the index view is not executed. If I take the line out, the view is called.

How can I call the view after adding code to an action. This is probably super-easy so apologies beforehand! :)

    public function indexAction()
    {
        echo $this->flash->output();
    }

If that's your line, it might have something to do with the fact you're echoing, and calling flash->output(). If you want to display your messages, do it in the View itself.



38.8k
Accepted
answer

I think you're absolutely right. In Rails, use can explicitly specify the 'render' command in an action to trigger the rendering of the associated view or nothing or various other options. Is there a comparable option in Phalcon?

Thanks.



38.8k

Awesome, thanks!

I've been following the code samples but now, I've gone a little "off piste" and have an app/view/login/new.phtml which, from a RESTful POV, should render a login form. However, calling the 'new' action on the Login controller results in no output whatsoever. I think this is because the current view mode isn't set to "Simple". Does that sound reasonable to you?

If I rename app/view/login/new.phtml to app/view/login/index.phtml and call the Login 'index' action, it renders the view? This is somewhat strange because I though the implicit hierarchical view rendering behaviour worked when you had index.phtml in the views folder like so ... app/views/index.phtml.

Bit confused. Have I missed something?

I've not dealt with hierarchical views. I've also never had to set the view mode.

When you say you "call the 'new' action", are you calling it like a method or are you visiting the url login/new? If the former, then calling the function won't change the view that gets rendered - to change that you can either view->pick() a new template file to use, or better yet, forward() the user to the "new" action, rather than just calling $this->newAction().



38.8k

Hi, no, I mean in my Welcome page, it checks for a session user. If there isn't one, it redirects to the login page:

class IndexController extends \Phalcon\Mvc\Controller
{
    public function indexAction()
    {
        if (!$this->session->has('user')) {
            return $this->response->redirect('login/new', true, 302);
        }
    }
}

Basically, it calls the new action in login controller but nothing renders. Previously, it called the 'index' action and I had the login form in an index.phtml file. This worked. I just couldn't understand why it didn't work when I tried to use 'new' (semantically creating a 'new' login/session).

So you have a file: app/view/login/new.phtml, and when you view the url login/new - nothing appears even though you have a LoginController::newAction() method? If so, I can't help - I've never had that problem.



38.8k

Yep, that's about the sum of it. To me, that should work but, I think it may be falling foul of the implicit view rendering and maybe, that's not the case with 'index'. Thanks again for your input. I appreciate it.

Hello,

I have his problem, I want pass some parameters to the view with the response method as you tried it.

return $this->response->redirect('login/new', true, 302);

But when I checked the view, the parameters not shown in it.

Could you resolve this problem?

Thanks So Much!

Hi, no, I mean in my Welcome page, it checks for a session user. If there isn't one, it redirects to the login page:

class IndexController extends \Phalcon\Mvc\Controller
{
   public function indexAction()
   {
       if (!$this->session->has('user')) {
           return $this->response->redirect('login/new', true, 302);
       }
   }
}

Basically, it calls the new action in login controller but nothing renders. Previously, it called the 'index' action and I had the login form in an index.phtml file. This worked. I just couldn't understand why it didn't work when I tried to use 'new' (semantically creating a 'new' login/session).

If you're having a problem @master355, post your own thread - please don't comment on a thread almost a year old.