codermarcel / simple-controller
使用反射的便捷且简单的silex控制器
Requires
- silex/silex: ~1.3
Requires (Dev)
- phpunit/phpunit: 5.0.*
- symfony/browser-kit: ^2.7
- symfony/css-selector: ^2.7
This package is not auto-updated.
Last update: 2024-09-28 18:58:36 UTC
README
#SimpleController
SimpleController是一个基于反射的便捷控制器,用于[php微框架silex](http://silex.sensiolabs.org/)。SimpleController使得您能够轻松地在silex应用程序中使用控制器,并能自动匹配控制器方法和路由
#安装
运行以下命令
composer require codermarcel/simple-controller
设置
方法 - 1
扩展SimpleController
use Codermarcel\SimpleController\SimpleController;; class MyExampleControllerExtended extends SimpleController { /** * Responds to requests to GET / */ public function getIndex() { return echo 'Welcome!'; } }
挂载路由
$app->mount('/', new App\Controllers\MyExampleControllerExtended());
方法 - 2
使用原始类
如果您不想扩展SimpleController类,则也可以使用原始类。
class MyExampleControllerRaw { /** * Responds to requests to GET / */ public function getIndex() { return echo 'Welcome!'; } }
挂载路由
注意 使用控制器类的完整命名空间名称
$app->mount('/', new Codermarcel\SimpleController\SimpleController('App\Controllers\MyExampleControllerRaw'));
#使用方法
HTTP方法
方法名称应从响应的HTTP动词开始,后跟路由名称。
以下方法可用
- get
- post
- put
- delete
- patch
- options
- match
以下是一些示例
class MyExampleControllerRaw { /** * Responds to requests to GET /test */ public function getTest() { // } /** * Responds to requests to GET /show/{id} */ public function getShow($id) { // } /** * Responds to requests to GET /admin-profile */ public function getAdminProfile() { // } /** * Responds to requests to POST /profile */ public function postProfile() { // } }
组织控制器
当您的应用程序开始定义过多的控制器时,您可能希望逻辑上分组它们
mount()会将所有路由的前缀设置为给定的前缀,并将它们合并到主应用程序中。因此,/ 将映射到主主页,/blog/ 将映射到博客主页,/forum/ 将映射到论坛主页。
有关组织控制器的更多信息,请参阅官方 [silex 文档](http://silex.sensiolabs.org/doc/organizing_controllers.html#organizing-controllers)
示例 1
$app->mount('/', new App\Controllers\MyExampleControllerExtended());
use Codermarcel\SimpleController\SimpleController;; class MyExampleControllerExtended extends SimpleController { /** * Responds to request to GET / */ public function getIndex() { // } /** * Responds to request to GET /login-page */ public function getLoginPage() { // } }
示例 2
$app->mount('/user', new App\Controllers\MyExampleControllerExtended());
use Codermarcel\SimpleController\SimpleController;; class MyExampleControllerExtended extends SimpleController { /** * Responds to request to GET /user/ */ public function getIndex() { // } /** * Responds to request to GET /user/home-page */ public function getHomePage() { // } }
路由变量
您可以在路由中定义如下变量部分
注意 默认路由值目前不支持,但可能在以后的版本中添加。
class MyExampleControllerRaw { /** * Responds to requests to POST /login{username}/{password} */ public function postLogin($username, $password) { return sprintf('Trying to log in with username: %s and password: %s', $username, $password); } }
请求和应用注入
您还可以像这样请求当前的请求和应用对象
注意 silex基于类型提示进行注入,而不是基于变量名称!
class MyExampleControllerRaw { use Silex\Application; use Symfony\Component\HttpFoundation\Request; /** * Responds to requests to GET /injection */ public function getInjection(Application $app, Request $request) { // } }
命名路由
您可以使用路由中的 $bind 参数将路由名称绑定到路由
有关命名路由和UrlGeneratorServiceProvider的更多信息,请参阅官方 [silex 文档](http://silex.sensiolabs.org/doc/providers/url_generator.html#urlgeneratorserviceprovider)
class MyExampleControllerRaw { use Silex\Application; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; /** * Responds to requests to GET /bind-example * * {@link http://silex.sensiolabs.org/doc/providers/url_generator.html#usage} */ public function getBindExample(Application $app, $bind = 'bind_example') { //Example usage of the bind_example route //You can use ABSOLUTE_URL or ABSOLUTE_PATH return new Response($app['url_generator']->generate('bind_example', array(), UrlGeneratorInterface::ABSOLUTE_PATH)); } }
中间件
Silex允许您通过中间件在不同的请求处理阶段运行更改默认Silex行为的代码
[…]
当匹配关联的路由时,将触发路由中间件。
有关中间件的更多信息,请参阅官方 [silex 文档](http://silex.sensiolabs.org/doc/middlewares.html#middlewares)
注意 您可以为请求、响应或应用程序对象进行类型提示,并且 silex 将为您注入它们。
class MyExampleControllerRaw { use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; /** * Before middleware example * * {@link http://silex.sensiolabs.org/doc/middlewares.html#before-middleware} */ public function beforeMiddleware(Request $request) { if ($request->getRequestUri() === '/before-middleware') { return new Response('YOU SHALL NOT PASS'); } } /** * After middleware example * * {@link http://silex.sensiolabs.org/doc/middlewares.html#after-middleware} */ public function afterSomeRandomNameThatDoesntMatter(Request $request, Response $response, Application $app) { if ($request->getRequestUri() === '/after-middleware') { return new Response($response->getContent() . ' | after-middleware content'); } } }
致谢
SimpleController受到以下灵感的启发
https://gist.github.com/igorw/4524636
和 https://laravel.net.cn/docs/5.1/controllers#implicit-controllers