Skip to content
Rougin Gutib edited this page Dec 15, 2023 · 32 revisions

Slytherin is a simple and extensible PHP micro-framework that tries to achieve a SOLID-based design for creating your next web application. It uses Composer as the dependency package manager to add, update or even remove external packages.

Background

In the current state of PHP ecosystem, the mostly used PHP frameworks are Symfony and Laravel which provide great tools for every PHP software engineer. While the said PHP frameworks provide a kitchen-sink solution for every need (e.g., content management system (CMS), CRUD, etc.), they are often overkill, overwhelming at first, or sometimes uses a strict directory structure.

With this, Slytherin tries an alternative approach to only require the basic tools (e.g., HTTP and Routing) and let the application evolve from a simple API tool to a full-featured web application. With no defined directory structure, every PHP software engineer can use Slytherin to mix and match any structure they like and to tap more of the open-source packages in the PHP ecosystem.

Installation

Install Slytherin through Composer:

$ composer require rougin/slytherin

Basic Example

Below is an example code for creating a simple Slytherin-based application:

// app/web/index.php

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Application;
use Rougin\Slytherin\Container\Container;
use Rougin\Slytherin\Http\Response;
use Rougin\Slytherin\Http\ServerRequest;
use Rougin\Slytherin\Routing\Dispatcher;
use Rougin\Slytherin\Routing\DispatcherInterface;
use Rougin\Slytherin\Routing\Router;

require 'vendor/autoload.php';

// Define HTTP objects that is compliant to PSR-07 standards ---
$request = new ServerRequest((array) $_SERVER);

$response = new Response(http_response_code());
// -------------------------------------------------------------

// Create a new router to add an HTTP route... ---
$router = new Router;

$router->get('/', function ()
{
    return 'Hello world!';
});
// -----------------------------------------------

// ...then define it to a dispatcher ---
$dispatcher = new Dispatcher($router);
// -------------------------------------

// Add the above objects through a container ------------
$container = new Container;

$container->set(ServerRequestInterface::class, $request);

$container->set(ResponseInterface::class, $response);

$container->set(DispatcherInterface::class, $dispatcher);
// ------------------------------------------------------

// Lastly, run the application ------
(new Application($container))->run();
// ----------------------------------

Then run the application using the PHP's built-in web server:

$ php -S localhost:8000 -t app/web

Note

The above example might be overwhelming at first, but this will be explained and can be improved in the succeeding guides.

Credits

Slytherin is inspired by the following packages below and their respective implementations. Without them, there would be no Slytherin: