Reputation: 1131
There are several questions on here that are similar, but not that are really providing exactly what I need.
I am creating a simple pet project in Laravel 5.3 that uses https://xboxapi.com/ to pull in JSON of Xbox Games. At first I was going to create a Game model, but I understand that because I am using 3rd party, I really won't be pulling any data in from my own DB and there's really no need to use Eloquent ORM at this time.
What is the "Laravel way" to do this? I can use Guzzle to hit the API, and create methods to get game by title, get game by publisher, etc. I guess what I am asking is where does this go within my app's file structure? I understand there may be no right or wrong answer. Really just looking for some insight into improving my OOP concepts.
Edit
Should I just put this into a GamesController? I don't want to have Guzzle in my controller, right?
Or should I actually create a Game class that extends the Eloquent model, and then overwrite methods like ::all() and ::find() to hit the API rather than the applications database?
Thanks ahead of time!
Upvotes: 0
Views: 1342
Reputation: 50798
Yes. Use Guzzle. The PSR-7
spec has implemented 3 RFC
's that dictate how HTTP request objects should be handled.
Guzzle does have PSR-7 support, you can see the git repository here.
For API based CRUD requests, create a new Controller namespace and move your API logic there. Consume your own API's internally if you need through these external end points:
artisan make:controller Api\\UserController
Now update your RouteServiceProvider
, and add a new declaration:
private $api_namespace = 'App\Http\Controllers\Api`.
Now update your mapApiRoutes()
and change your namespace:
'namespace' => $this->api_namespace
Now all of your API requests should route to Api\Controllers
.
Upvotes: 2