Delegating Requests
Active controllers can delegate the request to other controllers. This enables the reuse of controllers.
Delegating a Request
If we register the context path /example for the ExampleController below and call it with http://[url-to-n2n-public]/example/foo/bar/baz/qux, the context path /example and the command path /foo/bar/baz/qux apply to the call of this controller. The command path is analyzed to invoke the controller methods. Using n2n\web\http\controller\ControllerAdapter::delegate() this controller can now delegate the request to another controller.
class ExampleController extends ControllerAdapter {
public function doFoo($param, array $delegateParams,
ExampleDelegateController $exampleDelegateController) {
if ($param != 'bar') {
echo 'delegateParams: ' . implode(', ', $delegateParams);
return;
}
$this->delegate($exampleDelegateController, 2);
}
}
In this example doFoo() delegates the request to the ExampleDelegateController, unless $param is not equal to "bar". The second parameter of delegate() determines how many path parts should be moved from the command path to the context path. In the example these are two path parts, so the context path /example/foo/bar and the command path /baz/qux apply to the call of ExampleDelegateController. In principle, the context path contains all path parts that have already been processed.
The ExampleDelegateController could look like this:
class ExampleDelegateController extends ControllerAdapter {
public function doBaz($param) {
echo 'param: ' . $param;
}
}
Automatic Delegating
If you do not pass a second parameter to n2n\web\http\controller\ControllerAdapter::delegate(), delegate() decides itself how many path parts should be moved from the command path to the context path. In this case, for "index" and "do" methods all path parts are moved for which no array parameter is defined in the controller method. For methods that are annotated with a pattern, all path parts are moved for which no multiple modifier is defined.
Example 1
public function index(array $delegateParams,
ExampleDelegateController $exampleDelegateController) {
$this->delegate($exampleDelegateController);
}
Command path and context path remain the same.
Example 2
public function index($param, array $delegateParams,
ExampleDelegateController $exampleDelegateController) {
$this->delegate($exampleDelegateController);
}
public function doFoo(array $delegateParams,
ExampleDelegateController $exampleDelegateController) {
$this->delegate($exampleDelegateController);
}
For both methods, one path part each is moved from the command path to the context path.
Example 3
public function doBar($param, $prama2) {
$this->delegate($this->exampleDelegateController);
}
All three path parts are moved from the command path to the context path.
Example 4
class ExampleController extends ControllerAdapter {
private static function _annos(AnnoInit $ai) {
$as->m('foo', AnnosController::PATH, array('pattern' => 'param:*/*+'));
}
private $exampleDelegateController;
private function _init(ExampleDelegateController $exampleDelegateController) {
$this->exampleDelegateController = $exampleDelegateController;
}
public function foo($param) {
$this->delegate($this->exampleDelegateController);
}
}
One path part is moved from the command path to the context path.