API Module : Generating Token / Extending Routes

API Module : Generating Token / Extending Routes

kiltedup -
1 year ago

Hi,
I have started to play around with the API Module. Stuck with a couple of things.
Generating a token:
I followed some comments on Slack re using 'tinker' to generate a token, however now looking to see how to do it properly. Assume the missing image in github holds they key to what needs to be done. Anyone know how to generate a token?
Extending routes:
I would like to add my own routes. Primarily because I have a stream that has a bunch of relationship fields so rather than just seeing the id of in the API entry response, would like to return a custom response for certain routes. Struggling to find documentation on how to extend the routes - probably not just for API module but any core module in general. Any cunning tips out there?

Ta
Dave

Answer

ryanthompson -
1 year ago

Ah I remember something here :-/

So if you route foo/bar the resulting route will be api/foo/bar so if you are prefixing with api you are looking at api/api. I think that's what's going on.

I should pry remove that? Idk.. thoughts? It's quite misleading and pretty intrusive. Let me know what you think and I can remove it right now.

ryanthompson -
1 year ago

The fastest way to get a token is personal access tokens (in Tinker or wherever):

app(ApiUser::class)->createToken('Token Name')->accessToken;

Then plug that into your Authorization header.

The other methods require more setup and are better suited for providing others a way to gain such access.

ryanthompson -
1 year ago

As far as routing the API - I would not extend in this case I would create your own API end points.

In the case of the latter it's super easy to do with the API module. Simply define routes in an addon service provider using protected $api = []; just as you would for the $routes param.

You can also route just like the Laravel docs show you for the Passport package since that's the auth system behind this addon.

For extending routes - it's only doable with named routes (cleanly anyways). And all you would do define a route with the same name in /resources/{REF}/routes.php. It's really replacing but.. I get what you wanna do I think.

kiltedup -
1 year ago

Struggling with this.
So I add new module and in service provider I add
protected $api = [ 'testmodule/testendpoint' => 'Sandbox\TestModule\Http\Controller\TestController@test', ];
What is TestController extending? Assumed 'ApiController' but guess that is wrong. Played around with PublicController and ResourceController and various other random ideas from look at API Module.

404 abounds whatever I do.

Are there any examples anywhere of how to add a custome api endpoint in a module?

kiltedup -
1 year ago

ryanthompson -
1 year ago

kiltedup -
1 year ago

Same as always :
Type error: Argument 2 passed to Barryvdh\Debugbar\LaravelDebugbar::modifyResponse() must be an instance of Symfony\Component\HttpFoundation\Response, null given, called in /Applications/MAMP/htdocs/PyroCMS/hup234design.co.uk/vendor/barryvdh/laravel-debugbar/src/Middleware/Debugbar.php on line 60