如何在控制器phalcon php中停止执行代码

时间:2017-03-22 15:37:16

标签: php authorization phalcon

我想在块代码完成时停止控制器操作。

这是一些示例代码。

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/') );
            }
        }
    }
}

和从base扩展的另一个控制器是

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;
            }
        }
    }
}

我尝试使用exit;,但视图无法渲染。你能解释一下吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试send()这样的回复吗?

/* Redir to login */
$this->response->redirect( $this->url->get('authentication/login') )->send();
return false;

如果这不起作用,您可能必须在“BaseController”中使用beforeExecuteRoute

class Controller extends \Phalcon\Mvc\Controller {    

    public function beforeExecuteRoute()
    {
        // Check if logged
        if (!$notLogged) {
            $this->response->redirect('....')->send();
            return false;
        }
    }

我稍后会检查这些。希望到那时它对你有用。