Hi all, I using Phalcon 2.1 with multi module. I tried several times with "throw new Exception" but unsuccessfully. Please help me
I visit this action and get whitepapers I want the content error show in the file view module Home
1 In Action
throw new \Exception("Không tồn tại mẫu tin.");
2 File services.php. I think the problem in the services file
$di = new FactoryDefault();
$di->set('dispatcher', function () {
// Create an EventsManager
$eventsManager = new EventsManager();
// Attach a listener
$eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
// Handle 404 exceptions
if ($exception instanceof DispatchException) {
$dispatcher->forward([
'namespace' => 'App\Modules\Home\Controllers',
'module' => 'home',
'controller' => 'index',
'action' => 'show404'
]);
return false;
}
// Alternative way, controller or action doesn't exist
switch ($exception->getCode()) {
case Dispatcher::EXCEPTION_NO_DI:
$msg = $exception->getMessage();
if ($dispatcher->getControllerName() == 'admin') {
$dispatcher->forward([
'namespace' => 'App\Modules\Admin\Controllers',
'module' => 'admin',
'controller' => 'admin',
'action' => 'error',
'params' => ['msg' => $msg]
]);
}else{
$dispatcher->forward([
'namespace' => 'App\Modules\Home\Controllers',
'module' => 'home',
'controller' => 'index',
'action' => 'error',
'params' => ['msg' => $msg]
]);
}
return false;
case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward(
array(
'controller' => 'index',
'action' => 'show404'
)
);
return false;
}
});
$dispatcher = new MvcDispatcher();
// Bind the EventsManager to the dispatcher
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
3 index.php
try{
} catch (\Exception $e) {
echo $e->getMessage() . '<br>';
echo '<pre>' . $e->getTraceAsString() . '</pre>';
}