Skip to content
/ Slim Public
forked from slimphp/Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.

License

Notifications You must be signed in to change notification settings

j0k3r/Slim

Repository files navigation

Slim Framework

Build Status Coverage Status Total Downloads License

Slim is a PHP micro-framework that helps you quickly write simple yet powerful web applications and APIs.

Installation

It's recommended that you use Composer to install Slim.

$ composer require slim/slim:4.0.0-alpha

This will install Slim and all required dependencies. Slim requires PHP 7.1 or newer.

Choose a PSR-7 Implementation & ServerRequest Creator

Before you can get up and running with Slim you will need to choose a PSR-7 implementation that best fits your application. A few notable ones:

  • Slim-Psr7 - This is the Slim Framework projects PSR-7 implementation.
  • Nyholm/psr7 & Nyholm/psr7-server - This is the fastest, strictest and most lightweight implementation at the moment
  • Guzzle/psr7 & http-interop/http-factory-guzzle - This is the implementation used by the Guzzle Client. It is not as strict but adds some nice functionality for Streams and file handling. It is the second fastest implementation but is a bit bulkier
  • zend-diactoros - This is the Zend implementation. It is the slowest implementation of the 3.

Slim-Http Decorators

Slim-Http is a set of decorators for any PSR-7 implementation that we recommend is used with Slim Framework.

Hello World using AppFactory with PSR-7 auto-detection

In order for auto-detection to work and enable you to use AppFactory::create() and App::run() without having to manually create a ServerRequest you need to install one of the following implementations:

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Slim\Middleware\ErrorMiddleware;

require __DIR__ . '/../vendor/autoload.php';

// Instantiate App
$app = AppFactory::create();

// Add error middleware
$responseFactory = $app->getResponseFactory();
$errorMiddleware = new ErrorMiddleware($app->getCallableResolver(), $responseFactory, true, true, true);
$app->add($errorMiddleware);

// Add route
$app->get('/hello/{name}', function (Request $request, Response $response, $args) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");
    return $response;
});

$app->run();

Hello World with Slim-Http Decorators & Slim-Psr7

$ composer require slim/slim:4.0.0-alpha slim/psr7 slim/http
<?php
use Slim\Factory\AppFactory;
use Slim\Http\Factory\DecoratedResponseFactory;
use Slim\Http\Response;
use Slim\Http\ServerRequest as Request;
use Slim\Middleware\ErrorMiddleware;
use Slim\Psr7\Factory\ResponseFactory;
use Slim\Psr7\Factory\StreamFactory;

require __DIR__ . '/../vendor/autoload.php';

// Instantiate DecoratedResponseFactory and set it onto AppFactory
$responseFactory = new DecoratedResponseFactory(new ResponseFactory(), new StreamFactory());
AppFactory::setResponseFactory($responseFactory);

// Instantiate App
$app = AppFactory::create();

// Add error middleware
$errorMiddleware = new ErrorMiddleware($app->getCallableResolver(), $responseFactory, true, true, true);
$app->add($errorMiddleware);

// Decorated Request & Response object available via route callable
$app->get('/hello/{name}', function (Request $request, Response $response, $args) {
    $name = $args['name'];
    return $response->write("Hello, $name");
});

$app->run();

You may quickly test this using the built-in PHP server:

$ php -S localhost:8000

Going to http://localhost:8000/hello/world will now display "Hello, world".

For more information on how to configure your web server, see the Documentation.

Tests

To execute the test suite, you'll need to install all development dependencies.

$ git clone https://github.com/slimphp/Slim
$ composer install
$ composer test

Contributing

Please see CONTRIBUTING for details.

Learn More

Learn more at these links:

Security

If you discover security related issues, please email security@slimframework.com instead of using the issue tracker.

Credits

License

The Slim Framework is licensed under the MIT license. See License File for more information.

About

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%