[英]Laravel redirect to correct path on missing routes
在我的应用程序中,当用户以http://127.0.0.1:8000/showContent/aboutUs
laravel输入此网址时,将错误显示为
抱歉,找不到您要查找的页面。
正确的路径是http://127.0.0.1:8000/en/showContent/aboutUs
,我正在尝试对其进行管理并为此添加缺少的段:
路线:
Route::get('{lang?}/showContent/aboutUs', array('as' => 'lang', 'uses' => 'HomeController@showAboutUsPage'))->where('lang', '.+');
映射路线:
protected function mapWebRoutes()
{
$locale = request()->segment(1);
if ($locale != 'fa' && $locale != 'en') {
$locale = 'fa';
}
app()->setLocale($locale);
Route::middleware('web')
->namespace($this->namespace)
->prefix($locale)
->group(base_path('routes/web.php'));
}
在mapWebRoutes
函数上,我正在使用多语言并管理路线,并且对于缺少的参数作为segment(1)
上的语言键,我得到了错误,现在如何添加缺少的路段或管理路线以重定向到正确的路径?
对不起,重复评论! 它与您的Web服务器有关。 您可以使用.htaccess文件。 另一种方法是使用异常处理程序。 您可以检查异常类型以及请求路由。 然后根据需要重定向到适当的网址。
我的问题解决了:
我将kernel.php
更改为:
protected $middleware = [
\App\Http\Middleware\Language::class,
...
];
和Language
课:
public function handle($request, Closure $next)
{
$locale = $request->segment(1);
if (!array_key_exists($locale, config('app.locales'))) {
$segments = $request->segments();
array_unshift($segments,config('app.fallback_locale'));
return redirect(implode('/', $segments));
}
app()->setLocale($locale);
return $next($request);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.