1. Go to this page and download the library: Download fadugyamfi/laravel-api-base library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
/**
* Important change to ensure the right router version is used with the Laravel API Base package
*/
$app->singleton('router', LaravelApiBase\Services\ApiRouter::class);
GET /todos // Gets a paginated list to Todo items
GET /todos/{id} // Get a specific Todo item by ID
POST /todos // Create a new Todo record
PUT /todos/{id} // Update a Todo record
DELETE /todos/{id} // Delete a Todo record
GET /todos?limit=5 // Get the first 5 items
GET /todos?page=3&limit=3 // Get page 3 of items with only 3 items
GET /todos?title=My Todo // Get all Todos with a title "My Todo"
GET /todos?description_like=Call // Returns any todos with the word "call" in the description
GET /todos?id_gt=5&title_like=John // Gets all todos with an ID greater than 5 and have "John" in the title
GET /todos?id_in=1,3,5 // Gets a specific set of todos by ID
GET /todos?description_isNull // Get all Todos with a NULL description
GET /todos?contain=subtask // Get all Todos with associated subtasks
GET /todos?contain=subtask.assignee // Get all Todos with subtasks and subtask assignee
GET /todos?contain=user,subtask // Get all Todos with associated users and subtasks
// Counting associated models
GET /todos?count=subtask // Returns a `subtask_count` property in returned results
// Returning Associated Models in response after Creating or Updating a Resource
POST /todos?contain=subtask // Returns a subtask property in the response
PUT /todos/{id}?contain=subtask.assignee // Returns a subtask property with its assignee property in the response
// Return associations from models with longer names
GET /todos?contain=todo-category // Underscore or Hyphens are both supported
GET /todos?sort=id:desc // Get results sorted by ID Desc
GET /todos?sort=id:desc,title:asc // Sort by multiple columns
namespace App\Models;
use LaravelApiBase\Models\ApiModel;
class Todo extends ApiModel
{
protected $table = 'todos';
protected $fillable = ['title', 'description'];
public function user() {
return $this->belongsTo(User::class);
}
public function subtask() {
return $this->hasMany(Subtask::class);
}
public function todoCategory() {
return $this->belongsTo(TodoCategory::class);
}
}
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use LaravelApiBase\Models\ApiModelInterface;
use LaravelApiBase\Models\ApiModelBehavior;
class User extends Authenticatable implements ApiModelInterface
{
use Notifiable, ApiModelBehavior;
// ...
}
namespace App\Observers;
class TodoObserver
{
public function sendReminder(Todo $todo) {
// some logic to send a reminder
}
/**
* Sends a reminder when a new Todo is added
*/
public function created(Todo $todo) {
$this->sendReminder($todo);
}
/**
* Appends a timestamp at the end of the description
*/
public function updating(Todo $todo) {
$todo->description = $todo->description . ' Updated at ' . date('Y-m-d H:i:s');
}
}
namespace App\Providers;
use App\Models\Todo;
use App\Observers\TodoObserver;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
Todo::observe(TodoObserver::class);
}
}
namespace App\Http\Requests;
use LaravelApiBase\Http\Requests\ApiFormRequest;
class TodoRequest extends FormRequest implements ApiFormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'k',
'example' => 'Remember to publish library code'
]
];
}
}
namespace App\Http\Resources;
use LaravelApiBase\Http\Resources\ApiResource;
class TodoResource extends ApiResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
$url = 'https://via.placeholder.com/150';
return array_merge(parent::toArray($request), [
'url' => $url
]);
}
}
namespace App\Http\Controllers;
use App\Models\Todo;
use LaravelApiBase\Http\Controllers\ApiControllerBehavior;
/**
* By default, this Controller will locate the `App\Http\Requests\TodoRequest` and `App\Http\Resources\TodoResource`
* classes and use them for Request Validation and Response Formatting.
*/
class TodoController extends Controller
{
use ApiControllerBehavior;
public function __construct(Todo $todo) {
$this->setApiModel($todo);
}
// you can add additional methods here as needed and connect them in your routes file
/**
* Hypothetical Method To Return Subtasks
*/
public function subtasks(Request $request, $id) {
$subtasks = Todo::find($id)->subtasks;
return $this->Resource::collection($subtasks);
}
}
namespace App\Http\Controllers;
use App\Models\Todo;
use App\Http\Requests\Specials\SpecialTodoRequest;
use App\Http\Resources\Specials\SpecialTodoResource;
use LaravelApiBase\Http\Controllers\ApiController;
class TodoController extends Controller
{
use ApiControllerBehavior;
public function __construct(Todo $todo) {
$this->setApiModel($todo);
$this->setApiFormRequest(SpecialTodoRequest::class);
$this->setApiResource(SpecialTodoResource::class);
}
}
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
// Add a special endpoint for returns subtasks of a Todo. These must come BEFORE the apiResource call
Route::get('todos/{id}/subtasks', 'TodoController@subtasks');
// adds all the basic endpoints for GET, POST, PUT, DELETE as well as /search and /count
Route::apiResource('todos', 'TodoController');
php artisan scribe:generate
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.