【问题标题】:Setting a default layout in the controller (Laravel 5.2)在控制器中设置默认布局(Laravel 5.2)
【发布时间】:2016-07-02 09:25:14
【问题描述】:

我想在我的 BaseController 中设置一个默认布局,以便它用于我拥有的每个视图。我不想在每个视图中都使用“@extends”。

在 Laravel 4 中,这很容易做到。现在我在 Laravel 5.2 中找不到任何方法。

有人有想法吗?

顺便说一句:这是我关于stackoverflow的第一个问题,我希望我遵守规则。

【问题讨论】:

    标签: php laravel-5.2 laravel-blade


    【解决方案1】:

    我找到了解决方案。我有点笨,不想更深入地研究 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;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 2012-11-10
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多