nCore
nCore

Reputation: 2087

Global variable in laravel 4

I was wondering how to do a global variable to save me few lines of copy and pasting this lines. Array it probably and put them in one variable instead? I want to use this variable in other routes.

  $providerEmail = Auth::user()->email;
  $providerName = Auth::user()->first_name;
  $providerSurname = Auth::user()->last_name;
  $providerMobile = Auth::user()->mobile;

Upvotes: 19

Views: 27276

Answers (6)

The Alpha
The Alpha

Reputation: 146191

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

App::before(function($request)
{
    // Singleton (global) object
    App::singleton('myApp', function(){
        $app = new stdClass;
        if (Auth::check()) {
            // Put your User object in $app->user
            $app->user = Auth::User();
            $app->isLoggedIn = TRUE;
        }
        else {
            $app->isLoggedIn = FALSE;
        }
        return $app;
    });
    $app = App::make('myApp');
    View::share('myApp', $app);
});

In any view, use it like

if($myApp->isLoggedIn) {
    $myApp->user->email;
    $myApp->user->first_name;
    // ...
}

In any controller, you can use

$myApp = App::make('myApp');
if($myApp->isLoggedIn) {
    $myApp->user->email;
    $myApp->user->first_name;
    // ...
}

Check out Application Events.

Upvotes: 52

Justin
Justin

Reputation: 27301

I like the View::share method.

Add the following in app/controllers/BaseController.php

class BaseController extends Controller {
    public function __construct() {       
        $name = 'jack';    
        View::share('user', $name); // Share $user with all views
    }
}

and now $user will be available to all your views.

References: Laravel docs, blog post

Upvotes: 5

ajtrichards
ajtrichards

Reputation: 30565

The best way i've seen is by using a config file.

In your app -> config folder, you create a new file called (for example settings.php)

 app
     config
         settings.php

Then in your configuration file you just created (settings.php) you could add:

<?php

$setting_data['foo'] = 'bar';
$setting_data['bar'] = 'foo';

return $setting_data;

You can then retrieve the config file from your code using:

echo Config::get('settings.foo'); // Will echo bar
echo Config::get('settings.bar'); // Will echo foo

Upvotes: 13

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

Globals are bad. No matter of what, don't use global variables, don't think about using global variables, always think about how can you not use them in your code and still have all you need to have. Here are some reasons, and there are lot more.

Instead, use Laravel power to help you:

Enforce login on your routes creating groups of authenticated routes:

Route::group(array('before' => 'auth'), function()
{
    Route::get('/users/posts', array('as'=>'users.posts.index', 'uses'=>'PostsController@usersPostsIndex'));
});

Now you know that every call to your posts will be authenticated, you can just

class PostsController extends Controller {

    public function usersPostsIndex()
    {
        return View::('users.posts.index')->
                with('user', Auth::user());
    }

}

In your view you'll just have to

{{$user->email}}
{{$user->first_name . ' ' . $user->last_name}}
{{$user->email}}
{{$user->mobile}}

If you don't want to write code to send a User instance to all your views, use the magic of Laravel's View Composers, adding this to your filters.php or creating a composers.php file:

View::composer(array('users.posts.index','users.posts.edit'), function($view)
{
    $view->with('user', Auth::user());
});

And this is now how your views can be used now:

class PostsController extends Controller {

    public function usersPostsIndex()
    {
        return View::('users.posts.index');
    }

    public function usersPostsEdit()
    {
        return View::('users.edit.index');
    }

}

Because Laravel will automatically inject Auth::user() in those views as $user.

Upvotes: 7

Sajan Parikh
Sajan Parikh

Reputation: 4940

I also gave you the same answer in another question you asked, you did not respond. Link

There is no reason to have a separate variable for each property of your $provider model. Simply save the entire model to a variable like this.

if (Auth::check())
{
    $provider = Auth::user();
}

This would generally be done in a route like this.

Route::get('/provider/page/example', function()
{
    $provider = Auth::user();

    return View::make('name.of.view', ['provider' => $provider]);
});

After having done that, you can access the different properties in of the $provider in your views like this.

<p>Your email address is: {{$provider->email}} and your first name is {{$provider->first_name}}</p>

Another option is to use a controller and set this variable only once in the controller, making it accessible from all views using View::share().

class ProviderController extends BaseController {

    protected $provider;

    public function __construct()
    {
        $this->provider = Auth::user();

        View::share('provider', $this->provider);
    }

    public function getIndex()
    {
        return View::make('some.view.name');
    }
}

After having done just this, you can use the $provider variable in your views as shown above using things like $provider->email. You can also use it elsewhere in the controller by using $this->provider.

Upvotes: 1

user745235
user745235

Reputation:

You should create an object to do that.

Create a provider object with those properties, email, name, etc and instantiate it and set the properties values, like:

$provider = new Provider();

$provider->email = Auth::user()->email;

And then you save the object in your session:

$_SESSION['provider'] = $provider;

I'm not familiar with Laravel and I don't know if it's a good practice to work directly with the Auth object but a simpler solution would be:

$_SESSION['provider'] = Auth::user();

Also be aware of working with sensitive information on your session :)

Upvotes: 0

Related Questions