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
Middleware controllers act as a handler for multiple routes. Some conventions were made:
35
+
Middleware controllers act as a handler for multiple routes.
36
+
Some conventions were made:
36
37
37
-
- register controllers in the routes array just like any mezzio middleware. The requirement is that you should define an `action` route parameter(possibly optional) anywhere inside the route(e.g `/user[/{action}]`)
38
-
- 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`.
38
+
- register controllers in the `routes` array just like any Mezzio middleware.
39
+
The requirement is that you should define an `action` route parameter (possibly optional) anywhere inside the route (e.g `/user[/{action}]`).
40
+
- action parameter value is converted to a method name inside the controller.
41
+
Underscore, dot and line characters are removed and the action name is converted to a camel-case suffixed by the string `Action`.
42
+
For example, a route and action pair like `/user/forgot-password` will be converted to method `forgotPasswordAction`.
39
43
- 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.
40
44
41
-
In order to create your actionbased controllers, you must extend the abstract class `Dot\Controller\AbstractActionController`.
45
+
To create your action-based controllers, you must extend the abstract class `Dot\Controller\AbstractActionController`.
42
46
43
47
### Example
44
48
45
-
Creating a UserController with default action and a register action. Will handle routes `/user` and `/user/register`.
49
+
Creating a UserController with a default action and a register action.
50
+
Will handle routes `/user` and `/user/register`.
46
51
47
52
```php
48
53
use Dot\Controller\AbstractActionController;
@@ -61,7 +66,7 @@ class UserController extends AbstractActionController
61
66
}
62
67
```
63
68
64
-
Then register this controller as a routed middleware in file `RoutesDelegator.php` just like a regular middleware.
69
+
Then register this controller as routed middleware in file `RoutesDelegator.php` just like regular middleware.
65
70
66
71
```php
67
72
//Example from a RoutesDelegator
@@ -77,13 +82,13 @@ $app->route(
77
82
78
83
**Use case:**
79
84
You have defined a controller inside some package, with default actions.
80
-
You want to add actions that fall into the same controller name(or route name more exactly).
85
+
You want to add actions that fall into the same controller name(or route name more exactly).
81
86
You want to do this without extending the controller provided by the package.
82
87
In this case you can do the following:
83
88
84
-
- create your own controller, independent of the package's controller which adds more actions
89
+
- create your own controller, independent of the package's controller, which adds more actions
85
90
- Mezzio lets you define an array of middleware for a route, so you can register this controller before the package's controller
86
91
87
92
Now when a request for this route comes in, your controller will run first.
88
93
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.
89
-
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).
94
+
If this is the last controller, and the action does not match here, it will go to the default 404 Not found page(handled by NotFoundDelegate).
Copy file name to clipboardExpand all lines: docs/book/v3/configuration.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Configuration
2
2
3
-
After installation, the package can be used immediately but if you want to use all features of the package, like plugins and events you need to register the `ConfigProvider` in your project by adding the below line to your configuration aggregator (usually: `config/config.php`):
3
+
After installation, the package can be used immediately but if you want to use all features of the package, like plugins and events, you need to register the `ConfigProvider` in your project by adding the below line to your configuration aggregator (usually: `config/config.php`):
Copy file name to clipboardExpand all lines: docs/book/v3/events.md
+14-10Lines changed: 14 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,21 @@
1
1
# Events
2
2
3
-
Dotkernel's controller package supports events and those events can be of 2 types: global events (middleware-like) or manually dispatch events.
3
+
Dotkernel's controller package supports events, and those events can be of two types: global events (middleware-like) or manually dispatched events.
4
4
5
5
## Getting started
6
6
7
-
- Every event listener that is triggered from a controller needs to implement `Dot\Controller\Event\ControllerEventListenerInterface` which actually extends `Laminas\EventManager\ListenerAggregateInterface`.
7
+
- Every event listener triggered from a controller needs to implement `Dot\Controller\Event\ControllerEventListenerInterface` which actually extends `Laminas\EventManager\ListenerAggregateInterface`.
8
8
- You can add the trait `Dot\Controller\Event\ControllerEventListenerTrait` to override the method of the interface.
9
9
- Every event listener needs to be registered under the `['dot_controller']['event_listenenrs]` key in the ConfigProvider, and every key must be the class that you want to attach the events.
10
10
11
11
## Usage
12
12
13
-
The events in a controller can be done in 2 different ways, a global way where an event is attached automatically to all the controllers action and works in the same way the middlewares works or a manually dispatchable way, where you can define to which controller the events is attached, and you can trigger the event where you want.
14
-
For our example we have a UserController with some methods in it:
13
+
The events in a controller can be done in two different ways:
14
+
15
+
- a global way where an event is attached automatically to all the controller actions and works in the same way the middlewares works
16
+
- in a manually dispatchable way, where you can define to which controller the events are attached, and you can trigger the event where you want
17
+
18
+
For our example, we have a UserController with some methods in it:
15
19
16
20
```php
17
21
use Dotkernel\DotController\AbstractActionController;
@@ -36,9 +40,9 @@ class UserController extends AbstractActionController
36
40
}
37
41
```
38
42
39
-
### Example 1 - Global way
43
+
### Global method
40
44
41
-
First we will create the event listener:
45
+
First, we will create the event listener:
42
46
43
47
```php
44
48
use Dot\Controller\Event\ControllerEvent;
@@ -98,7 +102,7 @@ public function onAfterDispatch(ControllerEvent $e): void
98
102
So every time the `updateAction` is accessed and the method is post, right after the action is dispatched, we can log that the user was updated.
99
103
We can use the `onBeforeDispatch` in the same way, to log right before the user is updated.
100
104
101
-
### Example 2 - Manually triggered way
105
+
### Manual method
102
106
103
107
```php
104
108
use Dot\Controller\Event\ControllerEvent;
@@ -128,7 +132,7 @@ class UserUpdatedListener implements ControllerEventListenerInterface
128
132
}
129
133
```
130
134
131
-
The `attach` method is from the `ListenerAggregateInterface` which `ControllerEventListenerTrait` already is overriding it so can be used in a global way with `onBeforeDispatch` and `onAfterDispatch` methods, but we can make our custom event and bind it to our method.
135
+
The `attach` method is from the `ListenerAggregateInterface` which `ControllerEventListenerTrait` already is overriding it so can be used globally with `onBeforeDispatch` and `onAfterDispatch` methods, but we can make our custom event and bind it to our method.
132
136
In this case we create attach an event called `user.profile.update` and bind it to the `userProfileUpdated` method.
133
137
134
138
Next, we need to register the event:
@@ -137,7 +141,7 @@ Next, we need to register the event:
Copy file name to clipboardExpand all lines: docs/book/v3/plugins.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,22 @@
1
1
# Plugins
2
2
3
-
Dotkernel's controller support plugins, much like controllers in Laminas applications.
4
-
The package comes packed with a few builtin plugins, but you can extend controller functionality with your own plugins.
3
+
Dotkernel's controller supports plugins, much like controllers in Laminas applications.
4
+
The package comes packed with a few built-in plugins, but you can extend controller functionality with your own plugins.
5
5
6
6
## Usage
7
7
8
8
Any controller plugins must implement `Dot\Controller\Plugin\PluginInterface`.
9
9
You need to create a factory in addition to the plugin and register it under the `['dot_controller']['plugin_manager']['factories']` with the plugin name.
10
10
11
-
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).
11
+
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).
12
12
13
-
Controller plugins offer the advantage of globally accessible functionality in any controller without to manually inject dependencies.
13
+
Controller plugins offer the advantage of globally accessible functionality in any controller without manually injecting dependencies.
14
14
Plugins should be used for functions that are common to any controller.
15
15
Do not clutter controller's code with unnecessary plugins.
16
16
17
17
### Example
18
18
19
-
First we create our desired plugin, for our example a string helper:
19
+
First we create our desired plugin, for our example, a string helper:
20
20
21
21
```php
22
22
class StringPlugin implements PluginInterface
@@ -58,7 +58,7 @@ Register the factory under the `['dot_controller']['plugin_manager']['factories'
58
58
]
59
59
```
60
60
61
-
You don't need to register the plugin factory to a regular dependencies in a configuration because `AbstractPluginManager` actually extends `ServiceManager`.
61
+
You don't need to register the plugin factory to a regular dependency in a configuration because `AbstractPluginManager` actually extends `ServiceManager`.
62
62
Access it in a controller:
63
63
64
64
```php
@@ -67,9 +67,9 @@ $this->string(); // will return the StringPlugin class, so you can call any publ
67
67
$this->string()->toUpper("test") // will return TEST
68
68
```
69
69
70
-
## Build-in plugins
70
+
## Built-in plugins
71
71
72
-
The package comes in with 2 default plugins ``template`` and `url`.
72
+
The package comes in with two default plugins `template` and `url`.
73
73
You can use them in the same way as our example above.
74
74
75
75
-`url` - the plugin is an instance of `Mezzio\Helper\UrlHelper`:
0 commit comments