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
+4-3Lines changed: 4 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,25 +19,25 @@ It provides base classes for action based controllers similar to Laminas control
19
19
## Installation
20
20
21
21
Install `dot-controller` by executing the following Composer command:
22
+
22
23
```bash
23
24
$ composer require dotkernel/dot-controller
24
25
```
25
26
26
27
## Usage
27
28
28
29
Middleware controllers act as a handler for multiple routes. Some conventions were made:
30
+
29
31
- 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}]`)
30
32
- 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`.
31
33
- 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.
32
34
33
35
In order to create your action based controllers, you must extend the abstract class `DotKernel\DotController\AbstractActionController`
34
36
35
-
#####Example
37
+
### Example
36
38
Creating a UserController with default action and a register action. Will handle routes `/user` and `/user/register`
37
39
38
-
##### UserController.php
39
40
```php
40
-
41
41
use DotKernel\DotController\AbstractActionController;
42
42
43
43
class UserController extends AbstractActionController
@@ -69,6 +69,7 @@ $app->route(
69
69
### Multiple controllers for the same route
70
70
71
71
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
72
+
72
73
- create your own controller, independent of the package's controller which adds more actions
73
74
- Mezzio lets you define an array of middleware for a route, so you can register this controller before the package's controller
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
@@ -2,4 +2,4 @@
2
2
3
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
+17-6Lines changed: 17 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,22 @@
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
4
4
-
# Getting started
5
-
- Every event listener that is triggered from a controller
6
-
needs to implement `Dot\Controller\Event\ControllerEventListenerInterface` which actually extends `Laminas\EventManager\ListenerAggregateInterface`.
5
+
## Getting started
6
+
7
+
- Every event listener that is triggered from a controller
8
+
needs to implement `Dot\Controller\Event\ControllerEventListenerInterface` which actually extends `Laminas\EventManager\ListenerAggregateInterface`.
7
9
- You can add the trait `Dot\Controller\Event\ControllerEventListenerTrait` to override the method of the interface.
8
10
- 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
9
11
10
12
11
-
# Usage
13
+
## Usage
14
+
12
15
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
13
16
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
17
15
18
For our example we have a UserController with some methods in it
19
+
16
20
```php
17
21
use DotKernel\DotController\AbstractActionController;
18
22
@@ -34,10 +38,10 @@ class UserController extends AbstractActionController
34
38
35
39
}
36
40
}
37
-
38
41
```
39
42
40
43
### Example 1 - Global way
44
+
41
45
First we will create the event listener
42
46
```php
43
47
use Dot\Controller\Event\ControllerEvent;
@@ -64,6 +68,7 @@ class UserUpdatedListener implements ControllerEventListenerInterface
64
68
```
65
69
66
70
We register the event listener in the configuration key
71
+
67
72
```php
68
73
'dot_controller' => [
69
74
'event_listeners' => [
@@ -82,6 +87,7 @@ With this it doesn't matter what action is accessed, the event it will run befor
82
87
In addition, you can make use of the `event` variable to access information about the event.
83
88
84
89
For example:
90
+
85
91
```php
86
92
// UserUpdatedListener
87
93
public function onAfterDispatch(ControllerEvent $e): void
@@ -94,10 +100,12 @@ public function onAfterDispatch(ControllerEvent $e): void
94
100
}
95
101
}
96
102
```
103
+
97
104
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. <br>
98
105
We can use the `onBeforeDispatch` in the same way, to log right before the user is updated.
99
106
100
107
### Example 2 - Manually triggered way
108
+
101
109
```php
102
110
use Dot\Controller\Event\ControllerEvent;
103
111
use Dot\Controller\Event\ControllerEventListenerInterface;
@@ -131,6 +139,7 @@ with `onBeforeDispatch` and `onAfterDispatch` methods, but we can make our custo
131
139
In this case we create attach an event called `user.profile.update` and bind it to the `userProfileUpdated` method.
132
140
133
141
Next we need to register the event
142
+
134
143
```php
135
144
'dot_controller' => [
136
145
'event_listeners' => [
@@ -142,6 +151,7 @@ Next we need to register the event
142
151
```
143
152
144
153
Now you can manually trigger the event from the controller using build in `dispatchEvent` method.
154
+
145
155
```php
146
156
// UserController
147
157
// post method for updating the user
@@ -152,12 +162,13 @@ public function updateAction()
152
162
153
163
}
154
164
```
165
+
155
166
As you can see we attach the `user` key to the parameters, so we can actually access it.
167
+
156
168
```php
157
169
public function userProfileUpdated(ControllerEvent $event): void
`dot-controller` is DotKernel's controller package that can be use like middleware inside DotKernel or Mezzio application.
4
-
It provides base classes for action based controllers similar to Laminas controller component. It is more lightweight though, but supports controller plugins and event listeners
4
+
It provides base classes for action based controllers similar to Laminas controller component. It is more lightweight though, but supports controller plugins and event listeners
Copy file name to clipboardExpand all lines: docs/book/v3/plugins.md
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,7 @@ Controller plugins offer the advantage of globally accessible functionality in a
13
13
### Example
14
14
15
15
First we create our desired plugin, for our example a string helper
16
+
16
17
```php
17
18
class StringPlugin implements PluginInterface
18
19
{
@@ -26,6 +27,7 @@ class StringPlugin implements PluginInterface
26
27
```
27
28
28
29
We create a factory for the plugin
30
+
29
31
```php
30
32
use Psr\Container\ContainerInterface;
31
33
@@ -41,6 +43,7 @@ class StringPluginFactory
41
43
```
42
44
43
45
Register the factory under the `['dot_controller']['plugin_manager']['factories']` key.
46
+
44
47
```php
45
48
'dot_controller' => [
46
49
'plugin_manager' => [
@@ -50,21 +53,25 @@ Register the factory under the `['dot_controller']['plugin_manager']['factories'
50
53
]
51
54
]
52
55
```
56
+
53
57
You don't need to register the plugin factory to a regular dependencies in a configuration because `AbstractPluginManager` actually extends `ServiceManager`
54
58
55
59
Access it in a controller.
60
+
56
61
```php
57
62
//inside a controller
58
63
$this->string(); // will return the StringPlugin class, so you can call any public method from it
59
64
$this->string()->toUpper("test") // will return TEST
60
65
```
61
66
62
67
## Build-in plugins
68
+
63
69
The package comes in with 2 default plugins ``template`` and `url`. You can use
64
70
them in the same way as our example above.
65
71
66
72
- Url plugin
67
73
-- the plugin is an instance of `Mezzio\Helper\UrlHelper`
74
+
68
75
```php
69
76
//in a controller action
70
77
/** @var UrlHelper $url */
@@ -74,6 +81,7 @@ them in the same way as our example above.
74
81
75
82
- Template plugin
76
83
-- the plugin is an instance of `Mezzio\Template\TemplateRendererInterface`
Copy file name to clipboardExpand all lines: docs/book/v3/usage.md
+8-7Lines changed: 8 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,17 @@
1
1
# Usage
2
2
3
3
Middleware controllers act as a handler for multiple routes. Some conventions were made:
4
+
4
5
- 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}]`)
5
6
- 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`.
6
7
- 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.
7
8
8
9
In order to create your action based controllers, you must extend the abstract class `DotKernel\DotController\AbstractActionController`
9
10
10
-
#####Example
11
+
## Example
11
12
Creating a UserController with default action and a register action. Will handle routes `/user` and `/user/register`
12
13
13
-
##### UserController.php
14
14
```php
15
-
16
15
use DotKernel\DotController\AbstractActionController;
17
16
18
17
class UserController extends AbstractActionController
@@ -44,15 +43,17 @@ $app->route(
44
43
### Multiple controllers for the same route
45
44
46
45
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
46
+
47
47
- create your own controller, independent of the package's controller which adds more actions
48
48
- Mezzio lets you define an array of middleware for a route, so you can register this controller before the package's controller
49
49
50
50
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.
51
51
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)
0 commit comments