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
This 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
3
+
This is Dotkernel's controller package that can be used like middleware inside Dotkernel or Mezzio application.
4
+
It provides base classes for action-based controllers similar to a Laminas controller component.
5
+
It is more lightweight, though, but supports controller plugins and event listeners.
6
+
7
+
## Documentation
8
+
9
+
Documentation is available at: https://docs.dotkernel.org/dot-controller/v3/overview/.
Install `dot-controller` by executing the following Composer command:
22
28
23
-
```bash
24
-
$ composer require dotkernel/dot-controller
29
+
```shell
30
+
composer require dotkernel/dot-controller
25
31
```
26
32
27
33
## Usage
28
34
29
35
Middleware controllers act as a handler for multiple routes. Some conventions were made:
30
36
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}]`)
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`.
37
+
- register controllers in the `routes` array just like any mezzio middleware.
38
+
The requirement is that you should define an `action` route parameter (possibly optional) anywhere inside the route (e.g `/user[/{action}]`).
39
+
- action parameter value is converted to a method name inside the controller.
40
+
Underscore, dot and line characters are removed and the action name is converted to a camel-case suffixed by the string `Action`.
41
+
For example, a route and action pair like `/user/forgot-password` will be converted to method `forgotPasswordAction`.
33
42
- 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.
34
43
35
-
In order to create your actionbased controllers, you must extend the abstract class `DotKernel\DotController\AbstractActionController`
44
+
To create your action-based controllers, you must extend the abstract class `Dot\DotController\AbstractActionController`
36
45
37
46
### Example
38
47
39
-
Creating a UserController with default action and a register action. Will handle routes `/user` and `/user/register`
48
+
Creating a UserController with a default action and a register action. Will handle routes `/user` and `/user/register`.
40
49
41
50
```php
42
-
use DotKernel\DotController\AbstractActionController;
51
+
use Dot\DotController\AbstractActionController;
43
52
44
53
class UserController extends AbstractActionController
45
54
{
@@ -55,10 +64,10 @@ class UserController extends AbstractActionController
55
64
}
56
65
```
57
66
58
-
Then register this controller as a routed middleware in file `RoutesDelegator.php` just like a regular middleware.
67
+
Then register this controller as routed middleware in file `RoutesDelegator.php` just like regular middleware.
59
68
60
69
```php
61
-
//Example from a DotKernel RoutesDelegator
70
+
//Example from a Dotkernel RoutesDelegator
62
71
$app->route(
63
72
'/user[/{action}]',
64
73
UserController::class,
@@ -69,10 +78,13 @@ $app->route(
69
78
70
79
### Multiple controllers for the same route
71
80
72
-
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
81
+
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).
82
+
You want to do this without extending the controller provided by the package.
83
+
In this case you can do the following:
73
84
74
-
- create your own controller, independent of the package's controller which adds more actions
85
+
- create your own controller, independent of the package's controller, which adds more actions
75
86
- Mezzio lets you define an array of middleware for a route, so you can register this controller before the package's controller
76
87
77
-
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.
78
-
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)
88
+
Now when a request for this route comes in, your controller will run first.
89
+
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.
90
+
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).
0 commit comments