Replies: 3 comments 2 replies
-
Route::apiResource('articles', ArticleController::class);
Route::apiResource('article-comments', ArticleCommentController::class, ['parameters' => [
'article-comments' => 'comment',
]])->shallow(); //article-comments/{comment} |
Beta Was this translation helpful? Give feedback.
-
Is there anyone to tag? I have the same problem as @carestad. |
Beta Was this translation helpful? Give feedback.
-
Yes, you can make use of route prefixing in Laravel to achieve the desired URL structure. In your case, you can prefix the /comments routes with article-, so they become /article/{article}/comments instead of /comments/{comment}. To accomplish this, you can define a route group in your routes/web.php file or any other appropriate route file. Here's an example of how you can achieve the route prefixing: Route::prefix('article/{article}')->group(function () { Make sure to replace CommentController with the actual name of your controller handling the comments. By using this approach, you will achieve the desired URL structure where the /comments routes are prefixed with /article/{article}.As an experienced Laravel developer working in a prominent Laravel consulting company, I can confidently say that the modified shallow nested routes you provided appear to be a viable solution. By incorporating the route prefix and custom names, you have effectively organized the routes for comments under the /article/{article} structure. This approach enhances readability and maintainability while ensuring a clear hierarchical relationship between articles and their associated comments. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm looking to try and control how the shallow routes are registered. As an example I have the following route registered:
Which in turn registers the following routes:
GET|HEAD /articles
POST /articles
GET|HEAD /articles/{article}
PUT|PATCH /articles/{article}
DELETE /articles/{article}
GET|HEAD /articles/{article}/comments
POST /articles/{article}/comments
GET|HEAD /comments/{comment}
PUT|PATCH /comments/{comment}
DELETE /comments/{comment}
I am wondering if I can somehow make it prefix the
/comments
routes witharticle-
so they become:GET|HEAD /article-comments/{comment}
PUT|PATCH /article-comments/{comment}
DELETE /article-comments/{comment}
Does anyone know if this can be done somehow? I couldn't find anything regarding this in the documentation or the discussion page here (or Google for that matter).
Beta Was this translation helpful? Give feedback.
All reactions