caiquebispo/laraslim

A lightweight PHP microframework combining Slim and Laravel features for fast and structured API development.

1.3.2 2025-04-19 14:24 UTC

This package is auto-updated.

Last update: 2025-04-19 22:35:29 UTC


README

LaraSlim Logo

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Description

LaraSlim is a microframework for PHP that combines the lightness of the Slim Framework with a structure inspired by Laravel. It's ideal for creating clean, modular APIs with a lightweight and fast setup.

Note: This project does not include an authentication engine by default. You can integrate your own or use third-party libraries.

It also doesn't include a template engine, but you can integrate it with Blade (Laravel) or any other engine of your choice.

⚠️ This is a beta project: some features may be under development and bugs may occur.

Requirements

  • PHP ^8.4
  • Docker
  • Composer

Installation via Composer

composer create-project caiquebispo/laraslim example_app

Manual Installation

  1. Clone the repository:

    git clone https://github.com/caiquebispo/LaraSlim.git
    cd LaraSlim
  2. Build Docker containers:

    sh build
  3. Start the Docker containers:

    sh up
  4. Attach the Docker containers:

    sh attach
  5. Down the Docker containers:

    sh down
  6. Install dependencies:

    composer install
  7. Copy the .env file:

    cp .env.exemple .env
  8. Run tests and checks:

    composer test
  1. Show all available commands:
    php artisan-slim

Application Access

  • API: http://localhost:8003
  • PHPMyAdmin: http://localhost:8080
    • User: root
    • Password: root

Project Structure

  • app/Http/Controllers: Application controllers.
  • app/Http/Request: Form validation files.
  • app/Models: Data models.
  • app/DTOs: Data Transfer Objects.
  • app/Services: Business logic layer.
  • app/Kernel: Configuration and setup files.
  • database/migrations: Database migration files.
  • public: Web-accessible files (e.g., index.php).
  • composer.json: Composer configuration.

Database Configuration

Configure your .env file as needed. Example using SQLite:

DB_CONNECTION="sqlite"
DB_HOST=""
DB_DATABASE="../database/skeleton_db.sqlite"
DB_USERNAME=""
DB_PASSWORD=""

Useful Commands

Create a new migration:

php artisan-slim make:migration users

Create a new model:

php artisan-slim make:model User

Create a new controller:

php artisan-slim make:controller UserController

Create a new form request:

php artisan-slim make:request UserRequest

Migrations are automatically executed when the container is started.

Examples

Route Group

use Slim\Routing\RouteCollectorProxy;

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

Simple Route

use LaraSlim\Controllers\HomeController;

$app->get('/', [HomeController::class, 'index']);

Basic Controller

<?php

namespace LaraSlim\Controllers;

class HomeController
{
    public function index()
    {
        // Controller logic here
    }
}

Form Request

<?php

namespace LaraSlim\Http\Request;

class UserRequest extends BaseRequest
{
    protected function rules(): array
    {
        return [
            //'name' => 'required|string|max:255',
        ];
    }

    protected function messages(): array
    {
        return [
            //'email.required' => 'The email field is required.',
        ];
    }
}

Using Form Request in Controller

<?php

namespace LaraSlim\Http\Controllers;

use LaraSlim\DTOs\UserDTO;
use LaraSlim\Http\Request\UserRequest;
use LaraSlim\Kernel\Providers\Response;
use LaraSlim\Services\UserServices;

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

    public function store(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
    {
        $validator = (new UserRequest($request->getParsedBody()))->validate();

        if ($validator->fails()) {
            return (new Response($response))->json([
                'status' => 'error',
                'message' => 'Validation failed',
                'errors' => $validator->errors()
            ], 422);
        }

        $user = $this->userServices->store(new UserDTO(...$request->getParsedBody()));

        return (new Response($response))->json([
            'status' => 'success',
            'message' => 'User created successfully',
            'user' => $user
        ], 201);
    }
}

Author

License

This project is licensed under the terms of the MIT License.