在控制器中设置默认布局 (Laravel 5.2)
Setting a default layout in the controller (Laravel 5.2)
我想在我的 BaseController 中设置默认布局,以便它用于我拥有的每个视图。我不想在每个视图中都使用“@extends”。
在 Laravel 4 这很容易做到。现在我在 Laravel 5.2 中找不到任何方法来做到这一点。
有人知道吗?
顺便说一句:这是我关于 Whosebug 的第一个问题,希望我遵守规则。
我找到了解决方案。不深入研究 Laravel 4 代码真是太蠢了。我认为布局链接在核心深处的某个地方,但它只是控制器,它具有 "callAction" 功能。此函数设置布局,然后调用正确的方法。
下面是从这里获取的代码:
https://laracasts.com/discuss/channels/general-discussion/laravel-5-this-layout-content-not-working
<?php namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
class BaseController extends Controller {
protected $layout = 'core::layouts.default';
/**
* Show the user profile.
*/
public function setContent($view, $data = [])
{
if ( ! is_null($this->layout))
{
return $this->layout->nest('child', $view, $data);
}
return view($view, $data);
}
/**
* Set the layout used by the controller.
*
* @param $name
* @return void
*/
protected function setLayout($name)
{
$this->layout = $name;
}
/**
* Setup the layout used by the controller.
*
* @return void
*/
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = view($this->layout);
}
}
public function callAction($method, $parameters)
{
$this->setupLayout();
$response = call_user_func_array(array($this, $method), $parameters);
if (is_null($response) && ! is_null($this->layout))
{
$response = $this->layout;
}
return $response;
}
}
我想在我的 BaseController 中设置默认布局,以便它用于我拥有的每个视图。我不想在每个视图中都使用“@extends”。
在 Laravel 4 这很容易做到。现在我在 Laravel 5.2 中找不到任何方法来做到这一点。
有人知道吗?
顺便说一句:这是我关于 Whosebug 的第一个问题,希望我遵守规则。
我找到了解决方案。不深入研究 Laravel 4 代码真是太蠢了。我认为布局链接在核心深处的某个地方,但它只是控制器,它具有 "callAction" 功能。此函数设置布局,然后调用正确的方法。
下面是从这里获取的代码:
https://laracasts.com/discuss/channels/general-discussion/laravel-5-this-layout-content-not-working
<?php namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
class BaseController extends Controller {
protected $layout = 'core::layouts.default';
/**
* Show the user profile.
*/
public function setContent($view, $data = [])
{
if ( ! is_null($this->layout))
{
return $this->layout->nest('child', $view, $data);
}
return view($view, $data);
}
/**
* Set the layout used by the controller.
*
* @param $name
* @return void
*/
protected function setLayout($name)
{
$this->layout = $name;
}
/**
* Setup the layout used by the controller.
*
* @return void
*/
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = view($this->layout);
}
}
public function callAction($method, $parameters)
{
$this->setupLayout();
$response = call_user_func_array(array($this, $method), $parameters);
if (is_null($response) && ! is_null($this->layout))
{
$response = $this->layout;
}
return $response;
}
}