dbeurive / slim-controller
此包包含SLIM框架的控制管理器
1.0.3
2017-01-16 10:19 UTC
Requires
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-23 15:24:56 UTC
README
介绍
此包允许使用“控制器”与Slim框架。
概要
定义控制器
$ tree /path
/path
└── to
└── controllers
├── ProfileController.php
└── UserController.php
请注意用于创建控制器文件名的后缀:“
Controller.php”。此后缀可以更改。
<?php namespace dbeurive\Slim\Test\controller0; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; use dbeurive\Slim\controller\Controller; /** * Class ProfileController * @package dbeurive\Slim\Test\controller0 */ class ProfileController extends Controller { /** * Create or update a profile. * @param Request $request * @param Response $response * @return Response */ public function actionPostSet(Request $request, Response $response) { $response->getBody()->write("Profile has been set! (" . $this->app->getContainer()[FLAG] . ')'); return $response; } /** * Get a profile. * @param Request $request * @param Response $response * @return Response * @uri-params {id} */ public function actionGetGet(Request $request, Response $response) { $response->getBody()->write("This is the requested profile data (" . $this->app->getContainer()[FLAG] . ')'); return $response; } }
请注意实现控制器操作的方法名称(“actionPostSet”和“actionGetGet”)。操作名称以前缀“
action”开头,后跟期望的HTTP方法名称(请参阅Slim的文档)。请注意注解“
@uri-params {id}”(对于方法“actionGetGet()”)。这意味着与此操作关联的URI是“/profile/get/{id}”。请查看控制器ProfileController.php和控制器UserController.php。
创建列出所有控制器的索引
php slim-controller.php index --index-path /app/data/index.json /path/to/controllers/
索引“
/app/data/index.json”包含有关控制器的数据。请点击此处查看此文件的内容。请点击此处查看脚本slim-controller.php。
您可以将索引的内容以人类可读的方式转储
$ php slim-controller.php dump --host http://www.slim-controller.localhost /app/data/index.json
POST http://www.slim-controller.localhost/profile/set
/path/to/controllers/ProfileController.php
\dbeurive\Slim\Test\controller0\ProfileController::actionPostSet
GET http://www.slim-controller.localhost/profile/get/{id}
/path/to/controllers/ProfileController.php
\dbeurive\Slim\Test\controller0\ProfileController::actionGetGet
POST http://www.slim-controller.localhost/user/login
/path/to/controllers/UserController.php
\dbeurive\Slim\Test\controller0\UserController::actionPostLogin
GET http://www.slim-controller.localhost/user/get/{id}
/path/to/controllers/UserController.php
\dbeurive\Slim\Test\controller0\UserController::actionGetGet
实现Slim应用程序
use dbeurive\Slim\controller\Manager as ControllerManager; $app = new \Slim\App([]); ControllerManager::start($app, '/app/data/index.json'); $app->run();
请注意,在此示例中,仅在预期的控制器中(根据URL)声明操作。例如,假设URL为:
http://www.slim-controller.localhost/profile/get/10。预期的控制器是“ProfileController”。此行为可以修改。实际上,可以配置控制器管理器,以便它会系统地注册所有路由(来自所有控制器)。为此,我们应该写入:
ControllerManager::start($app, '/app/data/index.json', true);请点击此处查看真实示例。