1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Trick; |
4
|
|
|
|
5
|
|
|
use App\Repository\CategoryRepository; |
6
|
|
|
use App\Repository\TrickRepository; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
8
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
9
|
|
|
|
10
|
|
|
class TricksByCategoryController extends AbstractController |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var TrickRepository |
15
|
|
|
*/ |
16
|
|
|
private $trickRepository; |
17
|
|
|
/** |
18
|
|
|
* @var CategoryRepository |
19
|
|
|
*/ |
20
|
|
|
private $categoryRepository; |
21
|
|
|
|
22
|
|
|
public function __construct(TrickRepository $trickRepository, CategoryRepository $categoryRepository) |
23
|
|
|
{ |
24
|
|
|
$this->trickRepository = $trickRepository; |
25
|
|
|
$this->categoryRepository = $categoryRepository; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @Route("/trick/category/{categoryId}-{slug}", name="trick.search", methods={"GET"}) |
30
|
|
|
* @param string $categoryId |
31
|
|
|
* @param string $slug |
32
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
33
|
|
|
* show tricks in category |
34
|
|
|
*/ |
35
|
|
|
public function search($categoryId = "", $slug = "") |
36
|
|
|
{ |
37
|
|
|
$categories = $this->categoryRepository->findAll(); |
38
|
|
|
$criteria = array(); |
39
|
|
|
if ($categoryId !== "") { |
40
|
|
|
$category = $this->categoryRepository->find($categoryId); |
41
|
|
|
if($category->getSlug() !== $slug){ |
42
|
|
|
return $this->redirectToRoute('trick.search', [ |
43
|
|
|
'id' => $category->getId(), |
44
|
|
|
'slug' => $category->getSlug() |
45
|
|
|
], 301); |
46
|
|
|
} |
47
|
|
|
$criteria = array('category' => $category->getId()); |
48
|
|
|
} |
49
|
|
|
$tricks = $this->trickRepository->findBy($criteria); |
50
|
|
|
return $this->render('trick/search.html.twig', [ |
51
|
|
|
'tricks' => $tricks, |
52
|
|
|
'categories' => $categories, |
53
|
|
|
'categoryId' =>$categoryId, |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
} |