duanchendu69495 2017-03-22 15:37
浏览 40

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

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?

  • 写回答

1条回答 默认 最新

  • duanbo6871 2017-03-22 16:01
    关注

    Can you try send()-ing the response like this?

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

    If this does not work, you may have to use beforeExecuteRoute in your "BaseController".

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

    I will be able to check those later. Hope it works for you by then.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分