我正在学习Laravel,有些事情我不清楚。
我已经在 BaseController 类中添加了一个方法,它将处理我的 ajax 请求。
public function ajaxView($page) {
$view = View::make($page);
if(Request::ajax()) {
$sections = $view->renderSections(); // returns an associative array of 'content', 'head' and 'footer'
return $sections['content']; // this will only return whats in the content section
}
return $view; // just a regular request so return the whole view
}
我有一个扩展BaseController的ProfileController,我有以下代码:
return View::make('profile.user')
->with('user', $user);
我想将其更改为:
return parent::ajaxView('profile.user')
->with('user', $user);
我怎样才能使我的 ajaxView 方法具有与 View::make() 相同的功能,以便我可以使用 ->with()?有没有办法扩展它,即使ajaxView是一种方法?
不使用 with
,您可以将数据作为数组传入:
public function ajaxView($page, $data = [])
{
$view = View::make($page, $data);
if (Request::ajax())
{
$sections = $view->renderSections();
return $sections['content'];
}
return $view;
}
然后在ProfileController
传入数据:
return $this->ajaxView('profile.user', ['user' => $user]);