yii\base\Module::beforeAction PHP Метод

beforeAction() публичный Метод

The method will trigger the [[EVENT_BEFORE_ACTION]] event. The return value of the method will determine whether the action should continue to run. In case the action should not run, the request should be handled inside of the beforeAction code by either providing the necessary output or redirecting the request. Otherwise the response will be empty. If you override this method, your code should look like the following: php public function beforeAction($action) { if (!parent::beforeAction($action)) { return false; } your custom code here return true; // or false to not run the action }
public beforeAction ( Action $action ) : boolean
$action Action the action to be executed.
Результат boolean whether the action should continue to be executed.
    public function beforeAction($action)
    {
        $event = new ActionEvent($action);
        $this->trigger(self::EVENT_BEFORE_ACTION, $event);
        return $event->isValid;
    }

Usage Example

Пример #1
2
 public function beforeAction($action)
 {
     if (parent::beforeAction($action)) {
         // Запретим доступ неавторизованным пользователям
         //            TODO убрать запрет и сделать нормальные поведения
         if (Yii::$app->getUser()->isGuest) {
             throw new ForbiddenHttpException('У вас нет прав просматривать данную страницу.');
         }
         if (isset($action->controller->breadcrumbItems)) {
             $action->controller->addBreadcrumbsItem(['label' => 'Личный кабинет', 'url' => ['/cabinet']]);
             $items = $action->controller->breadcrumbItems[$action->id];
             if ($items === 'not add') {
                 // Просто выходим и разрешаем выолнять Action
                 return true;
             }
             if (is_array($items)) {
                 foreach ($items as $item) {
                     if (isset($item['url'])) {
                         $action->controller->addBreadcrumbsItem(['label' => $item['label'], 'url' => $item['url']]);
                     } else {
                         $action->controller->addBreadcrumbsItem(['label' => $item['label']]);
                     }
                 }
             } else {
                 $action->controller->addBreadcrumbsItem(['label' => $items]);
             }
         } else {
             $action->controller->addBreadcrumbsItem(['label' => 'Личный кабинет']);
         }
         return true;
         // or false if needed
     } else {
         return false;
     }
 }
All Usage Examples Of yii\base\Module::beforeAction