php - Laravel Blade 模板@extends

标签 php laravel laravel-blade

在我看来,我正在扩展一个默认 Blade 模板,它充当我的主模板。我有一个 ViewComposer 设置,它为该模板提供许多变量。

我遇到了一种情况,我需要访问 index.blade.php 中的这些变量之一,这是启动 @extends 函数的页面。

通过 ViewComposer 传递的变量是否达到初始 View 的范围?或者我需要创建另一个 ViewComposer 来传递相同的变量。

ViewComposer

<?php

namespace App\Http\ViewComposers;

use Illuminate\View\View;
use Illuminate\Http\Request;
use App\Repositories\UserRepository;
use Sentinel;
use App\ProjectUsers;

class MasterComposer
{
    /**
     * The user repository implementation.
     *
     * @var UserRepository
     */
    protected $users;
    private $request;

    /**
     * Create a new profile composer.
     *
     * @param  UserRepository  $users
     * @return void
     */
    public function __construct(Request $request)
    {

        $this->request = $request;
        // Dependencies automatically resolved by service container...
        $uid = Sentinel::getUser()->id;
        $this->users = ProjectUsers::where("user_id", '=', $uid)->get();
    }

    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        $view->with('projects', $this->users);
        $view->with('activeProject', $this->request->session()->get("activeProject"));
    }
}

ComposerServiceProvider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider {
    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function boot() {
        // Using class based composers...
        view ()->composer ( 'admin/layouts/default', 'App\Http\ViewComposers\MasterComposer' );
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register() {
        //
    }
}

index.blade.php

@extends('admin/layouts/default')
{{ $activeProject }}

这最终给了我一个 undefined variable 错误。

Undefined variable: activeProject (View: /home/laravel/public_html/base/resources/views/admin/index.blade.php)

最佳答案

是的,主布局中可用的所有变量都将传递给其任何子布局。

关于php - Laravel Blade 模板@extends,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38898045/

相关文章:

php - whereBetween 和 orWhere 获取日期范围内的数据

javascript - PHP+JS getElementById().value 返回 int 而不是 float

php - Laravel:不采用__invoke方法吗?

php - 证书 x509 Laravel 身份验证 SSL

php - 无法在 Laravel5.2 View 中输出图像

javascript - 使用 Javascript 在 Laravel 中重新格式化电话号码

javascript - 将 blade 文件中的数据直接传递给 Vue 实例

php - 使用 PHP 将表单字段作为 URL 参数传递

php - 数据库:排序分层数据(修改后的预序树遍历):如何检索直系子级

PHP将嵌套的json插入到mySQl中