laravel 5 在控制器中获取默认语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29448527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:17:41  来源:igfitidea点击:

laravel 5 get default language in controller

phplaravel

提问by hkguile

i'm new to laravel 5, i add a line in a homecontroller:

我是 Laravel 5 的新手,我在家庭控制器中添加了一行:

echo Lang::getLocale();

but a error ocurrs:

但是出现错误:

FatalErrorException in HomeController.php line 47:
Class 'App\Http\Controllers\Lang' not found



<?php namespace App\Http\Controllers;

use App\Article;
use App\Photo;
use App\VideoAlbum;
use App\PhotoAlbum;
use Illuminate\Database\Eloquent;
use Illuminate\Support\Facades\DB;

class HomeController extends Controller {

    /*
    |--------------------------------------------------------------------------
    | Home Controller
    |--------------------------------------------------------------------------
    |
    | This controller renders your application's "dashboard" for users that
    | are authenticated. Of course, you are free to change or remove the
    | controller as you wish. It is just here to get your app started!
    |
    */

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('auth');

        //parent::__construct();

        //$this->news = $news;
        //$this->user = $user;
    }


    /**
     * Show the application dashboard to the user.
     *
     * @return Response
     */
    public function index()
    {

        //echo Lang::getLocale();
        $articles = Article::with('author')->orderBy('position', 'DESC')->orderBy('created_at', 'DESC')->limit(4)->get();

//      TODO: abstract to model
        $sliders = Photo::join('photo_albums', 'photo_albums.id', '=', 'photos.photo_album_id')->where('photos.slider',
            1)->orderBy('photos.position', 'DESC')->orderBy('photos.created_at', 'DESC')->select('photos.filename',
            'photos.name', 'photos.description', 'photo_albums.folder_id')->get();

        $photoAlbums = PhotoAlbum::select(array(
            'photo_albums.id',
            'photo_albums.name',
            'photo_albums.description',
            'photo_albums.folder_id',
            DB::raw('(select filename from ' . DB::getTablePrefix() . 'photos WHERE album_cover=TRUE and ' . DB::getTablePrefix() . 'photos.photo_album_id=' . DB::getTablePrefix() . 'photo_albums.id LIMIT 1) AS album_image'),
            DB::raw('(select filename from ' . DB::getTablePrefix() . 'photos WHERE ' . DB::getTablePrefix() . 'photos.photo_album_id=' . DB::getTablePrefix() . 'photo_albums.id ORDER BY position ASC, id ASC LIMIT 1) AS album_image_first')
        ))->limit(8)->get();

        $videoAlbums = VideoAlbum::select(array(
            'video_albums.id',
            'video_albums.name',
            'video_albums.description',
            'video_albums.folder_id',
            DB::raw('(select youtube from ' . DB::getTablePrefix() . 'videos WHERE album_cover=TRUE and ' . DB::getTablePrefix() . 'videos.video_album_id=' . DB::getTablePrefix() . 'video_albums.id LIMIT 1) AS album_image'),
            DB::raw('(select youtube from ' . DB::getTablePrefix() . 'videos WHERE ' . DB::getTablePrefix() . 'videos.video_album_id=' . DB::getTablePrefix() . 'video_albums.id ORDER BY position ASC, id ASC LIMIT 1) AS album_image_first')
        ))->limit(8)->get();

        return view('pages.home', compact('articles', 'sliders', 'videoAlbums', 'photoAlbums'));

        //return view('pages.welcome');
    }

}

what is the problem?

问题是什么?

回答by Limon Monte

You have to properly reference the Langalias. Either by importing it at the top:

您必须正确引用Lang别名。通过在顶部导入它:

use Lang;

Or prepending every call with a backslash:

或者在每个调用前加一个反斜杠:

\Lang::getLocale();

回答by mirza

You need to add:

您需要添加:

use Illuminate\Support\Facades\Lang;

after extending your controller. just after the first line.

扩展控制器后。就在第一行之后。

or

或者

use Lang;

回答by Abdul Manaf Saparuddin

You can do:

你可以做:

$defaultLocale = config('app.locale');