3

I have created a file localization.php and i defined some variable like:

localization.php

 <?php

 $color = 'red';
 $background = 'orange';

 ?>

now i included the localization.php file in all my blade templates like:

@include('localization') 

and here in template i want to echo the variables which i mentioned in localiztion.php

 {{$color}} //when i use this it show error 'undefined variable'

can you guys please help me to fix this

Muhammad Kazim
  • 611
  • 2
  • 11
  • 26
  • 1
    Are You sure your file is properly included. Blade will look for it in app/views directory. Mby take a look [here](https://stackoverflow.com/a/21476083/5962118). – Anwarus Nov 24 '17 at 07:36
  • yes my file is in view directory 'app\resources'view\localization.blade.php' – Muhammad Kazim Nov 24 '17 at 07:39
  • if i echo something like echo 'abc' it works and shows in template but when i use the variable and call this variable in blade file its show error – Muhammad Kazim Nov 24 '17 at 07:42
  • add an echo "hello world" to your localization.php to see if it is really being included in the blade, if the hello world shows then it is really added. if not, check the element then see if there is an error directory cannot be found. it means you have the wrong address – Rey Norbert Besmonte Nov 24 '17 at 07:43
  • echo works and shows the result but when i use variable and call it it shows error undefined variable – Muhammad Kazim Nov 24 '17 at 07:44
  • may I ask why do you have a variable in the blade? as MVC state that should be in the controller where you should return in via compact or a ->with() method – Rey Norbert Besmonte Nov 24 '17 at 07:46
  • i want to use this localization file in all view i have some genereic variables and i want to include this in all blade templates – Muhammad Kazim Nov 24 '17 at 07:55
  • I created an answer and tested it just now. That should work now. – Rey Norbert Besmonte Nov 24 '17 at 08:21

2 Answers2

0

"i want to use this localization file in all view i have some genereic variables and i want to include this in all blade templates "

the best way to do this is put it on constants. in directory config>>constants.php

<?php 

return
[
'ToBeReturn' => [
     'color' => 'red',
     'background' => 'orange']
];

then at your controller you can return it

 public function index() 
    {
        return view('index')
                   ->with('color',config('constants.ToBeReturn.color')
                   ->with('background',config('constants.ToBeReturn.background'));
    }

now on your view you can view it as

{{ $color }}
{{ $background }}

do a

php artisan config:cache

This is the standard the MVC laravel follows. Putting variable in a view is not good. so since you want a localize variable, put it on constants.

Rey Norbert Besmonte
  • 791
  • 2
  • 12
  • 29
0

I tried this and it works

View::share(with([
'color' => 'red',
'background' => 'orange'
]));

it works, but unfortunately it only allows to add in route file .

Muhammad Kazim
  • 611
  • 2
  • 11
  • 26