3

I make a cURL request to an API

http://site/user


I got back this response

    data: Object
    first_name: "Bob"
    last_name: "Jones"

I grab the first name and last name and concatenate them together and stored into a variable call $name.

$fn = VSE::user('first_name',$username);
$ln = VSE::user('last_name',$username);
$name = ucwords($fn.' '.$ln); // Bob Jones

I want to display this $name on my navigation.

Sending this $name variable with every view would be a little over kill. I'm seeking for a better way of doing this.

What should I do to make that variable accessible throughout my application routes/views ?


Restriction: I don't have the access to the Auth::user object.

Martin Thorsen Ranang
  • 2,394
  • 1
  • 28
  • 43
code-8
  • 54,650
  • 106
  • 352
  • 604
  • 2
    Your best bet here is to share the variable data between all views using `view()->share('name', $name)`. It offers better performance than using view composers, because composers trigger the code associated with them on every matched view render action, which makes them inefficient if you have many views that need the data in one request. You can find more info about that in the [Laravel Views Documentation](http://laravel.com/docs/5.1/views#view-data). – Bogdan Oct 29 '15 at 21:19

3 Answers3

3

Use a view composer to make the variables you need available in each view.

View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.

http://laravel.com/docs/5.1/views#view-composers

view()->composer('*', function ($view) {
    $fn = VSE::user('first_name',$username);
    $ln = VSE::user('last_name',$username);
    $name = ucwords($fn.' '.$ln); // Bob Jones

    $view->with('name', $name);
});

If you didn't want to use a view composer, you could simply add a call to the view()->share() method in your AppServiceProvider boot method

public function boot()
{
    $fn = VSE::user('first_name',$username);
    $ln = VSE::user('last_name',$username);
    $name = ucwords($fn.' '.$ln); // Bob Jones

    view()->share('name', $name);
}
Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108
  • Ben, I have a question according to your second suggestion. I won't have the access variable until the user submit a form post to it. It came from here `$username = strtolower(Input::get('username'));` - what should I do now ? – code-8 Oct 29 '15 at 17:13
  • Ben, do you know anything about that ? Just curious. :( – code-8 Oct 29 '15 at 18:42
  • If it happens during a particular event such as the submission of post data, you should add that to the session in the controller which handles the request and then use the above answer to add the session data to the view if it exists. – Ben Swinburne Oct 30 '15 at 00:01
3

I don't understand why you can't just put the name in laravel session or cache.

Laravel session

Set a value in Session:

Session::put('key', 'value');

Retrieving An Item From The Session:

$value = Session::get('key');

for more information refer http://laravel.com/docs/5.0/session#session-usage

Laravel cache

setting cache:

Cache::put('key', 'value', $minutes);

geting cache value:

$value = Cache::get('key');

for more information refer http://laravel.com/docs/5.0/cache#cache-usage

Rameez Rami
  • 5,322
  • 2
  • 29
  • 36
0

try create a static method returning what you want. And in all places you can do for example.

Custom.getName();

See this documentations:

http://php.net/manual/pt_BR/function.forward-static-call.php

or

php static function

Community
  • 1
  • 1
Elias Pinheiro
  • 237
  • 3
  • 11
  • Can you please add more details ? Where should I put it - which file ? How do I call it back in my view ? – code-8 Oct 29 '15 at 15:59
  • See this documentation. And you put where makes more sense. http://php.net/manual/pt_BR/function.forward-static-call.php or http://stackoverflow.com/questions/902909/php-static-function – Elias Pinheiro Oct 29 '15 at 16:02