I want to stop the controller action when a block code is complete.
This some example code.
class Controller extends \Phalcon\Mvc\Controller {
/**
* Check if user have access
*
*/
protected function isAllowed($perm = false)
{
/**
* Always allowed if $perm not defined
*/
if (!$perm) {
return false;
}
/**
* if user not login
*/
if (!$this->authentication->isLoggedin()) {
/* Redir to login */
$this->response->redirect( $this->url->get('authentication/login') );
return false;
} else {
/* Check for user access */
if ($this->authorization->isAllowed($perm)) {
return true;
} else {
/* if not have access, it will be redir to index */
$this->flash->warning("U not have permission to access page");
return $this->response->redirect( $this->url->get('administrator/') );
}
}
}
}
and the another controller that extend from base is
class postController extends Controller {
/**
* Add group
*
*/
public function addAction()
{
/*
Check user access
this must redirect to login and stop exe script
*/
$this->isAllowed('group_add');
/*
But when i check with `Postman`
without auth i redirect to login.
but when i use post method and fill the header body like bellow.
i still redir to login but this code execute. why? and how to stop it.
*/
/* If request is POST */
if ($this->request->isPost()) {
/* Get all inputs from form */
$inputs = $this->request->getPost();
$name = $this->request->getPost('name', ['trim', 'striptags', 'string']);
$definition = $this->request->getPost('definition', ['trim', 'striptags', 'string']);
/* Filter validation */
if (!Validation::make($inputs, $this->rules())) {
$this->flash->error('...');
... redirect to page
return false;
}
/* Get from database */
$model = new AauthGroups;
$group = $model->findFirst([
'name = :name:',
'bind' => [
'name' => $name
]
]);
/* If cant find group then add it */
if (!$group) {
/* Set & save data */
$model->name = $name;
$model->definition = $definition;
$model->save();
$this->flash->success('Yay!! We found that name in database, do u want to change it?');
return;
}
/* If it finded than set flash error */
else {
$this->flash->error('Oops!! We found that name in database, do u want to change it?');
return;
}
}
}
}
I tried to use exit;
but the view will not render. Can you explain it?