-
Notifications
You must be signed in to change notification settings - Fork 42
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
Mark routes in router as JSON API routes #250
Comments
I am not fully expert in the Laravel routing at the moment, but does this also work when routes are cached with |
@ben221199 Good question. I haven't checked this yet but might do it later today. I will keep you updated! |
Okay, did a quick check. Currently it seems like routes from Laravel JSON api are not cached at all. Therefore it seems to be a bug, because caching my routes crashes my app ... |
This is my class RouteServiceProvider extends ServiceProvider {
public function boot() {
Route::prefix('api')
->middleware('api')
->group(function () {
$this->getJsonApiServer()->resources(function ( ResourceRegistrar $server ) {
$callback = require_once( base_path('routes/api/v1.php') );
if ( $callback instanceof \Closure ) {
$callback( $server );
}
});
});
}
private function getJsonApiServer(): PendingServerRegistration {
return (
ResourceRegistrar::server('v1')
->prefix('v1')
->namespace('\\')
);
}
} And my routes file ( return function ( ResourceRegistrar $server ) {
$server
->resource(
'access_tokens',
AccessTokenController::class
)
->only(
'store',
'show',
'destroy',
);
// ...
}; |
Okay, if we keep the ability to cache routes in mind, then my proposed approach will not work. But maybe it is possible to add a "tagged" "dummy middleware" and we can check the routes for the existence of this middleware. Via the "tag" we could also filter the version / server that defined a route. For example "jsonapi:v1" would tell that it is a route defined by JSON API and the "tag" would say, that it is from the server "v1". |
You mean, when using this extended class? They definitely do work for the current implementation, because I use cached routes in my production apps without any problems. I'd be intrigued to look into the Laravel route caching to see why an extended class doesn't work. Implies they're not serializing the objects directly but doing something else. |
@lindyhopchris no, when I run Anyway, I think there is no way to get the route class from the route cache file. But middlewares are included in the cache file and this might be a solution for this package. |
Currently there is no bulletproof way of filtering routes created by JSON API. But there might be a solution by adding JSON API routes as instances of a sub-class of
Illuminate\Routing\Route
. With this it would be easy to filter out routes from JSON API just by checking if a route is an instance of a JSON API route class or not. Or if such a route implements a contract from JSON API.It is quite simple:
Later we could filter out the json api routes like this:
The text was updated successfully, but these errors were encountered: