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

No exceptions are thrown for Non-existing Actions on Phalcon 4

Hi there, On our project, we have recently upgraded phalcon version from 2.0.8 to 4.0.6. Currently, I am not getting any Exceptions for non existing Actions in Controller.. Although I am getting Exceptions for non-existing Controllers as expected. Can anyone assist me on this issue?

Thank you.



978

Hi, don't know if you still have the problem or if it can help.

I use those on my public/index.php

try { 
    // ...
    // doing some stuff.
    // ...

    $application->handle($_SERVER["REQUEST_URI"])->send();
} catch (\Exception $e) {
    echo '<h1>Exception</h1>';
    echo '<pre>' . $e->getMessage() . '</pre>';
    echo '<pre>' . $e->getTraceAsString() . '</pre>';
}

And it throw exception when controller or action are wrong.

Hi Yann, Thanks for your reply. Unfortunately it doesn't work. I get exception when controller is wrong. But the problem is with the actions.

Hi, don't know if you still have the problem or if it can help.

I use those on my public/index.php

try { 
   // ...
  // doing some stuff.
  // ...

   $application->handle($_SERVER["REQUEST_URI"])->send();
} catch (\Exception $e) {
   echo '<h1>Exception</h1>';
   echo '<pre>' . $e->getMessage() . '</pre>';
   echo '<pre>' . $e->getTraceAsString() . '</pre>';
}

And it throw exception when controller or action are wrong.

Use beforeException with the EventManager, it works fine for me
https://docs.phalcon.io/4.0/en/dispatcher

Hi, I am already using beforeException in my code. It perfectly works for both wrong Controllers and actions in 2.0.8 version. But after upgrading to 4.0.6 version, there's no exception for wrong Actions.

$di->set('dispatcher', function () {
    //Create/Get an EventManager
    $eventsManager = new Manager();
    //Attach a listener
    $eventsManager->attach("dispatch", function ($event, $dispatcher, $exception) {
        //controller or action doesn't exist
        if ($event->getType() == 'beforeException') {
            switch ($exception->getCode()) {
                case \Phalcon\Dispatcher\Exception::EXCEPTION_HANDLER_NOT_FOUND:
                case \Phalcon\Dispatcher\Exception::EXCEPTION_ACTION_NOT_FOUND:
                    $dispatcher->forward([
                        'controller'    => 'error',
                        'action'        => 'notFound',
                        'params'        => [$exception]
                    ]);
                    return false;
                    break;
                default:
                    $dispatcher->forward([
                        'controller'    => 'error',
                        'action'        => 'index',
                        'params'        => [$exception]
                    ]);
                    return false;
            }
        }
    });

    $dispatcher = new Dispatcher();
    //Bind the EventsManager to the dispatcher
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
});

Use beforeException with the EventManager, it works fine for me
https://docs.phalcon.io/4.0/en/dispatcher



978

Don't know and can't try now if it's change something, but I'm using eventsManager::attach with dispatch:beforeException

$eventsManager->attach(
    'dispatch:beforeException',
    new NotFoundPlugin()
);

And my NotFoundPlugin

public function beforeException(Event $event, MvcDispatcher $dispatcher, \Exception $exception)
    {
        error_log($exception->getMessage() . PHP_EOL . $exception->getTraceAsString());

        if ($exception instanceof MvcDispatcherException) {
            switch ($exception->getCode()) {
                case DispatcherException::EXCEPTION_HANDLER_NOT_FOUND:
                case DispatcherException::EXCEPTION_ACTION_NOT_FOUND:
                    $dispatcher->forward(
                        [
                            'controller' => 'errors',
                            'action'     => 'show404',
                        ]
                    );
                    return false;
            }
        }

        $dispatcher->forward(
            [
                'controller' => 'errors',
                'action'     => 'show500',
            ]
        );

        return false;
    }

I am already using them in my code. My problem is, I am not getting any exception for non exist actions or wrong actions.

Don't know and can't try now if it's change something, but I'm using eventsManager::attach with dispatch:beforeException

$eventsManager->attach(
   'dispatch:beforeException',
   new NotFoundPlugin()
);

And my NotFoundPlugin

public function beforeException(Event $event, MvcDispatcher $dispatcher, \Exception $exception)
   {
       error_log($exception->getMessage() . PHP_EOL . $exception->getTraceAsString());

       if ($exception instanceof MvcDispatcherException) {
           switch ($exception->getCode()) {
               case DispatcherException::EXCEPTION_HANDLER_NOT_FOUND:
               case DispatcherException::EXCEPTION_ACTION_NOT_FOUND:
                   $dispatcher->forward(
                       [
                           'controller' => 'errors',
                           'action'     => 'show404',
                       ]
                   );
                   return false;
           }
       }

       $dispatcher->forward(
           [
               'controller' => 'errors',
               'action'     => 'show500',
           ]
       );

       return false;
   }


978

You're using : $eventsManager->attach("dispatch"

But I'm using :$eventsManager->attach('dispatch:beforeException',

But I don't know if it's really different :/

It's working and shows a 404 page for non existing controller, means I am getting DispatcherException::EXCEPTION_HANDLER_NOT_FOUND. But for wrong actions exception doesn't raise. So, DispatcherException::EXCEPTION_ACTION_NOT_FOUND case is not working.

You're using : $eventsManager->attach("dispatch"

But I'm using :$eventsManager->attach('dispatch:beforeException',

But I don't know if it's really different :/

edited Dec '20

Did you just set up this web server?

I run into this occasionally ToysRUs Credit Card

Look for your dir.conf and make sure index.php is the first file in the module.

Then also make sure to restart the service.

If you may, truly start with v4. As some distance as I comprehend it's not a large development over v3, and it's miles broadly speaking like minded with v3. However, v4 is the future, so in case you don't circulate to it, you will be locking yourself into v3 all the time. Better to transport now so that you can live with the most modern variations inside the future.

edited Dec '20

One cool component approximately it is that performance-wise, it's far arguably tons faster than other frameworks obtainable. The motive is that it isn't always your regular PHP framework whose files you simply finding tool reproduction onto your server and you are precise to move. It is in fact a PHP extension written in

I have the same problem i my pc idont know why this forum phalcon version is not working like the previous one i am searching for it some one suggested me some articles about this problem i will help you if i can see some solution in it.

Thanks all for giving suggestions. We have fixed this issue.

We had a magic __call() method inside our code. Which was triggering for non existing methods. That's why we didn't get any exceptions for non existing actions.

Thanks.