15

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

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'));
    }
}

3 Answers 3

24

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 }}
Sign up to request clarification or add additional context in comments.

8 Comments

hello im new to laravel. how can i use the first option. do i need to declare something in pagesController.php cause i got an error of "Undefined variable: year". P.S im using laravel 5
If you've added the code to your AppServiceProvider like described, there's nothing else you need to do. What is throwing this error?
i use this.. public function about() { return $year ; } and the error is Undefined variable: year. i copied and paste your code.
For this to work you can't just return the variable $year because it gets shared with a view. You need to return a view from your function: return view('pages.about') and then you can access $year from your blade template like this: {{ $year }}
yap thats correct i need the year to be pass on all views. thanks for the help
|
14

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') );
}

2 Comments

I try this private $year; and in function $year = $this->year; return view('pages.about',compact('year') ); but in view it returns null?
here is what I done private $superSupplier; and public function __construct() {pointedSupplier = Supplier::where( 'id', Auth::guard( 'supplier' )->id() )->with( 'supplier_role' )->first();} $this->superSupplier = $pointedSupplier->supplier_role->supper_supplier; this returns null
5

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');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.