-
Notifications
You must be signed in to change notification settings - Fork 4
Home
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:
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 can be improved in the succeeding guides.
Slytherin is inspired by the following packages below and their respective implementations. Without them, there would be no Slytherin: