mirror of
https://github.com/getformwork/formwork.git
synced 2025-03-14 19:19:48 +01:00
Add AbstractController::forward()
This commit is contained in:
parent
49cec1913a
commit
4066211cd7
@ -5,8 +5,11 @@ namespace Formwork\Controllers;
|
||||
use Formwork\App;
|
||||
use Formwork\Config\Config;
|
||||
use Formwork\Http\Request;
|
||||
use Formwork\Http\Response;
|
||||
use Formwork\Services\Container;
|
||||
use Formwork\Utils\Str;
|
||||
use Formwork\View\ViewFactory;
|
||||
use InvalidArgumentException;
|
||||
|
||||
abstract class AbstractController
|
||||
{
|
||||
@ -16,6 +19,7 @@ abstract class AbstractController
|
||||
protected string $name;
|
||||
|
||||
public function __construct(
|
||||
private readonly Container $container,
|
||||
protected App $app,
|
||||
protected Config $config,
|
||||
protected ViewFactory $viewFactory,
|
||||
@ -33,4 +37,19 @@ abstract class AbstractController
|
||||
{
|
||||
return $this->viewFactory->make($name, $data)->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Forward the request to another controller
|
||||
*
|
||||
* @param class-string<self> $controller
|
||||
* @param array<string, mixed> $parameters
|
||||
*/
|
||||
protected function forward(string $controller, string $action, array $parameters = []): Response
|
||||
{
|
||||
if (!is_subclass_of($controller, AbstractController::class)) {
|
||||
throw new InvalidArgumentException(sprintf('Controllers must extend %s', AbstractController::class));
|
||||
}
|
||||
$instance = $this->container->build($controller);
|
||||
return $this->container->call($instance->$action(...), $parameters);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user