From 2b595c1403f3e97eed0726bdd5c0b22cdbfea8ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Mon, 14 Oct 2024 09:34:23 +0200 Subject: [PATCH] Update star route. --- browser.php | 13 +++++++------ src/Controller/StarController.php | 14 ++++++++++++++ src/templates/product.html.twig | 2 +- src/templates/productList.html.twig | 2 +- 4 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 src/Controller/StarController.php diff --git a/browser.php b/browser.php index 55d1c4d..f0ad083 100644 --- a/browser.php +++ b/browser.php @@ -7,6 +7,7 @@ use Krzysiej\RyobiCrawler\Controller\CategoryController; use Krzysiej\RyobiCrawler\Controller\IndexController; use Krzysiej\RyobiCrawler\Controller\ProductController; use Krzysiej\RyobiCrawler\Controller\SearchController; +use Krzysiej\RyobiCrawler\Controller\StarController; use Krzysiej\RyobiCrawler\Models\Product; use Twig\{Environment, Loader\FilesystemLoader}; use Symfony\Component\Routing\Matcher\UrlMatcher; @@ -17,15 +18,17 @@ use Symfony\Component\Routing\RouteCollection; if (!file_exists('database.sqlite')) { exit('Database file database.sqlite missing. Run docker compose
docker compose exec php-app php index.php app:migrate
to create it.'); } -$productRoute = new Route('browser.php/product/{product_id}', ['_controller' => ProductController::class], ['product_id' => '\d+']); -$searchRoute = new Route('browser.php?search={search_term}', ['_controller' => SearchController::class]); +$productRoute = new Route('/browser.php/product/{product_id}', ['_controller' => ProductController::class], ['product_id' => '\d+']); +$searchRoute = new Route('/browser.php?search={search_term}', ['_controller' => SearchController::class]); $categoryRoute = new Route('/browser.php/category/{category_name}', ['_controller' => CategoryController::class]); -$indexRoute = new Route('browser.php', ['_controller' => IndexController::class]); +$starRoute = new Route('/browser.php/star/{product_id}', ['_controller' => StarController::class]); +$indexRoute = new Route('/browser.php', ['_controller' => IndexController::class]); $routes = new RouteCollection(); $routes->add('product_show', $productRoute); $routes->add('search_show', $searchRoute); $routes->add('category_show', $categoryRoute); +$routes->add('start_show', $starRoute); $routes->add('index_show', $indexRoute); $context = new RequestContext(); @@ -36,12 +39,10 @@ try { die($e->getMessage()); } -//dd($parameters['category_name']); - match ($parameters['_controller']) { SearchController::class => (new $parameters['_controller']())($parameters['search_term']), CategoryController::class => (new $parameters['_controller']())($parameters['category_name']), - ProductController::class => (new $parameters['_controller']())($parameters['product_id']), + ProductController::class, StarController::class => (new $parameters['_controller']())($parameters['product_id']), IndexController::class => (new $parameters['_controller']())(), default => throw new Exception('Route not found') }; diff --git a/src/Controller/StarController.php b/src/Controller/StarController.php new file mode 100644 index 0000000..069437b --- /dev/null +++ b/src/Controller/StarController.php @@ -0,0 +1,14 @@ +toggleStarred()->save(); + header('Location: ' . $_SERVER['HTTP_REFERER']); + } +} diff --git a/src/templates/product.html.twig b/src/templates/product.html.twig index 8634ec6..c56c4ea 100644 --- a/src/templates/product.html.twig +++ b/src/templates/product.html.twig @@ -3,6 +3,7 @@ {% block content %} + @@ -17,7 +18,6 @@
{% if product.starred %}★{% else %} ☆ {% endif %} {{ product.name }} {{ product.name }} {{ product.subTitle }}
- diff --git a/src/templates/productList.html.twig b/src/templates/productList.html.twig index 9d3a418..6b29be5 100644 --- a/src/templates/productList.html.twig +++ b/src/templates/productList.html.twig @@ -4,7 +4,7 @@
{% for product in products %} - +
{% if product.starred %}★{% else %} ☆ {% endif %}{% if product.starred %}★{% else %} ☆ {% endif %} {{ product.name }} {{ product.name }} {{ product.subTitle }}