Hello,
I seem to have a problem with my routing. I have cleared the default routes and added two routes for testing. The '/' and '/test' are added. But when I go to the website and use something like '/bla' I end up with the index controller and index action. Instead of that I should have received an exception with a forward to a error controller with a route404 action. The action itself is nothing more then a die( 'here' ); at the moment.
I have my controllers, views, plug-ins in the root of my application.
The goal that I want to reach is to display the errorController::route404() endpoint when a none existing route is being requested. '/bla' for example
Below is the definition of the router
$di->set( 'router', function()
{
$router =new Router( false );
$router->removeExtraSlashes( false ); // Allow for trailing slash
$router->add( '[/]{0,1}', [ 'controller' => 'index', 'action' => 'index' ]);
$router->add( '/test[/]{0,1}', [ 'controller' => 'index', 'action' => 'test' ]);
return $router;
});
Below is the definition of the dispatcher
$di->setShared( 'dispatcher', function()
{
// Create a new dispatcher
$dispatcher =new Dispatcher();
$dispatcher->setDefaultNamespace( 'Controllers' );
// Create an EventsManager
$eventsManager =new EventsManager();
// Attach an exception listener
$eventsManager->attach( 'dispatch:beforeException', new DispatchPlugin( ));
// Bind the EventsManager to the dispatcher
$dispatcher->setEventsManager( $eventsManager );
return $dispatcher;
});
Below is the definition of the DispatchPlugin class
<?php
namespace Plugins;
use Phalcon\Events\Event;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\User\Plugin;
use Phalcon\Mvc\Dispatcher\Exception;
class DispatchPlugin extends Plugin
{
public function beforeException( Event $event, Dispatcher $dispatcher, Exception $exception )
{
// Check stuff
switch( $exception->getCode( ))
{
// Handle 404 exceptions
case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
$action ='404';
break;
// All other errors
default:
$action ='503';
break;
}
$dispatcher->forward([
'action' => "error{$action}",
'controller' => 'error',
'namespace' => '\\Controllers',
]);
return false;
}
}