2222
2323### API Route Prefix
2424
25- When registering a JSON API, we automatically read the URL prefix and route name prefix from your
26- [ API's URL configuration] ( ./api#url ) and apply this to the route group for your API. The URL prefix in your JSON API
25+ When registering a JSON API, we automatically read the URL prefix and route name prefix from your
26+ [ API's URL configuration] ( ./api#url ) and apply this to the route group for your API. The URL prefix in your JSON API
2727config is ** always** relative to the root URL on a host, i.e. from ` / ` .
2828** This means when registering your routes, you need to ensure that no prefix has already been applied.**
2929
@@ -42,7 +42,7 @@ JsonApi::register('default')->withNamespace('Api')->routes(function ($api) {
4242```
4343
4444> We use ` withNamespace() ` instead of Laravel's usual ` namespace() ` method because ` namespace ` is a
45- [ Reserved Keyword] ( http://php.net/manual/en/reserved.keywords.php ) .
45+ [ Reserved Keyword] ( http://php.net/manual/en/reserved.keywords.php ) .
4646
4747## Resource Routes
4848
@@ -67,6 +67,16 @@ JsonApi::register('default')->routes(function ($api) {
6767});
6868```
6969
70+ By default the resource type is used as the URI fragment: i.e. the ` posts ` resource will have a URI of
71+ ` /posts ` . If you want to use a different URI fragment, use the ` uri() ` method. In the following example,
72+ the resource type is ` posts ` but the URI will be ` /blog_posts ` :
73+
74+ ``` php
75+ JsonApi::register('default')->routes(function ($api) {
76+ $api->resource('posts')->uri('blog_posts');
77+ });
78+ ```
79+
7080## Relationship Routes
7181
7282The JSON API spec also defines routes for relationships on a resource type. There are two types of relationships:
@@ -87,6 +97,19 @@ JsonApi::register('default')->routes(function ($api) {
8797});
8898```
8999
100+ By default the relationship name is used as the URI fragment: i.e. for the ` comments ` relationship on the
101+ ` posts ` resource, the related resource URI is ` /posts/{record}/comments ` . To customise the URI framgent,
102+ use the ` uri() ` method. In the following example, the relationship name is ` comments ` but the URI
103+ will be ` /posts/{record}/blog_comments ` :
104+
105+ ``` php
106+ JsonApi::register('default')->routes(function ($api) {
107+ $api->resource('posts')->relationships(function ($relations) {
108+ $relations->hasMany('comments')->uri('blog_comments');
109+ });
110+ });
111+ ```
112+
90113### Related Resource Type
91114
92115When registering relationship routes, it is assumed that the resource type returned in the response is the
@@ -252,7 +275,7 @@ JsonApi::register('default')->withNamespace('Api')->routes(function ($api, $rout
252275
253276### Controller Names
254277
255- If you call ` controller() ` without any arguments, we assume your controller is the camel case name version of
278+ If you call ` controller() ` without any arguments, we assume your controller is the camel case name version of
256279the resource type with ` Controller ` on the end. I.e. ` posts ` would expect ` PostsController ` and
257280` blog-posts ` would expect ` BlogPostsController ` . Or if your resource type was ` post ` ,
258281we would guess ` PostController ` .
@@ -332,7 +355,7 @@ If you are using these, you will also need to refer to the *Custom Actions* sect
332355
333356Also note that custom routes are registered * before* the routes defined by the JSON API specification,
334357i.e. those that are added when you call ` $api->resource('posts') ` . You will need to ensure that your
335- custom route definitions do not collide with these defined routes.
358+ custom route definitions do not collide with these defined routes.
336359
337360> Generally we advise against registering custom routes. This is because the JSON API specification may
338361 have additional routes added to it in the future, which might collide with your custom routes.
@@ -396,7 +419,7 @@ does not contain an `@` symbol we add the controller name to it.
396419
397420Secondly, if you are defining a custom relationship route, you must use the ` field ` method. This takes
398421the relationship name as its first argument. The inverse resource type can be specified as the second argument,
399- for example:
422+ for example:
400423
401424``` php
402425JsonApi::register('default')->withNamespace('Api')->routes(function ($api) {
0 commit comments