Global variable in laravel controller

后端 未结 3 724
Happy的楠姐
Happy的楠姐 2020-12-30 08:05

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



        
相关标签:
3条回答
  • 2020-12-30 08:38

    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') );
    }
    
    0 讨论(0)
  • 2020-12-30 08:38

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

    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');
    });
    
    0 讨论(0)
  • 2020-12-30 08:44

    1. Option: Use the AppServiceProvider

    In this case $year is available to ALL views!

    <?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

    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!

    <?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

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

    @inject('carbon', 'Carbon\Carbon')
    
    {{ $carbon->parse()->year }}
    
    0 讨论(0)
提交回复
热议问题