【发布时间】:2020-03-02 09:40:34
【问题描述】:
我的网址有问题。
我的网站上有发布新闻功能。为此,我使用了 Voyager CMS。我的网站有 3 种语言('ru'、'en'、'uz')。实际上我可以添加三种语言的新闻。 某些新闻仅以一种语言添加。另外两个是空的。
我的实际文章的直接 URL 看起来像这样 https://mywebsite.uz/hcont/562。
例如,网站的语言默认为英文。我只添加了俄语的新帖子(英语和乌兹别克语为空)。当有人像这样通过直接 URL https://mywebsite.uz/hcont/562 访问新添加的帖子时。它显示空白页面。因为网站加载默认语言(如果您第一次进入网站)。 我想通过单击直接 URL 来更改语言环境。
我知道首先,我必须像这样制作路线https://mywebsite.uz/news/562/en。 问题是我应该如何在中间件、控制器和路由中配置这个 URL?
下面是我现在为https://mywebsite.uz/hcont/562工作的代码:
web.php
Route::resource('hcont','HomeController');
Route::get("locale/{locale}" , function($locale){
Session::put('locale',$locale);
return redirect()->back();
});
HomeController.php
public function show($id){
$data = News::findOrFail($id);
$data->load('translations');
return view('the-news' , compact('data' ,'locale'));
}
home.blade.php
@foreach($news as $item)
<a href="{{ route('hcont.show', $item->id) }}">
<div class="news-item">
<div>
<img src="{{Voyager::image($item->img)}}" alt="">
<div class="news-content">
<h5>{!!$item->getTranslatedAttribute('title', \App::getLocale(), 'ru')!!}</h5>
<p>{!!str_limit($item->getTranslatedAttribute('body', \App::getLocale(), 'ru') , 70)!!}</p>
</div>
</div>
</div>
</a>
@endforeach
Localization.php
public function handle($request, Closure $next)
{
if(\Session::has('locale')){
\App::setLocale(\Session::get('locale'));
}
return $next($request);
}
【问题讨论】:
标签: php laravel localization