【问题标题】:Laravel send default variables to layoutLaravel 将默认变量发送到布局
【发布时间】:2013-01-16 16:01:45
【问题描述】:

具有以下由其他控制器扩展的类

class Admin_Controller extends Base_Controller 
{
    static $admin_layout = 'admin.layouts.default';

    public function __construct()
    {
        parent::__construct();

        $role_object = User::find(Auth::user()->id)->roles()->get();
        $role = $role_object[0]->attributes['name'];

如:

class Admin_Draws_Controller extends Admin_Controller
{
    public $restful = true;

    public function __construct()
    {
                $this->layout = parent::$admin_layout;
        parent::__construct();
    }

    public function get_index()
    {
        $view = View::make('admin.templates.draws');
        $this->layout->content = $view;
    }
}

如何将$role 变量发送到admin.layouts.default,以便在加载视图时获得它?

“全局”$role 变量的意义在于避免在我的所有View::make() 中调用它,如下所示:

$view = View::make('admin.templates.articles',
    array(
        'fields' => $fields,
        'data' => $results,
        'links' => $links,
                    'role' => 'role here'. // I don't want to add this where ever I call the View::make
    )
);
$this->layout->content = $view;

然后在我的header.blade.php 中进行echo $role 之类的操作

【问题讨论】:

    标签: php variables inheritance views laravel


    【解决方案1】:

    我最终做了以下工作。

    控制器

    // Example of variable to set
    $this->layout->body_class = 'user-register';
    
    $view = View::make('modules.user.register', array(
        'success' => true,
    ));
    
    $this->layout->content = $view;
    

    我的default.layout.php 视图

    <body class="<?php echo isset($body_class) ? $body_class : '' ;?>">
    
        @include('partials.header')
    
        {{ $content }}
    
        @include('partials.footer')
    

    变量,可以很容易地在视图中的任何其他上下文中使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-04
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 2014-03-28
      • 2012-02-02
      • 1970-01-01
      • 2020-08-30
      相关资源
      最近更新 更多