Home>

I am a beginner, but I am migrating from CakePHP2.5.2 to 3.6.7.

The flow is as follows. In ④, I thought that A200Controller's beforeFilter () was called
A100Controller beforeFilter () is called.
Please tell me how to resolve.

The actual flow (CakePHP 3.6.7)
①Request from browser http: // localhost/A100&id = 0001

②Common controller initialize () is called

class AppController extends Controller {
    public $components = array ('App');
    public function beforeFilter () {
        /* Common processing */
    }
    public function initialize ()
    {
        parent :: initialize ();
        $this->loadComponent ('RequestHandler', [
        'enableBeforeRedirect' =>false,
        ]);
        $this->loadComponent ('Flash');
        $this->loadComponent ('Security');
        $this->Session = $this->request->session ();
    }
}

③Common component initialize () is called
Here, redirect ('http: // localhost/A200') is called.

class AppComponent extends Component {
    public function initialize (array $config) {
        $controller = $this->_registry->getController ();
        $this->Session = $this->request->session ();
        if (/ * Judgment processing * /) {
            / * Pass here at this time * /
            / * $checkPage is 'A200' * /
            / * Store request in session * /
            return $controller->redirect ($controller->baseUrl. $checkPage);
        }
        $this->redirectToken ($controller);/* Custom processing * /
    }
}


④A100Controller beforeFilter () is called

class A100Controller extends ContactController {
    public function beforeFilter () {
        parent :: beforeFilter ();
        / * Original processing * /
    }
    public function index () {
        / * Original processing * /
    }
  • Answer # 1

    Even ifreturn $this->redirect ()within

    initialize (), subsequent processing cannot be stopped. This is becauseinitialize ()does not see the return value in a method called later in the constructor.

    Next to

    initialize ()is thebeforeFilter ()event. Ifreturn $this->redirect ()is executed in ControllerbeforeFilter (), the subsequent processing stops.

    Note that even ifreturn $this->redirect ()is executed inbeforeFilter ()of Component, the subsequent processing does not seem to stop. However, if$event->stopPropagation ();is executed beforeredirect (), the subsequent processing seems to be stopped. (AuthComponent was doing a similar process)

    So, change③Common component initialize () is calledtobeforeFilter (), beforeredirect ()I thought that I could do what I wanted to do by executing$event->stopPropagation ();.
    (beforeFilter ()is executed in the order of Component and Controller)