gabidj/dot-controller
Composer 安装命令:
composer require gabidj/dot-controller
包简介
DotKernel controller like middleware component with plugin support
README 文档
README
This package contains controller like middleware to be used inside a DotKernel or Expressive application. It provides base classes for action based controllers similar to ZF3 controller component. It is more lightweight though, but supports controller plugins.
Installation
Run the following composer command in your project directory
$ composer require dotkernel/dot-controller
Merge the ConfigProvider to your configuration aggregate.
Usage
Middleware controllers act as a handler for multiple routes. Some conventions were made:
- register controllers in the routes array just like any expressive middleware. The requirement is that you should define an
actionroute parameter(possibly optional) anywhere inside the route(e.g/user[/{action}]) - action parameter value is converted to a method name inside the controller. Underscore, dot and line characters are removed and the action name is converted to camel-case suffixed by the string
Action. For example a route and action pair like/user/forgot-passwordwill be converted to methodforgotPasswordAction. - the default action value, if not present in the URI is
index, so you should always define anindexActionwithin your controllers for displaying a default page or redirecting.
In order to create your action based controllers, you must extend the abstract class DotKernel\DotController\AbstractActionController
Example 1
Creating a UserController with default action and a register action. Will handle routes /user and /user/register
UserController.php
use DotKernel\DotController\AbstractActionController; class UserController extends AbstractActionController { public function indexAction() { //... } public function registerAction() { //... } }
Then register this controller as a routed middleware in file routes.php just like a regular middleware.
Multiple controllers for the same route
Use case: You have defined a controller inside some package, with default actions. You want to add actions that fall into the same controller name(or route name more exactly). You want to do this without extending the controller provided by the package. In this case you can do the following
- create your own controller, independent of the package's controller which adds more actions
- Zend Expressive lets you define an array of middleware for a route, so you can register this controller before the package's controller
Now when a request for this route comes in, your controller will run first. DotKernel controllers are designed to ignore requests that cannot be matched to one of its methods, so if no action matches, it will call the next middleware, in our case, the second controller. If this is the last controller, and action does not match here, it will go to the default 404 Not found page(handled by NotFoundDelegate)
Controller plugins
Controllers support controller plugins, much like controllers in a ZF3 application. The module comes packed with a few common plugins, but you can extend controller functionality with your own plugins too.
Usage
Controller plugins must implement Dot\Controller\Plugin\PluginInterface. You can add them to the config file, at key ['dk_controller']['plugin_manager']. The design pattern uses the AbstractPluginManager provided by ZF3 service manager component. So, registration of a plugin under the aforementioned config key looks the same as the declaration of regular dependencies, as AbstractPluginManager actually extends ServiceManager.
Once registered, a plugin can be directly accessed in any controller, by calling a method with the plugin's name(the service name or the key at which the plugin is registered inside the manager)
Controller plugins offer the advantage of globally accessible functionality in any controller without to manually inject dependencies. Plugins should be used for functions that are common to any controller. Do not clutter controller's code with unnecessary plugins.
Example
//inside a controller //assume we've already registered a plugin called testPlugin $this->testPlugin(); //will return the TestPlugin class so you can call any public defined method on it $this->testPlugin()->someMethod();
Built-in plugins
Note: Each of these plugins requires the associated ZE packages to be installed and available in your project. Although these are optional, if a package is missing, the controller will not have the associated functionality available
templatewraps TemplateInterface provided by ZE, to make template engine accessible to any controllerurlwraps the UrlHelper class provided by ZE helpers package. Used to generate URIs from routes
Controller Events
// @TODO
gabidj/dot-controller 适用场景与选型建议
gabidj/dot-controller 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 gabidj/dot-controller 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gabidj/dot-controller 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 15
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-21