PHP code example of caiquebispo / laraslim

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

    

caiquebispo / laraslim example snippets


use LaraSlim\Kernel\Providers\Response;

// Success response
return response()->success($data, 'Created', 201);

// Error response
return response()->error('Not found', 404);

// Validation error
return response()->validationError($errors);

// Paginated response
return response()->paginate($items, $total, $page, $perPage);

use LaraSlim\Kernel\Exceptions\HttpException;

// Throw HTTP exception
throw HttpException::notFound('User not found');
throw HttpException::unauthorized();

// Or use helpers
abort(404, 'Resource not found');
abort_if($condition, 403, 'Forbidden');

use Slim\Routing\RouteCollectorProxy;

$app->group('/api', function (RouteCollectorProxy $group) {
    $group->get('/users', 'UserController:index');
    $group->post('/users', 'UserController:store');
});



namespace LaraSlim\Http\Controllers;

use LaraSlim\Services\UserServices;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class UserController
{
    public function __construct(private UserServices $userServices) {}

    public function store(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
    {
        $data = $request->getParsedBody();
        $user = $this->userServices->store($data);

        return response($response)->created($user, 'User created successfully');
    }
}
bash
php artisan-slim make:migration users
php artisan-slim make:model User
php artisan-slim make:controller UserController
php artisan-slim make:request UserRequest
php artisan-slim          # List all commands