A Controller with an API
$laravel new simpleAPI
$cd simpleAPI
$php artisan make:controller dummyAPI
$code app/Http/Controllers/dummyAPI.php
app/Http/Controllers/dummyAPI.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class dummyAPI extends Controller
{
function getData(){
return ["name"=>"alice"];
}
}
routes/api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\dummyAPI;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::get("data",[dummyAPI::class,'getData']);
$php artisan serve
Get http://127.0.0.1:8000/api/data on Postman or Browser
the output
{
"name": "alice"
}
Controller plus Model and API
create database and table, use mysql as example
mysql> create database dummy;
$php artisan make:model Dummy -rmc
Model created successfully.
Created Migration: 2021_02_09_094710_create_dummies_table
Controller created successfully.
modify database/migrations/2021_02_09_094710_create_dummies_table.php
public function up()
{
Schema::create('dummies', function (Blueprint $table)
{
$table->id();
$table->string('name');
$table->timestamps();
});
}
migrate and insert data
$php artisan migrate
mysql> use dummy;
mysql> insert into dummies(name) values ('dummy1');
mysql> insert into dummies(name) values ('dummy2');
modify routes/api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DummyController;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::get("data",[DummyController::class,'show']);
run php artisan serve
Get http://127.0.0.1:8000/api/data on Postman or Browser
[
{
"id": 1,
"name": "dummy1",
"created_at": null,
"updated_at": null
},
{
"id": 2,
"name": "dummy2",
"created_at": null,
"updated_at": null
}
]
CRUD API
CREATE
edit
app/Http/Controllers/DummyController.php
//add this to top of the code
use Symfony\Component\HttpFoundation\Response as Res;
...
public function store(Request $request)
{
$dummy = Dummy::create($request->all());
$dummy = $dummy->refresh();
return response($dummy,Res::HTTP_CREATED);
}
edit app/Models/Dummy.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class dummy extends Model
{
use HasFactory;
protected $fillable = [
'name'
];
}
add
Route::apiResource('dummy',DummyController::class);
(optional) Install REST client on VS code
create dummy.http
POST http://127.0.0.1:8000/api/dummy HTTP/1.1
content-type:application/json
{
"name":"henry"
}
click send request
//result
{
"id": 3,
"name": "henry",
"created_at": "2021-02-10T04:15:28.000000Z",
"updated_at": "2021-02-10T04:15:28.000000Z"
}
DELETE
edit app/Http/Controllers/DummyController.php
public function destroy(dummy $dummy)
{
$dummy->delete();
return response(null, RES::HTTP_NO_CONTENT);
}
send request in dummy.http
DELETE http://127.0.0.1:8000/api/dummy/1
UPDATE
edit app/Http/Controllers/DummyController.php
public function update(Request $request, dummy $dummy)
{
$dummy->update($request->all());
return response($dummy,RES::HTTP_OK);
}
send request in dummy.http
PATCH http://127.0.0.1:8000/api/dummy/2 HTTP/1.1
content-type:application/json
{
"name":"the modify one"
}
READ
- read single data
edit app/Http/Controllers/DummyController.php
public function show(dummy $dummy)
{
return response($dummy,RES::HTTP_OK);
}
send request in dummy.http
GET http://127.0.0.1:8000/api/dummy/2
content-type:application/json
- read all data
edit app/Http/Controllers/DummyController.php
public function index()
{
$dummy = Dummy::get();
return response(['data' => $dummy], Res::HTTP_OK);
}
send request in dummy.http
GET http://127.0.0.1:8000/api/dummy
content-type:application/json
Top comments (0)