-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Action forwarding is not working in 'dispatch:beforeException'. #2851
Comments
More info on the subject.
Item 2 above is especially problematic, since it makes impossible throwing exceptions in controller plugins (ACL is a good example). |
I'm currently facing the same issue. But in the case of the ACL, you can simply launch the forward action: if (!$logged) {
$dispatcher->forward(array('namespace' => 'MyNamespace', 'controller' => 'error', 'action' => 'forbidden'));
return false;
} |
Means that if you throw an exception in the dispatcher or in the controller's action, you will be able to handle the exception in 'beforeException'
These exceptions won't go through 'beforeException'. You must use a try/catch in your function. Say: public function afterDispatch(Event $event, Dispatcher $dispatcher)
{
try
{
// do something
return true; // or false to stop execution
}
catch (\Exception $exception)
{
// handle the exception (do a forward...)
return false;
}
} By the way, you helped me handling the issue: I'm using a Also don't forget to |
What I've found is that exceptions thrown in "beforeDispatchLoop" are indeed caught by "beforeException", but the dispatcher's internal redirect no longer works. |
Could you please try again with 2.0.0? |
In the Invo app, if we print the NotFoundPlugin Exception caused by SecurityPlugin's beforeDispatch function, we get : Does that mean that we can't access the errors/show401 page ? Apparently, you forgot to declare it in the $publicResources array, so I did it and the problem is still the same (same Exception, same message) |
I've finally upgraded my project to the latest Phalcon 3.x. The issue is still there. I was debugging INVO again, and whenever execution lands in So, again - my question is as follows: Can I expect the following code to forward action to ErrorsController: public function beforeException(Event $event, MvcDispatcher $dispatcher, Exception $exception)
{
$dispatcher->forward(array(
'controller' => 'errors',
'action' => 'show500'
));
return false;
} The way it works now - dispatcher exits the execution loop and action is not forwarded to ErrorsController. |
I think this issue is already opened somewhere. @virgofx was planning to fix this. The issue is super obvious - https://github.com/phalcon/cphalcon/blob/master/phalcon/dispatcher.zep#L343 There is exception is catched, but actual dispatch loop where forward could work is happening here https://github.com/phalcon/cphalcon/blob/master/phalcon/dispatcher.zep#L390 This is why you can't forward from beforeException. |
@temuri416 Did you try something like this |
But your code @sergeyklay won't work too. Check above. Catch exception and handling it is happened outside dispatch loop. |
I checked it right now |
Looking at that code I don't see any difference from what I pasted above. In both cases a boolean is returned. Looking at |
|
the problem is - how do you use it |
@temuri416 This is a problem. There is a more recent issue - #11819 - So new follow up should reference this issue. It's tentatively fixed and in a holding pattern waiting for a core bug in Zephir to be fixed before it will be merged. Hopefully very soon. Also, any exception thrown in the beforeDispatchLoop does get thrown with the fix, which was a mistep with a lot of previous checkins on Dispatcher. Documentation is already ready to be submitted and will be updated as well. |
You mean this issue i created - zephir-lang/zephir#1336 ? I guess it's possibly affecting code like:
too |
thats a separate (but closely related) issue, not related to the issue with the dispatcher fixes. More info can be found here- zephir-lang/zephir#1325 |
Anyway this problem form this issue actually is not related to those issues - just current code is not allowing to forward from beforeDispatchLoop or afterDispatchLoop, no bug or something. Just forward can be done only from dispatch loop, obviously before or after it you can't do it. |
Actually not fixed, just new feature to forward from beforeDispatchLoop and afterDispatchLoop :D also update tests @virgofx because i latest checked tests are failing in your repo with those changes for old unit-tests folder |
Will check juri. Last I ran, everything was working, 100% on the dispatcher was working. The old dispatcher tests were removed because they're weren't accurate. |
Oh then it's possible i run tests in wrong repo then.... my mistake if i did this |
Hi all, I've looked closely at the execution workflow and quite honestly, don't see how to fix this without refactoring dispatch method. The only thing that comes to mind is to enclose every event fire in its own try / catch block similar to https://github.com/phalcon/cphalcon/blob/master/phalcon/dispatcher.zep#L341. Is this the intended approach? Thanks! |
Do you understand there is NO BUG ? Forward can happen only from DISPATCH LOOP, like from WHILE, you currently trying to forward outside of dispatch loop which obviously don't work. This is not bug, this is NFR, create seperated issue. The other issue is about zephir and some other exception related stuff, but only about forward and why it's not working it's not related to this. |
Yes, I see this is not a bug.
Yes, right now that's the way it is. But there's no reason it could not happen from anywhere inside https://github.com/phalcon/cphalcon/blob/master/phalcon/dispatcher.zep#L390 loop. |
But doing forward from event works fine. The problem which there is currently is throwing exception from event - this doesn't work currently with php 5.6, on php 7 there are some other bugs which can occur. And we are talking mostly about this with @virgofx Forward should work totally fine, i'm even using it extensively and there are no any problem. |
Hi all,
It is related to this: http://docs.phalconphp.com/en/latest/reference/dispatching.html#handling-not-found-exceptions. Whenever an exception is thrown, action forwarding to Error controller does not work and results in a blank page.
Steps to repro:
Exception "Just because" is thrown and caught in 'dispatch:beforeException'. However, forwarding to /error/index does not work. Execution terminates immediately after "return false". I am using PHPEd debugger to trace it.
Also,
Can Phalcon guys explain the following cryptic sentence from the docs:
What does that really mean??
Thanks!
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: