Skip to content
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

[2.1.x] Dispatcher doesn't forward when handling exception in PHP5.6 #11819

Closed
virgofx opened this issue May 23, 2016 · 6 comments
Closed

[2.1.x] Dispatcher doesn't forward when handling exception in PHP5.6 #11819

virgofx opened this issue May 23, 2016 · 6 comments
Milestone

Comments

@virgofx
Copy link
Contributor

virgofx commented May 23, 2016

Code works in 2.0.x. It may have to do with the creation of the new _dispatch() method in 2.1.x and multiple levels of try/catch which do not work well in zephir.

Pseudo code:

bootstrap

$di = new \Phalcon\Di\FactoryDefault();

$eventsManager = new \Phalcon\Events\Manager();
$eventsManager->attach('dispatch:beforeException', function($event, $dispatcher, $exception) {
    $dispatcher->forward([
        'controller' => 'test',
        'action' => 'second'
    ]);
    // Prevent the exception from bubbling
    return false;
});

$dispatcher = new \Phalcon\Mvc\Dispatcher();
$dispatcher->setEventsManager($eventsManager);

$di->setShared('dispatcher', $dispatcher);

$application = new \Phalcon\Mvc\Application();
$application->setEventsManager(new \Phalcon\Events\Manager());
$application->setDI($di);

echo $application->handle()->getContent();

TestController.php

class TestController extends \Phalcon\Mvc\Controller
{
    public function indexAction()
    {
        throw new \Exception('whups bad controller');
    }

    public function secondAction() 
    {
        echo 'I should be displayed';
        exit;
    }
}
@sergeyklay sergeyklay added this to the 2.1.0 milestone Jun 16, 2016
@andresgutierrez andresgutierrez modified the milestones: 2.1.1, 2.1.0 Jun 27, 2016
@sergeyklay sergeyklay modified the milestones: 3.0.1, 3.0.2 Aug 10, 2016
@virgofx virgofx changed the title [2.1.x] Dispatcher doesn't forward when handling exception [2.1.x] Dispatcher doesn't forward when handling exception in PHP5.6 Aug 26, 2016
virgofx pushed a commit to virgofx/cphalcon that referenced this issue Sep 9, 2016
exceptions, and improved documentation/test-suite for dispatcher
components [phalcon#12154](phalcon#12154),
[phalcon#11819](phalcon#11819)
@temuri416
Copy link
Contributor

Hi @virgofx, is there any progress on this issue?

Thanks!

@virgofx
Copy link
Contributor Author

virgofx commented Nov 22, 2016

@temuri416 We have it fixed; however, still waiting on a core bug in Zephir land which causes a segfault in the updated tests that would allow this to pass and get things merged in. So still in a holding pattern. Anxiously waiting help from @andresgutierrez to help with zephir-lang/zephir#1325. Perhaps if you want to add another ping in that thread -- might help. In the meantime, I've had to override the dispatcher manually which fixes this in our production boxes - If you need this snippet email me directly and I can share with you.

@temuri416
Copy link
Contributor

@andresgutierrez Can we please have your attention? We need your help to get the issue fixed. Thank you!

@sergeyklay sergeyklay modified the milestones: 3.0.2, 3.0.3 Nov 26, 2016
@sergeyklay sergeyklay modified the milestones: 3.0.3, 3.0.4 Dec 23, 2016
@sergeyklay sergeyklay modified the milestones: 3.0.4, 3.1.0 Feb 12, 2017
@sergeyklay sergeyklay modified the milestones: 3.0.4, 3.1.0, 3.1.x Feb 12, 2017
sergeyklay added a commit that referenced this issue Apr 8, 2017
@sergeyklay
Copy link
Contributor

Fixed in the 3.2.x branch.

@sergeyklay sergeyklay modified the milestones: 3.2.x, 3.2.0 Apr 8, 2017
@temuri416
Copy link
Contributor

Verified, working as expected! This is an enormous improvement, thanks a lot.

@mladen7
Copy link

mladen7 commented Feb 10, 2019

A bit late to the party but having the same problem at the moment... Using php 5.6.10 and phalcon 3.2.4 version, i cannot forward either from the beforeException or the beforeExecuteRoute methods.. When i try to forward from the beforeExecuteRoute, i trigger a dispatch cycle loop eventually being picked up by the beforeException, which then also does not manage to forward but returns the index page.. I used the basic INVO tutorial available on the phalcon docs website.. Relevant code follows:
bootstrap

    $eventsManager = new EventsManager();
    // Listen for events produced in the dispatcher using the Security plugin
    $eventsManager->attach(
        'dispatch:beforeExecuteRoute',
        new \Plugins\SecurityPlugin()
    );

    // Handle exceptions and not-found exceptions
    $eventsManager->attach(
        'dispatch:beforeException',
        new \Plugins\NotFoundPlugin()
    );

    $dispatcher = new Dispatcher();

    // Assign the events manager to the dispatcher

    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
}, true);

not found plugin

public function beforeException($event, Dispatcher $dispatcher, $exception) {
        switch ($exception->getCode()) {
            case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
            case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                $dispatcher->forward(
                    array(
                        'namespace'  => 'Controllers',
                        'controller' => 'GenericError',
                        'action'     => 'handleRoute404'
                    )
                );
                return false;
            default:
                $dispatcher->forward(
                    array(
                        'namespace'  => 'Controllers',
                        'controller' => 'GenericError',
                        'action'     => 'handleError'
                    )
                );
                return false;
        }
    }

security plugin

// Authorize
        $roleFromToken = $this->authorize();
        if (is_null($roleFromToken)) {
            $dispatcher->forward(
                [
                    'namespace'  => 'Controllers',
                    'controller' => 'GenericError',
                    'action'     => 'handle401',
                    
                ]
            );
            return false; }

any ideas will be greatly appreciated. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants