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

Redirect Problem with Simple View

I'm kinda new to phalcon and like it so far but i got some problems.

I use SimpleView and when i use it i cant response->redirect .. it gives me an error like this

Call to undefined method Phalcon\Mvc\View\Simple::disable()

Well it kinda make sense because SimpleView does not have disable() method when i check its api document , i can see that.The problem is why application try to run a method that simpleView does not have.

edited May '15

There is a little bug in Phalcon 2.0.0, trying to reference a method that does not exist again in View class. This method existed in version 1.3.4 but somehow was removed.

let dependencyInjector = this->getDI();

if !header {
let url = <UrlInterface> dependencyInjector->getShared("url"),
header = url->get(location);
}

if dependencyInjector->has("view") {
let view = <ViewInterface> dependencyInjector->getShared("view");
view->disable();
}

Whoever has removed the method obviously did not consider that there is a call to it when redirect is called if the view is passed to DI(Dependency Injection). If this is not considered a bug then Phalcon team has decided that we shouldn't inject the view into our application if we would ever call return $this->response->redirect(). The call to redirect checks if you injected a view and trys to call disable view. This throws an error because there is no more method called disable in View class for Phalcon 2.0.0.

Here is how to resolve this problem. Remove the view you injected via the services script (services.php). Go to your ControllerBase.php and add your view as you would use it normally.


use Phalcon\Mvc\Controller,
Phalcon\Mvc\Dispatcher,
Phalcon\Mvc\View\Simple as SimpleView,

class ControllerBase extends Controller
{
public $view;
protected $di;

public function initialize()
{
$this->di = $this->getDI();

$this->view = new SimpleView();
$this->view->setViewsDir($this->di->get('config')->application->viewsDir); //this is to set your view directory
}

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

/**
* Execute before the router so we can determine if this is a provate controller, and must be authenticated, or a
* public controller that is open to all.
*
* @param Dispatcher $dispatcher
* @return boolean
*/
public function beforeExecuteRoute(Dispatcher $dispatcher)
{

}
}

You can then use $this->view in your controllers.

class AccountsController extends ControllerBase
{ 
public function loginAction()
{
$config = $this->di->get('config'); 

$config->api->google->state = $this->session->getId();
$this->view->setVar('googleLogin', $config->api->google);

$this->view->setVar( "title", "Account Login" );

echo $this->view->render('admin/login');

}

What we have done is to bypass the check that trys to call method disable() that was not implemented in the class View and being called when calling the redirect method.

Hope this makes you happy. :-)