Skip to content

Commit 870d71c

Browse files
committed
renamed controller event, added new controller event
1 parent 1827c53 commit 870d71c

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
## 0.2.2 - unreleased
1+
## 0.3.0 - unreleased
22

33
### Changed
44
* added the controller as dispatch event parameter
5+
* controller event names
56

67
### Added
7-
* Nothing
8+
* new controller event `ControllerEvent::EVENT_CONTROLLER_AFTER_DISPATCH` after dispatching, which allows you to capture the returned response, and possible override the response generation
89

910
### Deprecated
1011
* Nothing
1112

1213
### Removed
13-
* Nothing
14+
* `ControllerEvent::EVENT_CONTROLLER_DISPATCH` renamed to `ControllerEvent::EVENT_CONTROLLER_BEFORE_DISPATCH`
1415

1516
### Fixed
1617
* Nothing

src/AbstractActionController.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function dispatch(): ResponseInterface
3131
$action = AbstractController::getMethodFromAction($action);
3232

3333
if (method_exists($this, $action)) {
34-
$r = $this->dispatchEvent(ControllerEvent::EVENT_CONTROLLER_DISPATCH, [
34+
$r = $this->dispatchEvent(ControllerEvent::EVENT_CONTROLLER_BEFORE_DISPATCH, [
3535
'request' => $this->request,
3636
'delegate' => $this->getDelegate(),
3737
'controller' => $this,
@@ -42,7 +42,15 @@ public function dispatch(): ResponseInterface
4242
}
4343

4444
$this->request = $r->getParam('request');
45-
return $this->$action();
45+
$response = $this->$action();
46+
$params = array_merge($r->getParams(), ['response' => $response]);
47+
48+
$r = $this->dispatchEvent(ControllerEvent::EVENT_CONTROLLER_AFTER_DISPATCH, $params);
49+
if ($r instanceof ResponseInterface) {
50+
return $r;
51+
}
52+
53+
return $response;
4654
}
4755

4856
//just go the the next middleware, it will eventually hit a 404 if no one handles the request

src/Event/ControllerEvent.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
*/
1818
class ControllerEvent extends Event
1919
{
20-
const EVENT_CONTROLLER_DISPATCH = 'event.controller.dispatch';
20+
const EVENT_CONTROLLER_BEFORE_DISPATCH = 'event.controller.beforeDispatch';
21+
const EVENT_CONTROLLER_AFTER_DISPATCH = 'event.controller.afterDispatch';
2122
}

0 commit comments

Comments
 (0)