You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+11-43Lines changed: 11 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,21 +4,17 @@ This package contains controller like middleware to be used inside a DotKernel o
4
4
5
5
## Installation
6
6
7
-
We use this in our web starter packages, so it will automatically be installed if you use them. For manual installation, just add the appropriate package to your `composer.json`
8
-
7
+
Run the following composer command in your project directory
9
8
```bash
10
9
$ composer require dotkernel/dot-controller
11
10
```
12
-
13
-
For default module dependencies, you need to merge the ConfigProvider class output to your config. Again, if you are using the web starter packages(frontend, admin etc.), this is already handled.
14
-
15
-
By doing this, you "enable" the module to be available functionally for your project.
11
+
Merge the `ConfigProvider` to your configuration aggregate.
16
12
17
13
## Usage
18
14
19
15
Middleware controllers act as a handler for multiple routes. Some conventions were made:
20
16
- register controllers in the routes array just like any expressive middleware. The requirement is that you should define an `action` route parameter(possibly optional) anywhere inside the route(e.g `/user[/{action}]`)
21
-
- 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-password` will be converted to method `forgotPasswordAction` inside a `UserController` class(as defined in routes config)
17
+
- 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-password` will be converted to method `forgotPasswordAction`.
22
18
- the default action value, if not present in the URI is `index`, so you should always define an `indexAction` within your controllers for displaying a default page or redirecting.
23
19
24
20
In order to create your action based controllers, you must extend the abstract class `DotKernel\DotController\AbstractActionController`
@@ -45,53 +41,24 @@ class UserController extends AbstractActionController
45
41
}
46
42
```
47
43
48
-
Then register this controller in the `routes` config(we assume you use FastRouter and ZF3 service manager)
49
-
##### routes.global.php
50
-
```php
51
-
'dependencies' => [
52
-
//add UserController class as a dependency, in invokable or factories etc.
Then register this controller as a routed middleware in file `routes.php` just like a regular middleware.
67
45
68
46
### Multiple controllers for the same route
69
47
70
48
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
71
49
- create your own controller, independent of the package's controller which adds more actions
72
-
-ZE lets you define an array of middleware for a route, so you can register this controller before the package's controller
50
+
-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. Dot 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.
86
-
If this is the last controller, and action does not match here, it will go to the default 404 Not found page. There is a simple middleware defined in dot-helpers that makes sure any request that does not match will be converted explicitly to a 404 error.
52
+
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.
53
+
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)
87
54
88
55
## Controller plugins
89
56
90
57
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.
91
58
92
59
### Usage
93
60
94
-
Controller plugins must implement `DotKernel\DotController\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`.
61
+
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`.
95
62
96
63
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)
97
64
@@ -112,6 +79,7 @@ Although these are optional, if a package is missing, the controller will not ha
112
79
-`template` wraps TemplateInterface provided by ZE, to make template engine accessible to any controller
113
80
-`url` wraps the UrlHelper class provided by ZE helpers package. Used to generate URIs from routes
0 commit comments