Laravel 5本地化:exclude / public /目录

时间:2015-04-25 20:58:59

标签: php laravel laravel-5 laravel-routing laravel-middleware

我正在尝试在我的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时,它会将public替换为en,如果我转到/en/public,它会告诉我该路线不是yield存在。

任何帮助将公共目录排除在此之外或以更好的方式实现本地化并不涉及中间件的任何帮助?

1 个答案:

答案 0 :(得分:1)

您的图片必须位于公共文件夹下,公用文件夹必须是公共文件夹,并在Apache中配置为该文件夹。

您必须修改配置,以便使用以下网址访问该图片:http://0.0.0.0/css/images/myimage.png

当public是您配置的公用文件夹时,就会发生这种情况。