matiasnamendola / slimpower-slim
为 Slim 框架动态实例化控制器类
Requires
- php: >=5.3.0
- slim/slim: 2.*
- symfony/yaml: v2.6.0
This package is not auto-updated.
Last update: 2024-09-28 20:03:46 UTC
README
Slim 框架的扩展,允许您在路由时动态实例化控制器,就像您通常使用闭包或回调一样。
控制器可以选择从 Slim 的 DI 容器中加载,允许您按需注入依赖项。
此外,此扩展可以轻松实现 Json 中间件和视图。
安装
请参阅 安装文件
使用 - 动态控制器实例化
在通常使用闭包的地方使用字符串格式 {控制器类名}:{操作方法名}
例如。
require 'vendor/autoload.php'; $app = new \SlimPower\Slim\Slim(); $app->get('/hello:name', 'App\IndexController:home');
您还可以将控制器注册到 Slim 的 DI 容器中
<?php require 'vendor/autoload.php'; $app = new \SlimPower\Slim\Slim(); $app->container->singleton('App\IndexController', function ($container) { // Retrieve any required dependencies from the container and // inject into the constructor of the controller. return new \App\IndexController(); }); $app->get('/', 'App\IndexController:index');
示例控制器
SlimPower - Slim 控制器 将调用控制器中存在的 setApp()
、setRequest()
和 setResponse()
方法,并适当地填充。然后调用控制器的 `init()` 方法。
因此,典型的控制器可能看起来像
<?php namespace App; class IndexController { // Optional properties. protected $app; protected $request; protected $response; public function index() { echo "This is the home page"; } public function hello($name) { echo "Hello, $name"; } // Optional setters. public function setApp($app) { $this->app = $app; } public function setRequest($request) { $this->request = $request; } public function setResponse($response) { $this->response = $response; } // Init public function init() { // Do things now that app, request and response are set. } }
使用 - Json 中间件
要包含中间件和视图,您只需使用默认的 Slim 方式加载它们。有关 Slim 的更多信息,请参阅此处 (https://github.com/codeguy/Slim#getting-started)
require 'vendor/autoload.php'; $app = new \SlimPower\Slim\Slim(); $app->view(new \SlimPower\Slim\Middleware\Json\JsonView()); $app->add(new \SlimPower\Slim\Middleware\Json\JsonMiddleware());
示例方法
您的所有请求都将返回 JSON 输出。使用方法为 $app->render( (int)$HTTP_CODE, (array)$DATA);
示例代码
$app->get('/', function() use ($app) { $app->render(200, array( 'msg' => 'welcome to my API!', )); });
示例输出
{ "msg":"welcome to my API!", "error":false, "status":200 }
错误
要显示错误,只需在您的数据数组中将 error => true
设置为 true。所有请求都将有一个默认为 false 的 error
参数。
$app->get('/user/:id', function($id) use ($app) { // Your code here. $app->render(404, array( 'error' => TRUE, 'msg' => 'user not found', )); });
{ "msg":"user not found", "error":true, "status":404 }
您可以选择抛出异常,中间件将捕获所有异常并显示错误消息。
$app->get('/user/:id', function($id) use ($app) { // Your code here. if(...) { throw new \Exception("Something wrong with your request!"); } });
{ "error": true, "msg": "ERROR: Something wrong with your request!", "status": 500 }
将响应数据元数据和业务信息嵌入到单独的容器中
可以在单独的容器中分离响应元数据和业务信息。
要实现这一点,只需使用容器名称初始化 JsonView
require 'vendor/autoload.php'; $app = new \SlimPower\Slim\Slim(); $app->view(new \SlimPower\Slim\Middleware\Json\JsonView("resource", "meta")); $app->add(new \SlimPower\Slim\Middleware\Json\JsonMiddleware());
响应
{ "resource":{ "msg":"welcome to my API!" }, "meta":{ "error":false, "status":200 } }
将特定请求路由到 API
如果您的网站正在使用常规 HTML 响应,并且您只想在特定路由上公开 API 点,您可以使用 Slim 路由中间件来定义此操作。
function jsonResponse(){ $app = \SlimPower\Slim\Slim::getInstance(); $app->view(new \SlimPower\Slim\Middleware\Json\JsonView()); $app->add(new \SlimPower\Slim\Middleware\Json\JsonMiddleware()); } $app->get('/home',function() use($app){ // Regular HTML response. $app->render("template.tpl"); }); $app->get('/api','jsonResponse',function() use($app){ // This request will have full JSON responses. $app->render(200, array( 'msg' => 'welcome to my API!', )); });
中间件
中间件将为默认请求设置一些静态路由。 如果您不想使用它,可以将其内容代码复制到您的引导文件中。
重要:请记住使用 $app->config('debug', false);
或错误将仍然在 HTML 中打印
致谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件