Spoiler: I'd like to better understand the lifecycle of a request to a CakePHP app.
Background: I'm on CakePHP v2.3. I'm debugging a problem that results in an undesired redirect which I can't find. I'm trying to work my way through the lifecycle of the request by incrementally adding die()
to try to identify where the redirection is getting triggered.
I hit a dead end because I can kill the execution from inside the controller's beforeFilter(), but the redirect happens if I move the die()
into the actual action.
So my specific question is: what happens after the beforeFilter but before the action? I know of beforeRender(), which had no effect when I placed the die()
there.
My more general / better question is: is there documentation for the full lifecycle of a CakePHP request?
An answer to either of these would be great.
Update
Thanks to Mathew F.'s helpful suggestions, I'm focusing my attention on the Auth component because it's almost the only candidate and the redirect I'm debugging looks like it's handiwork (the user arrives at the authRedirect location). However, when I trie to die()
at the top of the AppController's isAuthorized()
nothing happens. And my controller has no isAuthorized()
of its own. So this leaves me a bit stumped again.
I hit a dead end because I can kill the execution from inside the controller's beforeFilter(), but the redirect happens if I move the die() into the actual action.
That is a big clue that the redirect was performed by a component.
beforeFilter()
is called before anything is configured for the request. This includes the controller and it's components. Components are initialized before a controller's action is called. So it's possible for a component to redirect (i.e. the AuthComponent does this).
So my specific question is: what happens after the beforeFilter but before the action? I know of beforeRender(), which had no effect when I placed the die() there.
The request is first processed by the dispatcher. Where it's routed to a controller's action. That controller is then instantiated and all of it's components are then instantiated. For each component their initialize()
method is called. After that the controller's beforeFilter()
method is called. Followed by all the components startup()
method. The controller's action is then called.
http://book.cakephp.org/2.0/en/controllers/components.html#component-api
You can try excluding components to find the one causing the trouble. Another option is to add echo "hello"
to your index.php
in the webroot. This will force a header can not be modified
error at the spot the redirect is being sent.
Good question! Taken from the book:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With