dimanche 13 avril 2014

php - le meilleur moyen de mettre certaines variables dans Laravel-> lame - Stack Overflow


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?




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

Or View Share:


View::share('client_to_show', $client_to_show);


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?



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

Or View Share:


View::share('client_to_show', $client_to_show);

Related Posts:

0 commentaires:

Enregistrer un commentaire