Hi All,
I have a userController
that handle all CRUD of user
(add/delete/create/update). I want to check user existance in top of all actions before any action calls and if user
does not exist (with passed id
) then throw exception, then catch it in my ErrorController
.
If I do this check in every action, everything works. but I cannot found ant way to handle this in initialize
or any point of dispaching loop steps.
This is my DispatchErrorPlugin
:
class DispatchErrorPlugin extends PhPlugin
{
public function beforeException(PhEvent $event, PhDispatcher $dispatcher, $exception)
{
//Handle 404 exceptions
if ($exception instanceof PhDispatchException) {
$dispatcher->setParam('exception', $exception);
$dispatcher->forward(array(
'controller' => 'error',
'action' => 'show404'
));
return false;
}
//Handle other exceptions
$dispatcher->setParam('exception', $exception);
$dispatcher->forward(array(
'controller' => 'error',
'action' => 'show500'
));
return false;
}
}
and in bootstrap I have attached this to dispatcher
:
$errorHandller = new DispatchErrorPlugin();
$evManager->attach('dispatch:beforeException', $errorHandller);
I want to do somthing like this in controller:
use Phalcon\Mvc\Dispatcher\Exception as PhDispatchException;
class ManageCourseController extends Phalcon\Mvc\Controller
{
public function initialize()
{
$userid = $this->dispatcher->getParam('id', 'int');
$user = Users::findFirstById($userid);
if(!$user) {
throw new PhDispatchException('User not found!');
}
}
public function showAction()
{
}
public function deleteAction()
{
}
}
This exception catched in bootstrap $application->handle()->getContent()
try/catch block!
Any adea?
Thanks.