jgautheron/slim-jsend

Slim PHP框架的JSend实现

dev-master 2015-04-01 15:10 UTC

This package is auto-updated.

Last update: 2024-09-05 19:01:49 UTC


README

Slim PHP框架的JSend中间件。

如何安装

您可以通过Composer安装SlimJsend:

composer install jgautheron/slim-jsend

或者将以下行添加到您的composer.json文件中

"jgautheron/slim-jsend": "dev-master"

如何使用

require 'vendor/autoload.php';
$app = new \Slim\Slim();

// Add the middleware globally
$app->add(new \SlimJsend\Middleware([
    // true means *
    'cors' => true,
    // for a finer control over the allowed origin
    'cors' => 'https://foo.com'
]));

$app->get('/', function() use ($app) {
    // SlimJsend will automatically generate the proper JSend response depending of the status code
    $app->render(200, $messages); // success
    $app->render(500, $data); // fail
    
    // if an exception is thrown, it will be automatically converted to a JSend error message
    throw new Exception('Uh oh... missing username');
});

$app->run();

鸣谢