diff --git a/app/Controller/HelloWorldController.php b/app/Controller/HelloWorldController.php index ea04b63..aac2684 100644 --- a/app/Controller/HelloWorldController.php +++ b/app/Controller/HelloWorldController.php @@ -13,9 +13,9 @@ use Ardent\Undercurrent\View\ViewInterface; class HelloWorldController { #[Route('/')] - public function index(): ResponseInterface + public function index(): ViewInterface { - return new GenericResponse('Index, Hello'); + return new BaseView('/index.php'); } #[Route('/error')] @@ -30,10 +30,10 @@ class HelloWorldController return new GenericResponse('Hello World!'); } - #[Route('/view')] - public function view(): ViewInterface + #[Route('/view/{name}')] + public function view(string $name): ViewInterface { - return new BaseView('/Template/home.php', ['message' => 'Hello World!']); + return new BaseView('/home.php', ['name' => $name]); } #[Route('/routes/{word}')] diff --git a/app/Template/base.php b/app/Template/base.php index 357b81d..f4cee5c 100644 --- a/app/Template/base.php +++ b/app/Template/base.php @@ -1,10 +1,10 @@ - + - TOSTI MAN KOMT JE HALEN + Base view title - data['slot'] ?> + Base, slot: data['slot'] ?> \ No newline at end of file diff --git a/app/Template/home.php b/app/Template/home.php index 02c6814..ebbbbcd 100644 --- a/app/Template/home.php +++ b/app/Template/home.php @@ -1,4 +1,4 @@ -extends = '/Template/base.php' ?> +extends = '/base.php' ?> -data['message'] ?> +Hello data['name'] ?>! diff --git a/app/Template/index.php b/app/Template/index.php new file mode 100644 index 0000000..9d633a4 --- /dev/null +++ b/app/Template/index.php @@ -0,0 +1,4 @@ + +extends = '/base.php' ?> + +Hello index! diff --git a/src/AppConfig.php b/src/AppConfig.php index bcf0ee6..e3ce075 100644 --- a/src/AppConfig.php +++ b/src/AppConfig.php @@ -6,6 +6,7 @@ class AppConfig { public function __construct( private readonly string $rootPath, + private readonly string $templatePath, ) { } @@ -14,4 +15,9 @@ class AppConfig { return $this->rootPath; } + + public function getTemplatePath(): string + { + return $this->getRootPath() . $this->templatePath; + } } \ No newline at end of file diff --git a/src/Attribute/Route.php b/src/Attribute/Route.php index 7129654..9767137 100644 --- a/src/Attribute/Route.php +++ b/src/Attribute/Route.php @@ -11,6 +11,7 @@ class Route public function __construct( public string $path, public ?MethodEnum $method = null, + public ?string $name = null, ) { } diff --git a/src/Http/GenericRouter.php b/src/Http/GenericRouter.php index ed42ccd..ffcfa06 100644 --- a/src/Http/GenericRouter.php +++ b/src/Http/GenericRouter.php @@ -5,6 +5,7 @@ namespace Ardent\Undercurrent\Http; use Ardent\Undercurrent\AppConfig; use Ardent\Undercurrent\Container\ContainerInterface; use Ardent\Undercurrent\Logger\LoggerInterface; +use Ardent\Undercurrent\View\ViewHelper; use Ardent\Undercurrent\View\ViewInterface; class GenericRouter implements RouterInterface @@ -38,7 +39,10 @@ class GenericRouter implements RouterInterface ); if ($response instanceof ViewInterface) { - return new GenericResponse($response->render($this->container->get(AppConfig::class))); + return new GenericResponse($response->render( + $this->container->get(AppConfig::class), + $this->container->get(ViewHelper::class) + )); } return $response; @@ -47,7 +51,7 @@ class GenericRouter implements RouterInterface throw new RouteNotFoundException($request); } - public function toUri(string $controller, string $method): string + public function toUri(string $controller, string $method, array $params): string { foreach ($this->config->getRoutes() as $route) { if ($route->getController() === $controller && $route->getMethod() === $method) { diff --git a/src/Http/RouterInterface.php b/src/Http/RouterInterface.php index f3fbb1d..a760bea 100644 --- a/src/Http/RouterInterface.php +++ b/src/Http/RouterInterface.php @@ -7,4 +7,6 @@ use Ardent\Undercurrent\View\ViewInterface; interface RouterInterface { public function dispatch(RequestInterface $request): ResponseInterface; + + public function toUri(string $controller, string $method, array $params): string; } \ No newline at end of file diff --git a/src/Kernel/BaseKernel.php b/src/Kernel/BaseKernel.php index 904232e..64f83cc 100644 --- a/src/Kernel/BaseKernel.php +++ b/src/Kernel/BaseKernel.php @@ -14,6 +14,7 @@ use Ardent\Undercurrent\Http\RouterInterface; use Ardent\Undercurrent\Http\StatusEnum; use Ardent\Undercurrent\Logger\LogContainer; use Ardent\Undercurrent\Logger\LoggerInterface; +use Ardent\Undercurrent\View\ViewHelper; use Ardent\Undercurrent\View\ViewInterface; class BaseKernel @@ -26,7 +27,7 @@ class BaseKernel public function run(): void { - $appConfig = new AppConfig($this->rootDirectory); + $appConfig = new AppConfig($this->rootDirectory, '/Template'); $container = (new GenericContainer()); $container @@ -36,6 +37,7 @@ class BaseKernel ->add(GenericContainer::class, fn($container) => $container) ->add(AppConfig::class, fn() => $appConfig) ->add(GenericRouter::class) + ->add(ViewHelper::class) ->add(LogContainer::class); $this->dependencies($container); diff --git a/src/View/BaseView.php b/src/View/BaseView.php index ba9fd44..1c9820b 100644 --- a/src/View/BaseView.php +++ b/src/View/BaseView.php @@ -3,28 +3,29 @@ namespace Ardent\Undercurrent\View; use Ardent\Undercurrent\AppConfig; -use Ardent\Undercurrent\Http\GenericResponse; -use Ardent\Undercurrent\Http\ResponseInterface; class BaseView implements ViewInterface { + private ViewHelper $helper; + public function __construct( - private readonly string $path, - private readonly array $data = [], - private ?string $extends = null, + private readonly string $path, + private readonly array $data = [], + private ?string $extends = null, ) { } - public function render(AppConfig $config): string + public function render(AppConfig $config, ViewHelper $helper): string { - $path = $config->getRootPath() . $this->path; + $this->helper = $helper; + $path = $config->getTemplatePath() . $this->path; ob_start(); include $path; $output = ob_get_clean(); if ($this->extends) { - return (new BaseView($this->extends, $this->data + ['slot' => $output]))->render($config); + return (new BaseView($this->extends, $this->data + ['slot' => $output]))->render($config, $helper); } return $output; diff --git a/src/View/ViewHelper.php b/src/View/ViewHelper.php new file mode 100644 index 0000000..f73f510 --- /dev/null +++ b/src/View/ViewHelper.php @@ -0,0 +1,19 @@ +router->toUri($class, $method, $params); + } +} \ No newline at end of file diff --git a/src/View/ViewInterface.php b/src/View/ViewInterface.php index 8b2b654..29f7d29 100644 --- a/src/View/ViewInterface.php +++ b/src/View/ViewInterface.php @@ -7,5 +7,5 @@ use Ardent\Undercurrent\Http\ResponseInterface; interface ViewInterface { - public function render(AppConfig $config): string; + public function render(AppConfig $config, ViewHelper $helper): string; } \ No newline at end of file