Laravel 5.5 Resistant API

I am trying to create an API for my application so that I can share the endpoint and have one application as the main application with business logic, and the other can communicate with the open endpoint in order to use the function as services.

I get an error when I try to get to the endpoint.

Below is my route /api.php

<?php use App\PostModell; use App\Http\Resources\PostModellResource; use Illuminate\Http\Request; Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); }); Route::get( '/cars',function(){ return new PostModellResource(PostModell::all()); }); 

My resource class looks like

 lass PostModellResource extends Resource { public function toArray($request) { return [ 'id'=>$this->id, 'title'=>$this->title, 'body'=>$this->body, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, ]; } 

Error

Sorry, the page you are looking for could not be found.

+5
source share
4 answers

use api prefix -

 127.0.0.1:8000/api/cars 

To transform a collection of resources you need to use the collection () method -

 return PostModellResource::collection(PostModell::all()); 
+3
source

With the route in api routes, use this URI:

 https://example.com/api/cars 

Also, since I am showing in my best practice repos you should not inject logic into routes , move all logic to the controller instead.

+3
source

if you use Laravel api.php then you should use api prefix,

Or, if you use Lumen web.php , then you can call it directly or you can define the api prefix according to your requirement.

in Laravel: localhost:8000/api/yoururl

in Lumen: localhost/yoururl

+1
source

All routes in api have a prefix path /api (default). Therefore, in your case, you must access it through: http: // YOURAPPURL / api / cars

You can check your App\Providers\ RouteServiceProvider@mapApiRoutes for more information.

+1
source

Source: https://habr.com/ru/post/1274918/


All Articles