Laravel重定向到缺少路线的正确路径 [英] Laravel redirect to correct path on missing routes

查看:93
本文介绍了Laravel重定向到缺少路线的正确路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,当用户输入http://127.0.0.1:8000/showContent/aboutUs laravel作为

In my application when user enter this url as http://127.0.0.1:8000/showContent/aboutUs laravel show error as an

对不起,找不到您要查找的页面.

Sorry, the page you are looking for could not be found.

正确路径的

http://127.0.0.1:8000/en/showContent/aboutUs,我正在尝试对其进行管理并为此添加缺失的段:

for that correct path is http://127.0.0.1:8000/en/showContent/aboutUs and I'm trying to manage that and adding missing segment for that for example:

路线:

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)上的语言键,我得到了错误,现在如何添加缺失的段或管理路线以重定向到正确的路径? /p>

on mapWebRoutes function i'm using multi language and manage routes, and for missing parameters as language key on segment(1) I get error, now how can I add missing segment or manage routes to redirect to correct path?

推荐答案

我的问题已解决:

我将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);
}

这篇关于Laravel重定向到缺少路线的正确路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆