【问题标题】:Display data in view based on session基于会话在视图中显示数据
【发布时间】:2019-11-03 07:48:42
【问题描述】:

我有一个带有语言列“lang”的帖子表,我想只显示会话中存储的语言的帖子。

但我一直得到的只是默认语言(Fr)的帖子

控制器:

public function index(Request $request)

{
        if ($request->session()->has('en')) {
            $posts = Post::where('lang','=','En')
            ->with('author','tags','category','comments')
            ->latestFirst()
            ->filter(request()->only(['term', 'year', 'month']))
        }
        elseif ($request->session()->has('ar')) {
            $posts = Post::where('lang','=','Ar')
            ->with('author','tags','category','comments')
            ->latestFirst()
            ->filter(request()->only(['term', 'year', 'month']))
        }
        else  {
            $posts = Post::where('lang','=','Fr')
            ->with('author','tags','category','comments')
            ->latestFirst()
            ->filter(request()->only(['term', 'year', 'month']))
            }
  return view("blog.index", compact('posts'));
}

【问题讨论】:

    标签: php laravel session controller multilingual


    【解决方案1】:

    从应用实例获取当前区域设置并回退到Fr,因为它是默认设置

    public function index(Request $request)
    {
        $locale = ucfirst(app()->getLocale());
        $posts = Post::where('lang', $locale)
            ->with('author', 'tags', 'category', 'comments')
            ->latestFirst()
            ->filter(request()->only(['term', 'year', 'month']));
        return view("blog.index", compact('posts'));
    }
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      这是因为没有带有键“Ar”或“En”的会话值。

      您有 2 个选项。通过中间件或特征,您可以在需要时在控制器类中使用。

      请注意,如果您使用我将要发布的此选项,搜索机器人将无法获取它,因为 URL 完全相同。对于我的项目没关系,但它可以为你的。如果您不想要这个,您必须选择将其添加到您的路线中 (https://yourweb.site/en/your_urls)

      如果您选择使用中间件,要更改语言,您必须在任何路由 ?lang=en 或 ?lang=fr 仅添加一次,之后您的会话将记住该选择。

      中间件

      namespace App\Http\Middleware;
      
      use Closure;
      
      class Language
      {
          /**
           * The availables languages.
           *
           * @array $languages
           */
          protected $languages = ['en', 'ar', 'fr'];
      
          /**
           * Handle an incoming request.
           *
           * @param  \Illuminate\Http\Request $request
           * @param  \Closure                 $next
           *
           * @return mixed
           */
          public function handle($request, Closure $next)
          {
              if ($request->session()->has('lang'))
              {
                  $request->session()->put('lang', $request->getPreferredLanguage($this->languages));
              }
              if ($request->has('lang'))
              {
                  $request->session()->put('lang', $request->get('lang'));
              }
              app()->setLocale($request->session()->get('lang'));
      
              return $next($request);
          }
      }
      

      如果有新访客到达,我们将以首选语言为他或她提供服务,在您的情况下为法语。现在,任何对另一种语言的选择都会在代码中的任何位置保留为 session('lang')

      $posts = Post::where('lang','=', session('lang', 'fr')->...
      

      【讨论】:

        猜你喜欢
        • 2015-07-05
        • 2013-09-16
        • 2016-05-19
        • 2011-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-09
        • 1970-01-01
        相关资源
        最近更新 更多