5

I am trying to implement localization in my Laravel 5 project and I'm running into an issue. The middleware that I put in to catch the language is as follows:

<?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');
        });
    }

It's working perfectly, except for one thing. When I try to go to http://0.0.0.0/public/css/images/myimage.png it is replacing public with en and if I go to /en/public it's telling me that the route doesn't exist.

Any help getting the public directory excluded from this or implementing localization in a better way that doesn't involve middleware?

watzon
  • 2,401
  • 2
  • 33
  • 65
  • 1
    Please see this: http://stackoverflow.com/q/29723344/2772319 – Emeka Mbah Apr 25 '15 at 23:11
  • 4
    Your webserver should be serving the `public` directory directly as the site root, not passing it back to PHP. That's the proper way to fix this. What webserver are you using? Can you share that config file with us? – Moshe Katz Jun 11 '15 at 17:50

1 Answers1

1

Your image must be under public folder, and the public folder must be the folder public and configured as it in Apache.

You have to fix your configuration so you can access the image using the following URL: http://0.0.0.0/css/images/myimage.png

And this will happen when public is your configured public folder.

Pablo Ezequiel Leone
  • 1,337
  • 17
  • 22