拜托,您能否给我一个想法,为什么我的代码不起作用?
我使用本教程:https://laracasts.com/discuss/channels/tips/example-on-how-to-use-multiple-locales-in-your-laravel-5-website/?page=1能够使我的laravel5网站多语言,但现在它抛出,当我尝试这样的网址时: smth.com/en 此错误: 在编译.php第7693行中 NotFoundHttpException:
你有什么想法,请问如何让它运作起来?非常感谢你:))
CODE:
routes.php的一部分
Route::get('login', [
'as' => 'login',
'uses' => 'Auth\AuthController@login'
]);
Route::post('login', [
'as' => 'postLogin',
'uses' => 'Auth\AuthController@authenticate'
]);
RouteServiceProvider:
// here I add prefix
public function map(Router $router, Request $request)
{
$locale = $request->segment(1);
$this->app->setLocale($locale);
$router->group(['namespace' => $this->namespace, 'prefix' => $locale], function($router) {
require app_path('Http/routes.php');
});
}
// here is complete language middleware
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Routing\Redirector;
use Illuminate\Http\Request;
use Illuminate\Foundation\Application;
use Illuminate\Contracts\Routing\Middleware;
class Language implements Middleware {
public function __construct(Application $app, Redirector $redirector, Request $request) {
$this->app = $app;
$this->redirector = $redirector;
$this->request = $request;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// Make sure current locale exists.
$locale = $request->segment(1);
if ( ! array_key_exists($locale, $this->app->config->get('app.locales'))) {
$segments = $request->segments();
$segments[0] = $this->app->config->get('app.fallback_locale');
return $this->redirector->to(implode('/', $segments));
}
$this->app->setLocale($locale);
return $next($request);
}
}