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

Dispatch loop, initialize, and forwarding actions

I am a little confused on something. I have a base controller that has an initialize method, setting the title, and template, and a beforeExecuteRoute method that checks access.

In the beforeExecuteRoute access check, if access is not allowed, a flash is set and the request is forwarded to IndexController / indexAction.

In general, this works as expected. For example, if I am not logged in and browse to /blog/edit/1, beforeExecuteRoute is called and I am forwarded to index/index. The page title is set via initialize() and we are good. However, if I were to browse to /index/protected, beforeExecuteRoute is called and I am forwarded to index/index, but initialize() is NOT called. A forward to the same controller seems to bypass the intialize() call. Is this the expected behaviour? It wasn't quite what I expected. I assumed initialize would have been called.

So, in a more concise verbage:

Controller A to Controller B

  1. request restricted page /blog/edit/1
  2. beforeExecuteRoute fires and a flash is set. Forward to index/index
  3. beforeExecuteRoute fires again (new dispatch cycle), no permission issues, continue
  4. initialize() called in base controller setting title and template.
  5. works

Controller A to Controller A

  1. request restricted page /index/protected
  2. beforeExecuteRoute fires and a flash is set. Forward to index/index
  3. beforeExecuteRoute fires again (new dispatch cycle), no permission issues, continue
  4. initialize() is NOT called. no page title, no template.
  5. index/index displayed with no layout.

So what's the difference? I am not 100% clear on the dispatching and when objects are created and methods called.



98.9k
Accepted
answer
edited Sep '14

Take a look at the Dispatcher component in Phalcon 2: https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/dispatcher.zep



1.2k

Ok, that's good to know. Thanks for pointing that out