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 package contains controller like middleware to be used inside a DotKernel or Mezzio application. It provides base classes for action based controllers similar to Laminas controller component. It is more lightweight though, but supports controller plugins.
3
+
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

@@ -17,27 +18,27 @@ This package contains controller like middleware to be used inside a DotKernel o
17
18
18
19
## Installation
19
20
20
-
Run the following composer command in your project directory
21
+
Install `dot-controller` by executing the following Composer command:
22
+
21
23
```bash
22
24
$ composer require dotkernel/dot-controller
23
25
```
24
-
Merge the `ConfigProvider` to your configuration aggregate.
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 1
37
+
### Example
38
+
36
39
Creating a UserController with default action and a register action. Will handle routes `/user` and `/user/register`
37
40
38
-
##### UserController.php
39
41
```php
40
-
41
42
use DotKernel\DotController\AbstractActionController;
42
43
43
44
class UserController extends AbstractActionController
@@ -56,38 +57,22 @@ class UserController extends AbstractActionController
56
57
57
58
Then register this controller as a routed middleware in file `RoutesDelegator.php` just like a regular middleware.
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
73
+
62
74
- create your own controller, independent of the package's controller which adds more actions
63
75
- Mezzio lets you define an array of middleware for a route, so you can register this controller before the package's controller
64
76
65
-
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.
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.
66
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)
67
-
68
-
## Controller plugins
69
-
70
-
Controllers support controller plugins, much like controllers in a Laminas application. The module comes packed with a few common plugins, but you can extend controller functionality with your own plugins too.
71
-
72
-
### Usage
73
-
74
-
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 Laminas 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`.
75
-
76
-
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)
77
-
78
-
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.
79
-
80
-
##### Example
81
-
```php
82
-
//inside a controller
83
-
//assume we've already registered a plugin called testPlugin
84
-
$this->testPlugin(); //will return the TestPlugin class so you can call any public defined method on it
85
-
$this->testPlugin()->someMethod();
86
-
```
87
-
88
-
### Built-in plugins
89
-
Note: Each of these plugins requires the associated Mezzio packages to be installed and available in your project.
90
-
Although these are optional, if a package is missing, the controller will not have the associated functionality available
91
-
92
-
-`template` wraps TemplateInterface provided by Mezzio, to make template engine accessible to any controller
93
-
-`url` wraps the UrlHelper class provided by Laminas helpers package. Used to generate URIs from routes
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`):
0 commit comments