API Module : Generating Token / Extending Routes

kiltedup - 8 months 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 - 8 months 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 - 8 months 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 app(ApiUser::class)->createToken('Token Name')->accessToken; header.

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

ryanthompson - 8 months 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.

Hope this helps!

kiltedup - 8 months ago

Struggling with this. So I add new module and in service provider I add protected $api = [ 'testmodule/testendpoint' => 'Sandbox\TestModule\Http\Controller\[email protected]', ]; 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?

Ta :-)

ryanthompson - 8 months ago

Extend: Anomaly\Streams\Platform\Http\Controller\ResourceController

kiltedup - 8 months ago

Hmmm - that is what I'm currently extending.

Anything else needed in the service provider over and above protected $api .....

ryanthompson - 8 months ago

What's it kicking back with?

kiltedup - 8 months ago

  1. 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

ryanthompson - 8 months 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.

stevenweijdt - 8 months ago

api/api definitily no. Better to let the dev decide.

kiltedup - 8 months ago

And adding the prefix 'api/' means I am shortly going to start drinking beer. Thanks man.

I think I personally would remove it - although in saying that, it keeps the routes in line with actual module.

ryanthompson - 8 months ago

@kiltedup take note man - Im removing it now. So your route is your route - as it should be!

kiltedup - 8 months ago

makes sense. cheers :-)

kiltedup - 8 months ago

Hey Ryan, Just did an update and dropped the api prefix - getting "Call to undefined method Illuminate\Database\Query\Builder::withAccessToken()"