Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 localization: exclude /public/ directory

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?

like image 826
watzon Avatar asked Nov 10 '22 15:11

watzon


1 Answers

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.

like image 198
Pablo Ezequiel Leone Avatar answered Nov 15 '22 12:11

Pablo Ezequiel Leone