Php 仅当Artisan serve运行时,语言切换器才工作

Php 仅当Artisan serve运行时,语言切换器才工作,php,laravel,laravel-5.4,Php,Laravel,Laravel 5.4,我发现了一篇关于制作一个多语言的laravel网站的文章,这篇文章很有魅力 但是,当我没有运行php artisan service(例如,在我的托管服务上)时,切换语言会重定向到“原始语言/lang/destination语言”,并给我一个404 RouteNotFound: NotFoundHttpException in RouteCollection.php line 179: in RouteCollection.php line 179 at RouteCollection->

我发现了一篇关于制作一个多语言的laravel网站的文章,这篇文章很有魅力

但是,当我没有运行
php artisan service
(例如,在我的托管服务上)时,切换语言会重定向到“原始语言/lang/destination语言”,并给我一个404 RouteNotFound:

NotFoundHttpException in RouteCollection.php line 179:
in RouteCollection.php line 179
at RouteCollection->match(object(Request)) in LanguageController.php line 
22
at LanguageController->switchLang(object(Request), 'fr')
at call_user_func_array(array(object(LanguageController), 'switchLang'), 
array('request' => object(Request), 'lang' => 'fr')) in Controller.php 
line 55
at Controller->callAction('switchLang', array('request' => 
object(Request), 'lang' => 'fr')) in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), 
object(LanguageController), 'switchLang') in Route.php line 204
at Route->runController() in Route.php line 160
at Route->run() in Router.php line 559
我完全不知道它来自哪里。这是我的LanguageController.php方法
switchLang()

希望有帮助,谢谢


(我能感觉到这是一个根/公用文件夹路由问题,但我找不到位置)

这个问题需要我很长时间才能解决

您有两个选择:

  • 确保操作系统上有单独的域,并确保从路径中删除public
  • 上传你的项目在一个真正的领域,与我的工作
  • public function switchLang(Request $request, $lang)
    {
        // Store the URL on which the user was
        $previous_url = url()->previous();
    
        // Transform it into a correct request instance
        $previous_request = app('request')->create($previous_url);
    
        // Get Query Parameters if applicable
        $query = $previous_request->query();
    
        // In case the route name was translated
        $route_name = app('router')->getRoutes()->match($previous_request)->getName();
    
        // Store the segments of the last request as an array
        $segments = $previous_request->segments();
    
        // Check if the first segment matches a language code
        if (array_key_exists($lang, config('translatable.locales'))) {
    
            // If it was indeed a translated route name
            if ($route_name && Lang::has('routes.'.$route_name, $lang)) {
    
                // Translate the route name to get the correct URI in the required language, and redirect to that URL.
                if (count($query)) {
                    return redirect()->to($lang . '/' .  trans('routes.' . $route_name, [], $lang) . '?' . http_build_query($query));
                }
    
                return redirect()->to($lang . '/' .  trans('routes.' . $route_name, [], $lang));
            }
    
            // Replace the first segment by the new language code
            $segments[0] = $lang;
    
            // Redirect to the required URL
            if (count($query)) {
                return redirect()->to(implode('/', $segments).'?'.http_build_query($query));
            }
    
            return redirect()->to(implode('/', $segments));
        }
    
        return redirect()->back();
    }