How to access my lang files from controller in laravel 5

Converting from Laravel 4 to Laravel 5. Attempts to access the Lang file in the controller like this:

$var = Lang::get('directory/index.str1');

      

This gives me: Class 'App \ Http \ Controllers \ Lang' not found. However

{{Lang::get('directory/index.str1');}}

      

Works great in browse mode

+3


source to share


1 answer


You are missing a use statement for the Lang class , and PHP looks for it in the current namespace, which is why you see App \ Http \ Controllers \ Lang in the error message.

It works in a view because the view files are executed in the global namespace where the Lang facade exists .



To get your code to work, do one of the following:

  • Use fully qualified class name for Lang

    $var = \Lang::get('directory/index.str1');
    
          

  • Add a usage instruction at the top of your controller

    <?php namespace App\Http\Controllers;
    use Lang;
    
          

+3


source







All Articles