https://github.com/dotkernel/dot-controller
DotKernel controller like middleware component
https://github.com/dotkernel/dot-controller
Last synced: about 2 months ago
JSON representation
DotKernel controller like middleware component
- Host: GitHub
- URL: https://github.com/dotkernel/dot-controller
- Owner: dotkernel
- License: mit
- Created: 2016-09-05T16:38:53.000Z (over 9 years ago)
- Default Branch: 4.0
- Last Pushed: 2025-02-28T05:51:40.000Z (10 months ago)
- Last Synced: 2025-04-28T15:50:09.063Z (8 months ago)
- Language: PHP
- Homepage: https://docs.dotkernel.org/dot-controller/
- Size: 366 KB
- Stars: 2
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# dot-controller
This is Dotkernel's controller package that can be use like middleware inside 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 and event listeners.
## Documentation
Documentation is available at: https://docs.dotkernel.org/dot-controller/.
## Badges


[](https://github.com/dotkernel/dot-controller/issues)
[](https://github.com/dotkernel/dot-controller/network)
[](https://github.com/dotkernel/dot-controller/stargazers)
[](https://github.com/dotkernel/dot-controller/blob/4.0/LICENSE.md)
[](https://github.com/dotkernel/dot-controller/actions/workflows/continuous-integration.yml)
[](https://codecov.io/gh/dotkernel/dot-controller)
[](https://github.com/dotkernel/dot-controller/actions/workflows/static-analysis.yml)
## Installation
Install `dot-controller` by executing the following Composer command:
```shell
composer require dotkernel/dot-controller
```
## Usage
Middleware controllers act as a handler for multiple routes. Some conventions were made:
- 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}]`)
- 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`.
- 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.
In order to create your action based controllers, you must extend the abstract class `Dot\Controller\AbstractActionController`.
### Example
Creating a UserController with default action and a register action. Will handle routes `/user` and `/user/register`.
```php
use Dot\Controller\AbstractActionController;
class UserController extends AbstractActionController
{
public function indexAction()
{
//...
}
public function registerAction()
{
//...
}
}
```
Then register this controller as a routed middleware in file `RoutesDelegator.php` just like a regular middleware.
```php
//Example from a RoutesDelegator
$app->route(
'/user[/{action}]',
UserController::class,
[RequestMethodInterface::METHOD_GET, RequestMethodInterface::METHOD_POST],
'user'
);
```
### Multiple controllers for the same route
**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:
- create your own controller, independent of the package's controller which adds more actions
- Mezzio 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.
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.
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).