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

forward

<?php

use Phalcon\Mvc\Controller;

class ControllerBase extends Controller { static $redis = null; public $number;

public function initialize()
{
    if(!$this->cookies->has('number'))
    {
        $number = uniqid();
        $this->cookies->set('number',$number,time()+30*86400);
    }

    $number = $this->cookies->get('number')->getValue();

    if(!$this->getRedisInstance()->exists($number.'_address'))
    {
        $this->dispatcher->forward(array(
            'controller'    =>  'school',
            'action'        =>  'select'
        ));
    }

    $this->number = $number;

    if(method_exists($this, '_init'))
        $this->_init();
}

static public function getRedisInstance ()
{
    if(!self::$redis)
    {
        $frontCache = new \Phalcon\Cache\Frontend\Data(array(
            'lifetime'  =>  2678400,
        ));

        $cache = new Phalcon\Cache\Backend\Redis($frontCache,array(
            'host'          =>  'localhost',
            'port'          =>  6379,
            // 'auth'           =>  'foobared',
            'persistent'    =>  false,
        ));

        self::$redis = $cache;
    }

    return self::$redis;
}

}

?>

Forward why not jump to the school controller? Instead, the default Index controller jumps



11.0k

try this

use Phalcon\Mvc\Dispatcher;

public function initialize()
{
    if(!$this->cookies->has('number'))
    {
        $number = uniqid();
        $this->cookies->set('number',$number,time()+30*86400);
    }

    $number = $this->cookies->get('number')->getValue();

    if(!$this->getRedisInstance()->exists($number.'_address'))
    {
       $dispatcher = new Dispatcher;
        $dispatcher->forward(array(
            'controller'  =>   'school',
            'action'      =>   'select'
        ));
    }

    $this->number = $number;

    if(method_exists($this, '_init'))
        $this->_init();
}

static public function getRedisInstance ()
{
    if(!self::$redis)
    {
        $frontCache = new \Phalcon\Cache\Frontend\Data(array(
            'lifetime'    =>   2678400,
        ));

        $cache = new Phalcon\Cache\Backend\Redis($frontCache,array(
            'host'            =>   'localhost',
            'port'            =>   6379,
            // 'auth'         =>   'foobared',
            'persistent'  =>   false,
        ));

        self::$redis = $cache;
    }

    return self::$redis;
}

try this

use Phalcon\Mvc\Dispatcher;

public function initialize()
{
   if(!$this->cookies->has('number'))
   {
       $number = uniqid();
       $this->cookies->set('number',$number,time()+30*86400);
   }

   $number = $this->cookies->get('number')->getValue();

   if(!$this->getRedisInstance()->exists($number.'_address'))
   {
     $dispatcher = new Dispatcher;
       $dispatcher->forward(array(
           'controller'  =>   'school',
           'action'      =>   'select'
       ));
   }

   $this->number = $number;

   if(method_exists($this, '_init'))
       $this->_init();
}

static public function getRedisInstance ()
{
   if(!self::$redis)
   {
       $frontCache = new \Phalcon\Cache\Frontend\Data(array(
           'lifetime'    =>   2678400,
       ));

       $cache = new Phalcon\Cache\Backend\Redis($frontCache,array(
           'host'            =>   'localhost',
           'port'            =>   6379,
           // 'auth'         =>   'foobared',
           'persistent'  =>   false,
       ));

       self::$redis = $cache;
   }

   return self::$redis;
}

Still, I can do this in other controllers, but I can't forward it in the base class.



11.0k

in other controller extend from base controller ? can you try this function in controllerbase and call it in other controller

public function test(){
return $this->dispatcher->forward(array(
            'controller'  =>   'school',
            'action'      =>   'select'
        ));
}

if it can redirect fill return before call dispacther

in other controller extend from base controller ? can you try this function in controllerbase and call it in other controller

public function test(){
return $this->dispatcher->forward(array(
           'controller'  =>   'school',
           'action'      =>   'select'
       ));
}

if it can redirect fill return before call dispacther Well, I found that it was not in the initialize method, and the cookie is no way to set the problem is very strange -