Answer the question
In order to leave comments, you need to log in
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;
}
Answer the question
In order to leave comments, you need to log in
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).
Maybe you need it?
public function beforeAction($action)
{
if(\Yii::$app->request->isAjax){
\Yii::$app->response->format = Response::FORMAT_JSON;
}
return parent::beforeAction($action);
}
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);
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 questionAsk a Question
731 491 924 answers to any question