D
D
DarkByte20152017-10-16 14:16:41
Yii
DarkByte2015, 2017-10-16 14:16:41

How to process the result of an action?

I use these tabs . There is such a thing that when you go to another tab, you can specify a link to load content. So I wanted for a group of my actions so as not to prescribe in each one to make such a handler that if the request is Ajax, then as it says the result of the request (tab rendering) for jsonencode. Please tell me how to do it?
PS PHP 5.6, Yii2.
upd. Not currently found this solution:

public function render($view, $params = []) { // перегрузил
  $result = parent::render($view, $params);

  if (StringHelper::startsWith($this->action->id, 'step') && Yii::$app->request->isAjax)
    return Json::encode($result);

  return $result;
}

But it's rough. Why can be seen in the screenshot. Not only the content of the tab is rendered, but the entire page. So far I don't know how to solve it.
59e4a962f2fe2699653872.png

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
DarkByte2015, 2017-10-17
@DarkByte2015

Not relevant. I did it without ajax. It turns out that the tabs had a url parameter. I just set it and now, when I click on a tab, I switch to another one with a full render (maybe worse, but easier).

A
Abdula Magomedov, 2017-10-16
@Avarskiy

Maybe you need it?

public function beforeAction($action)
        {
            
            if(\Yii::$app->request->isAjax){
                \Yii::$app->response->format = Response::FORMAT_JSON;
            }
            
            return parent::beforeAction($action);
        }

E
Evgeny Bukharev, 2017-10-16
@evgenybuckharev

Redefining the render method in this case is not the best solution, in general, you can do this:

$is_stap_ajax=StringHelper::startsWith($this->action->id, 'step') && Yii::$app->request->isAjax;
return $is_stap_ajax ? $this->renderPartial($view, $params) : $this->render($view, $params);

M
Maxim Timofeev, 2017-10-17
@webinar

why render() and then Json::encode if you just need renderAjax. For the render pulls the layout, but the renderAjax does not.

public function actionRender($view, $params = []) { // перегрузил
  if(!Yii::$app->request->isAjax)
     throw new MethodNotAllowedHttpException('только ajax, черти');

  if (StringHelper::startsWith($this->action->id, 'step') ) //и другие проверки
    return $this->renderAjax($view, $params);

  throw new ErrorException('чет не пашет');
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question