gpt4 book ai didi

php - Laravel Blade 模板@extends

转载 作者:行者123 更新时间:2023-12-02 03:44:30 24 4
gpt4 key购买 nike

在我看来,我正在扩展一个默认 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com