php Laravel 控制器中的全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32942379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:17:16  来源:igfitidea点击:

Global variable in laravel controller

phplaravelglobal-variables

提问by Nixxx27

I want the $yearvariable to be available in all functions of my PagesController. I tried this code but I didn't succeed.

我希望该$year变量在我的PagesController. 我试过这段代码,但没有成功。

class PagesController extends Controller
{
    public function __construct()
    {
        $dt = Carbon::parse();
        $year = $dt->year;
    }

    public function index()
    {
        return view('pages.index');
    }

    public function about()
    {
        return view('pages.about', compact('year'));
    }

    public function create()
    {
        return view('pages.create', compact('year'));
    }
}

回答by Tim

1. Option: Use the AppServiceProvider

1. 选项:使用 AppServiceProvider

In this case $year is available to ALL views!

在这种情况下,所有视图都可以使用 $year!

<?php

namespace App\Providers;

use Carbon;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->share('year', Carbon::parse()->year);
    }

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

2. Option: Use a View Composer

2. 选项:使用View Composer

In this case, the variable is only available to the views where you need it.

在这种情况下,该变量仅可用于您需要它的视图。

Don't forget to add the newly created provider to config/app.php!

不要忘记将新创建的提供程序添加到config/app.php!

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Carbon;

class ComposerServiceProvider extends ServiceProvider
{
    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function boot()
    {
        // Using Closure based composers...
        view()->composer('pages.*', function ($view) {
            $view->with('year', Carbon::parse()->year);
        });
    }

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

3. Use Blades @inject-method

3.使用刀片@inject-method

Within the views that need the year you could inject a Carbon instance like this:

在需要年份的视图中,您可以像这样注入一个 Carbon 实例:

@inject('carbon', 'Carbon\Carbon')

{{ $carbon->parse()->year }}

回答by aldrin27

Try this:

尝试这个:

//give private declaration
private $year;
public function __construct()
{
    $dt = Carbon::parse();
    $this->year = $dt->year;
}

public function index()
{
    return view('pages.index');
}

public function about()
{
    $year = $this->year;
    return view('pages.about',compact('year') );
}

public function create()
{
     $year = $this->year;
    return view('pages.create',compact('year') );
}

回答by Viktor

You can create a global singleton within App::before event

您可以在 App::before 事件中创建一个全局单例

App::before(function($request) {
    App::singleton('customYear', function(){
        $dt = Carbon::parse();
        $customYear = $dt->year;
        return $customYear;
    });

    // If you use this line of code then it'll be available in any view
    View::share('customYear', app('customYear'));

    //To get the same data in any controller you may use:
    $customYear = app('customYear');
});