Slim 是一个PHP微型框架,它可以帮助你快速开发简单且强大的web应用和API。

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

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

$app = AppFactory::create();

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

$app->run();

下载 & 安装

我们推荐你使用 Composer 依赖管理来安装slim框架。

只安装slim核心代码,demo地址:slim4you

$ mkdir -p slim-demo/public && cd slim-demo
$ composer require slim/slim:"4.*" slim/psr7
$ touch public/index.php //复制页面最上面的demo代码
$ php -S localhost:8080 -t public public/index.php

开始使用slim最简单的方法是用 Slim-Skeleton创建工程项目,只需要执行如下命令:

$ php composer.phar create-project slim/slim-skeleton:dev-master [my-app-name]

用你的项目名称替换 [my-app-name]这里。

你可以使用PHP内置web服务器运行项目:

$ cd [my-app-name]; php -S localhost:8080 -t public public/index.php

特性

HTTP路由

Slim provides a fast and powerful router that maps route callbacks to specific HTTP request methods and URIs. It supports parameters and pattern matching.

中间件

Build your application with concentric middleware to tweak the HTTP request and response objects around your Slim app.

支持 PSR-7

Slim supports any PSR-7 HTTP message implementation so you may inspect and manipulate HTTP message method, status, URI, headers, cookies, and body.

依赖注入 DI

Slim supports dependency injection so you have complete control of your external tools. Use any Container-Interop container.

最新动态

查看以前

欢迎交流

You can chat with other Slim Framework developers to share code or troubleshoot tricky issues using either Slack or IRC. Our nicks are akrabat, silentworks and codeguy if you have have questions or need assistance.

Slack

We can be found on Slack at slimphp.slack.com.