Api: [L5.1] Route Caching

Created on 17 Jun 2015  ·  18Comments  ·  Source: dingo/api

It seems that route caching and dingo/api doesn't work.
We have this route:

$api = app('api.router');

$api->version('v1', ['prefix' => 'api'], function($api)
{
    $api->post('login', 'App\Http\Controllers\APIUserController@login');
});

And we got the following when route caching is enabled:

{"message":"The version given was unknown or has no registered routes.","status_code":400,"debug":
{"line":51,"file":"\../vendor\/dingo\/api\/src\/Routing\/Adapter\/Laravel.php","class":"Dingo\\Api\\Exception
\\UnknownVersionException","trace":["#0/dingo\/api\/src\/Routing\/Router.php(526): Dingo\\Api\\Routing
\\Adapter\\Laravel->dispatch(Object(Dingo\\Api\\Http\\Request), 'v1')","#1 \vendor\/dingo\/api\/src\/Http
\/Middleware\/Request.php(91): Dingo\\Api\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http
\\Request))","#2 [internal function]: Dingo\\Api\\Http\\Middleware\\Request->Dingo\\Api\\Http\\Middleware
\\{closure}(Object(Dingo\\Api\\Http\\Request))","#3 /bootstrap\/cache\/compiled.php(9392): 
call_user_func(Object(Closure), Object(Dingo\\Api\\Http\\Request))","#4 

Clearing the route cache fixes it.

Most helpful comment

Did I not update this? My bad! Okay, with Laravel 5.1 you can now cache your API routes as well as your Laravel routes. The command is as follows:

php artisan api:cache

To make this work I've had to have the command run the frameworks route:cache command as well. So you'll end up with a cached routes file that contains a call to set the framework routes and a call to set the API routes.

This is also only available on Laravel. Not Lumen.

All 18 comments

5.0 isn't supported by dingo.

5.1 sorry
On Jun 17, 2015 1:15 AM, "Graham Campbell" [email protected] wrote:

5.0 isn't supported by dingo.


Reply to this email directly or view it on GitHub
https://github.com/dingo/api/issues/468#issuecomment-112712013.

I am yet to test with route caching. Will check it out.

For now my suggestion is to define API routes in a separate routes.php file, perhaps an api.php file. Then in your RouteServiceProvider::boot method simply require this file manually.

Thanks, let me know if I can help
On Jun 17, 2015 8:03 PM, "Jason Lewis" [email protected] wrote:

I am yet to test with route caching. Will check it out.


Reply to this email directly or view it on GitHub
https://github.com/dingo/api/issues/468#issuecomment-113022703.

We actually have multiple route files that we include. So we don't have a single routes.php file.

But you include them from within routes.php, right @jrm2k6?

yep!

we have something like that:

foreach (File::allFiles(__DIR__.'/Routes') as $partial)
    {
        require $partial->getPathName();
    }

Okay cool, that's all good. For now just include your API routes separately from the provider. The only reason it throws the error is because once you cache the routes the original routes.php file is never loaded so the API routes are never registered.

You register the apis route later in the process? How do the api routes
registration works in few sentences?

On Thu, Jun 18, 2015 at 3:47 PM, Jason Lewis [email protected]
wrote:

Okay cool, that's all good. For now just include your API routes
separately from the provider. The only reason it throws the error is
because once you cache the routes the original routes.php file is never
loaded so the API routes are never registered.


Reply to this email directly or view it on GitHub
https://github.com/dingo/api/issues/468#issuecomment-113307880.

Jérémy Dagorn
www.jeremydagorn.com

Android apps I developed:
https://play.google.com/store/apps/developer?id=JumyApps

They don't get registered later in the process. They just don't get cached.

In a nutshell, all API routes are registered with the API router and not the Laravel/Lumen router. This is so we can do a few things such as have duplicate URIs for different versions.

So, because they're not registered with the Laravel router they aren't cached with the Laravel routes. Once you have cached your Laravel routes the routes.php file is never included again. So, to still have your API routes registered, you can edit app/Providers/RouteServiceProvider.php and simply require an api.php file that contains your API routes.

Sorry for the delay in getting back to you.
I will check your solution and see how it goes.

Thanks!

Did I not update this? My bad! Okay, with Laravel 5.1 you can now cache your API routes as well as your Laravel routes. The command is as follows:

php artisan api:cache

To make this work I've had to have the command run the frameworks route:cache command as well. So you'll end up with a cached routes file that contains a call to set the framework routes and a call to set the API routes.

This is also only available on Laravel. Not Lumen.

So can the api routes live inside of the main routes now? Or should we still be loading from the routes SP?

Yeah main routes file is fine.

On Sun, 28 Jun 2015 13:22 Tom Schlick [email protected] wrote:

So can the api routes live inside of the main routes now? Or should we
still be loading from the routes SP?


Reply to this email directly or view it on GitHub
https://github.com/dingo/api/issues/468#issuecomment-116188092.

Awesome. Thanks!

On Sat, Jun 27, 2015 at 11:29 PM Jason Lewis [email protected]
wrote:

Yeah main routes file is fine.

On Sun, 28 Jun 2015 13:22 Tom Schlick [email protected] wrote:

So can the api routes live inside of the main routes now? Or should we
still be loading from the routes SP?


Reply to this email directly or view it on GitHub
https://github.com/dingo/api/issues/468#issuecomment-116188092.


Reply to this email directly or view it on GitHub
https://github.com/dingo/api/issues/468#issuecomment-116189428.

i know this is late but how do i clear the api route cache?

Simply run php artisan route:clear command.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lloricode picture lloricode  ·  3Comments

fengerwoo picture fengerwoo  ·  4Comments

Sogl picture Sogl  ·  4Comments

MicroDroid picture MicroDroid  ·  3Comments

nghiepit picture nghiepit  ·  4Comments