5
我想在我的Laravel 5項目中實現本地化,並且遇到了問題。我把在趕語言中間件如下:Laravel 5本地化:排除/公共/目錄
<?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);
}
}
kernel.php:
protected $middleware = [
'App\Http\Middleware\Language',
'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
'Illuminate\Cookie\Middleware\EncryptCookies',
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
'Illuminate\Session\Middleware\StartSession',
'Illuminate\View\Middleware\ShareErrorsFromSession',
'App\Http\Middleware\VerifyCsrfToken',
];
routeserviceprovider.php:
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');
});
}
它的工作完美,除了一個事情。當我嘗試去http://0.0.0.0/public/css/images/myimage.png它將替換爲en
,如果我去/en/public
它告訴我路線不存在。
獲取公共目錄的任何幫助都被排除在外,或者以不涉及中間件的更好方式實現本地化?
請參閱:http://stackoverflow.com/q/29723344/2772319 – Digitlimit
您的網絡服務器應該直接以'站點根目錄'的方式提供'public'目錄,而不是將它傳遞迴PHP。這是解決這個問題的正確方法。你使用什麼網絡服務器?你可以與我們分享這個配置文件嗎? –