ergy / slim-annotations-router
基于 Slim 框架 V3 的控制器和注解路由器
Requires
- php: >=5.5.0
- slim/slim: ^3.0@RC
This package is not auto-updated.
Last update: 2024-09-28 18:53:05 UTC
README
Slim Annotations Router
基于 Slim 框架 V3 的控制器和注解路由器
安装
通过 Composer
composer require ergy/slim-annotations-router
初始化
要初始化注解路由器,只需将以下行添加到加载您 Slim 应用的 index.php 文件中
$app = new \Slim\App(); $c = $app->getContainer(); $c['router'] = function() use ($app) { return new \Ergy\Slim\Annotations\Router($app, '/path_to_controller_files/', // Path to controller files, will be scanned recursively '/path_to_cache_files/' // Path to annotations router cache files, must be writeable by web server, if it doesn't exist, router will attempt to create it ); };
如果您的应用包含多个文件夹中的控制器,您可以通过将数组传递给构造函数的第二个参数来添加它们,而不是字符串,例如
return new \Ergy\Slim\Annotations\Router($app, ['/path_to_controller_files_1/', '/path_to_controller_files_2/', ...], '/path_to_cache_files/' );
控制器文件
注解路由器检测所有以 "Controller.php" 结尾的文件以及所有以 "Action" 结尾的公共方法。然后它解析它们的注解以生成 Slim 框架识别的路由。
支持以下注解
@Route 对象接受以下语法
// Route pattern (required), can contain regular expressions @Route("/home") // Route methods (optional), if not set only GET route will be generated @Route("/home", methods={"GET","POST","PUT"}) // Route name (optional), used to generate route with Slim pathFor() method @Route("/home", methods={"GET","POST","PUT"}, name="home")
让我们看一个例子
/** * File /my/app/controllers/homeController.php * @RoutePrefix("/home") */ class HomeController { /** * @Route("/hello/{name:[\w]+}", methods={"GET","POST"}, name="home.hello") */ public function helloAction($name) { echo 'Hello '.$name.' !'; } }
通过打开 URL http://your_site_url/home/hello/foobar,你应该看到 "Hello foobar !"。
获取 Slim 依赖项容器
一旦控制器加载,您可能需要与当前 HTTP 请求和响应进行交互,要获取这些对象,您的控制器只需扩展 Ergy\Slim\Annotations\Controller 类。
扩展此类允许您获取 Slim 依赖项容器的引用,因此要获取当前请求,您只需请求 $this->request。
让我们用一个之前的类来举个例子
/** * File /my/app/controllers/homeController.php * @RoutePrefix("/home") */ class HomeController extends Ergy\Slim\Annotations\Controller { /** * @Route("/hello/{name:[\w]+}", methods={"GET","POST"}, name="home.hello") */ public function helloAction($name) { echo 'Hello '.$name.' !'; // Dump the current request details var_dump($this->request); // Dump the current response details var_dump($this->response); } }
由于我们有了对 Slim 依赖项容器的引用,我们可以检索它包含的任何对象,例如,如果您想获取应用的设置,只需请求 $this->settings。
路由事件
注解路由器公开 beforeExecuteRoute 和 afterExecuteRoute 方法。
这些方法接受一个 Ergy\Slim\Annotations\RouteInfo 实例作为参数,它提供了关于正在运行的路由的信息。
在控制器中实现这些方法(或其父类中)允许您在执行动作之前/之后实现钩子点。
上一个类的例子
/** * File /my/app/controllers/homeController.php * @RoutePrefix("/home") */ class HomeController { public function beforeExecuteRoute(Ergy\Slim\Annotations\RouteInfo $route) { echo 'before '; } public function afterExecuteRoute() // Parameter Ergy\Slim\Annotations\RouteInfo is optional { echo ' after'; } /** * @Route("/hello/{name:[\w]+}", methods={"GET","POST"}, name="home.hello") */ public function helloAction($name) { echo 'Hello '.$name.' !'; } }
通过打开 URL http://your_site_url/home/hello/foobar,你应该看到 "before Hello foobar ! after"。
操作取消
之前的钩子允许您在需要时取消操作
-
您可以通过在 beforeExecuteRoute 方法中返回 false 或 \Psr\Http\Message\ResponseInterface 的实例来取消当前路由的执行和 afterExecuteRoute 方法。
-
您可以通过在当前路由方法中返回 false 或 \Psr\Http\Message\ResponseInterface 的实例来取消对 afterExecuteRoute 方法的调用。
例如
/** * File /my/app/controllers/homeController.php * @RoutePrefix("/home") */ class HomeController { public function beforeExecuteRoute(Ergy\Slim\Annotations\RouteInfo $route) { echo 'before '; } public function afterExecuteRoute() // Parameter Ergy\Slim\Annotations\RouteInfo is optional { echo ' after'; } /** * @Route("/hello/{name:[\w]+}", methods={"GET","POST"}, name="home.hello") */ public function helloAction($name) { echo 'Hello '.$name.' !'; return false; } }
通过打开 URL http://your_site_url/home/hello/foobar,你应该看到 "before Hello foobar !"。