Question

I am new to Laravel, and I if want, for example, have available a variable in all the views by default, is there a better way than passing that variable every time w/

return View::make('views_x.some_view')->with('client_to_show', $client_to_show);

? Or better than storing them in Session and then accessing them in the view? I tried putting the variable in layout master:

//layout.master.blade.php on top
<?php 
  $str_sel_names        = 'one_value';
?>
<!-- starts template -->
<!DOCTYPE html>
<html lang="en">

but this one is only accesible inside the template-layout.. not the subview...

@extends('layouts.master')

@section('content')
...
{{$str_sel_names}}

I get: [ErrorException Undefined variable: str_sel_names (View: C:\Work\lara_street\laravel\app\views\core\dashboard.blade.php)]

Ideas? Better ways to do this?

Was it helpful?

Solution

You can use View Composers:

View::composer(['store.index', 'products.*'], function($view)
{
    $view->with('client_to_show', $client_to_show);
});

You can put that in your routes file, filters file or, like, me, create a app/composers.php and load by adding

require app_path().'/composers.php';

To your app/start/global.php file.

View Share is another option:

View::share('client_to_show', $client_to_show);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top