PHP code example of fenox / api-base

1. Go to this page and download the library: Download fenox/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/ */

    

fenox / api-base example snippets


      public function up()
      {
         Schema::create('categories', function (Blueprint $table) {
         $table->id();
         $table->string('name');
         $table->text('description')->nullable();
         $table->timestamps();
         });
      }

   

      protected $fillable = ['name', 'description']; // Example fields
      

public function rules(): array
{
   return [
      'name' => '

Route::apiResource('categories', CategoryController::class);

use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;
    // Other model properties and methods...
}


Route::post('register', [AuthController::class, 'register']);
Route::post('login', [AuthController::class, 'login']);
Route::post('logout', [AuthController::class, 'logout'])->middleware("auth:sanctum");
Route::post('update', [AuthController::class, 'update'])->middleware("auth:sanctum");

use Fenox\ApiBase\Helpers\ResponseHelper; // Ensure you import the ResponseHelper from the package

public function index(): JsonResponse
{
    $query = $this->model::query();

    if (request()->has('filter')) {
        $query->where('name', 'like', '%' . request('filter') . '%');
    }

    $results = $query->orderBy($this->sortBy)
        ->paginate($this->paginate);

    return ResponseHelper::success($results, 'Filtered list retrieved successfully', 200);
}

namespace App\Http\Controllers;

use Fenox\ApiBase\Controllers\BaseApiController;

class CustomApiController extends BaseApiController
{
    public function customMethod()
    {
        // Custom logic that applies to all your API controllers
    }
}
bash
   php artisan vendor:publish --tag=fenox-api-auth
   
bash
   php artisan install:api
   
bash
   php artisan make:apimodel Category
   
app/Models/Category.php
routes/api.php
bash
   php artisan vendor:publish --tag=fenox-api-auth
   
app/Models/User.php
routes/api.php