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

White page when forwarding

Hi. i have those actions in my controller:

    public function newAction(){

        $imp_id = $this->request->getPost('importid');
        $imp_from =  $this->request->getPost('import_from');

        if(!$imp_id)
        return $this->dispatcher->forward(['namespace' => 'Controllers\Admin', 'controller' => 'titles', 'action' => 'import']);

        $this->view->titleform = new Create();
    }

    public function importAction(){
    }

They both work fine when Router calls them but when I do "$this->dispatcher->forward" it shows a white page. if I put an echo inside "importAction" is shows that content (but only that). (Basicly it is ignoring the view).

Do you have a view file for import action?

edited Jan '18

Do you have a view file for import action?

I do. As I said

$router->add('/admin/titles/new', [
    'namespace'  => 'Controllers\Admin',
    'controller' => 'titles',
    'action'     => 'new'
])->setName('admin.titles.new');

$router->add('/admin/titles/import', [
    'namespace'  => 'Controllers\Admin',
    'controller' => 'titles',
    'action'     => 'import'
])->setName('admin.titles.import');

when using "/admin/titles/import" it works fine.

edited Jan '18

Actions should either return Phalcon\Mvc\Response object, void or a scalar (in regular MVC pattern).

Try it like so:

 public function newAction(){
        $imp_id = $this->request->getPost('importid');
        $imp_from =  $this->request->getPost('import_from');
        if(!$imp_id)
            $this->dispatcher->forward(['namespace' => 'Controllers\Admin', 'controller' => 'titles', 'action' => 'import']);
        return;
        $this->view->titleform = new Create();
    }
edited Jan '18

Actions should either return Phalcon\Mvc\Response object, void or a scalar (in regular MVC pattern).

Try it like so:

public function newAction(){
       $imp_id = $this->request->getPost('importid');
       $imp_from =  $this->request->getPost('import_from');
       if(!$imp_id)
          $this->dispatcher->forward(['namespace' => 'Controllers\Admin', 'controller' => 'titles', 'action' => 'import']);
      return;
       $this->view->titleform = new Create();
   }

I've tried that before and it didn't work. I tried to use $this->view->pick(...) but no sucess. Also tried to rename that action.

Okay. I think I found the problem. I have this on my baseController

$this->view->setViewsDir($this->view->getViewsDir() . 'admin/');

After $this->dispatcher->forward, $this->view->getViewsDir() returns "admin/" two times.



7.3k
Accepted
answer

Solved:

class baseController extends \Phalcon\Mvc\Controller
{

    public function initialize()
    {   
        //$this->view->setViewsDir($this->view->getViewsDir() . 'admin/');  => moved to onConstruct().        
    }

    public function onConstruct()
    {
        $this->view->setViewsDir($this->view->getViewsDir() . 'admin/'); 
    }

}