Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching routes #308

Closed
mgarciadelojo opened this issue Mar 10, 2016 · 14 comments
Closed

Caching routes #308

mgarciadelojo opened this issue Mar 10, 2016 · 14 comments

Comments

@mgarciadelojo
Copy link

Hi guys,

Is there any plan for caching routes? If not, I offer my help in order to implement it. I noticed that response times are a lot higher when routes are not cached.

Best regards,

@rtucek
Copy link
Contributor

rtucek commented Mar 10, 2016

No it's not possible in its current implementation.
The problem resists in that part of code in our routes file:

Route::group([

        'prefix' => LaravelLocalization::setLocale(),
        'middleware' => [
            'localeSessionRedirect',
            'localizationRedirect',
        ],

    ], function () {
        ...
    }
];

So when a HTTP request arrives, Laravellocalization::setLocale() will return something dynamically to prefix (a language code like 'en' for example). And depending on the next HTTP request you may get a different language code. So the prefix gets dynamically resolved.

If you call php artisan route:cache Laravel will only try to resolve once every route in one language combination and caches that result.

In order to achieve this we would need to generate every route combination foreach language.
Something like that would work possibly.

foreach(LaravelLocalization::getLanguageList() as $language) {

    Route::group([

            'prefix' => $language,
            'middleware' => [
                'localeSessionRedirect',
                'localizationRedirect',
            ],

        ], function () {
            ...
        }
    ];

}

And obviously such an implementation would require a major version update.

@mgarciadelojo
Copy link
Author

I tried with no luck before your message... the thing is I am translating the routes as well using the transRoute function...

@rjsworking
Copy link

@mgarciadelojo See this old issue #201

@lucasff
Copy link

lucasff commented Jul 20, 2016

This is a must. I'm having the same problem.

@scorpionick
Copy link

scorpionick commented Aug 9, 2016

I found a fix!!!!

routes.php

foreach(LaravelLocalization::getSupportedLocales() as $language => $properties) {
Route::group(
[
  'as' => $language.".",
  'middleware' => ['localeSessionRedirect', 'localizationRedirect'],
  'prefix' => $language,
],
function () use ($language){

...

});
}

Controller.php

class Controller extends BaseController
{
    use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
    public function __construct()
    {
      \LaravelLocalization::setLocale();
    }
}

vendor\laravel\framework\src\Illuminate\Foundation\helpers.php

if (! function_exists('route')) {
    /**
     * Generate a URL to a named route.
     *
     * @param  string  $name
     * @param  array   $parameters
     * @param  bool    $absolute
     * @return string
     */
    function route($name, $parameters = [], $absolute = true)
    {
        return app('url')->route(LaravelLocalization::getCurrentLocale().'.'.$name, $parameters, $absolute);
    }
}

Afterward php artisan route:cache works like a charm! ENJOY!

@rjsworking
Copy link

Hi. You should not change core files. See the solution in #201.
Cheers

@lucasff
Copy link

lucasff commented Aug 10, 2016

You can override 'route' function declaration so it gets loaded before the core one. Should be better this way.

@czim
Copy link
Contributor

czim commented Aug 26, 2016

If anyone's interested, I wrote a simple solution that works fine for our company's projects:

https://github.com/czim/laravel-localization-route-cache

This allows you to use artisan route:trans:cache to set a cache for all supported locales in separate files, and it loads the cached file corresponding to the relevant locale.

Additionally, you can list the routes for a specific locale with artisan route:trans:list {locale}.

It's not pretty, a bit of a hack-job (using a putenv() trick), but it doesn't mess with core files and it should be transparent to the rest of the app.

Your mileage may vary, but I very much welcome feedback & suggestions.

@rjsworking
Copy link

rjsworking commented Oct 18, 2016

@czim Tried your solution but found a problem.
When a user types site.com he's redirected to site.com/en or whatever the default locale is. With your soltution the redirect doesn't happen. It only works ok if the user types site.com/en.

Cheers

@czim
Copy link
Contributor

czim commented Oct 18, 2016

Can you make an issue out of it in my repo, with some snippets from the routes file, so I know how you've set it up? I'll try to reproduce and fix it then.

@rjsworking
Copy link

@czim Thank you. Sure I will. Let me get some time to perform another test with a fresh project and then I create the issue if needed.

Cheers

@czim
Copy link
Contributor

czim commented Nov 18, 2016

The problem mentioned by rsdev000 has been fixed in a new version, thanks to MrVokia.

@nicklasos
Copy link

@mcamara Hey! You should merge this package https://github.com/czim/laravel-localization-route-cache

What do you think about it?

@mcamara
Copy link
Owner

mcamara commented Oct 16, 2017

I've added a section in the README.md redirecting the user to czim's package. Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants