PHP code example of jissanto / rapidrest

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

    

jissanto / rapidrest example snippets




apidRest\Application;
use RapidRest\Http\Request;
use RapidRest\Http\Response;

$app = new Application();

// Add a route with a path parameter
$app->get('/hello/{name}', function (Request $request, string $name) {
    return (new Response())
        ->withJson([
            'message' => "Hello, $name!"
        ]);
});

// Run the application
$app->run();

class CreateUsersTable extends Migration
{
    public function up(): void
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('users');
    }
}

class User extends Model
{
    protected static string $table = 'users';

    protected array $fillable = [
        'name',
        'email',
        'password',
    ];

    protected array $hidden = [
        'password',
    ];

    public function posts(): array
    {
        return $this->hasMany(Post::class);
    }

    public function profile(): Profile
    {
        return $this->hasOne(Profile::class);
    }
}

// Basic queries
$users = User::query()
    ->where('status', '=', 'active')
    ->orderBy('created_at', 'DESC')
    ->get();

// Complex queries
$users = User::query()
    ->select(['users.*', 'profiles.bio'])
    ->join('profiles', 'users.id', '=', 'profiles.user_id')
    ->where('users.is_active', '=', true)
    ->whereIn('users.role', ['admin', 'moderator'])
    ->limit(10)
    ->get();

// GET request
$app->get('/users', function (Request $request) {
    return (new Response())->withJson(['users' => []]);
});

// POST request with JSON body
$app->post('/users', function (Request $request) {
    $data = $request->getParsedBody();
    return (new Response())
        ->withJson(['message' => 'User created'], 201);
});

// Path parameters
$app->get('/users/{id}', function (Request $request, string $id) {
    return (new Response())->withJson(['id' => $id]);
});

class AuthMiddleware implements MiddlewareInterface
{
    public function process(Request $request, callable $handler): Response
    {
        $token = $request->getHeader('Authorization');
        if (!$token) {
            return new Response(401, [], ['error' => 'Unauthorized']);
        }
        
        // Verify JWT token
        try {
            $user = $this->jwt->verify($token);
            $request = $request->withAttribute('user', $user);
            return $handler($request);
        } catch (Exception $e) {
            return new Response(401, [], ['error' => 'Invalid token']);
        }
    }
}

$app->use(new AuthMiddleware());

$rules = [
    'name' => 'email|unique:users',
    'password' => 'te($data, $rules);
if (!empty($errors)) {
    return $this->error($errors, 422);
}