在 blade 模板中定义了一个变量, 如何在继承该模板的 blade 文件中使用 ?

// 父模板: laryout.blade.php
<!doctype html>
<html>
<head>
    <title>Document</title>
</head>
<body>
    {{ $variable = 'abccd' }}
    <div class="container">
        @yield('content')
    </div>
</body>
</html>
// 子 blade.php
@extends('layout.php')
@section('content')
        {{ $variable }}   // 报错, 找不到变量
@endsection
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

在 AppServiceProvider 中

    public function register()
    {
        //
        \Illuminate\Support\Facades\View::share('variable', request()->fullUrl());
    }

见文档: 视图 -- 与所有视图共享数据

5年前 评论
讨论数量: 1

在 AppServiceProvider 中

    public function register()
    {
        //
        \Illuminate\Support\Facades\View::share('variable', request()->fullUrl());
    }

见文档: 视图 -- 与所有视图共享数据

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!