Hi all,
I have solved, below is a guide. Thanks @Aleksandre Vardanidze and @mraspor
1 You need throw one error
public function testAction($id){
throw new \Exception("Thanhansoft");
}
2 Config dispatcher in services.php
$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(
array(
'controller' => 'index',
'action' => 'show404'
)
);
return false;
}
// Alternative way, controller or action doesn't exist
switch ($exception->getCode()) {
case Dispatcher::EXCEPTION_NO_DI:
if ($dispatcher->getControllerName() == 'admin') {
$msg = $exception->getMessage();
$dispatcher->forward([
'namespace' => 'App\Modules\Admin\Controllers',
'module' => 'admin',
'controller' => 'admin',
'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 And then
<?php
namespace App\Modules\Admin\Controllers;
class AdminController extends \App\Controllers\ControllerBackend {
public function errorAction($msg = null) {
echo $msg;
}
}
This is result: https://s1092.photobucket.com/user/thanhansoft/media/2016-05-12230524zpsmffucmwf.png.html