|
8 | 8 | use Nowo\BlogKitBundle\Form\BlogSettingsType; |
9 | 9 | use Nowo\BlogKitBundle\Repository\BlogSettingsRepository; |
10 | 10 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 11 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
11 | 12 | use Symfony\Component\HttpFoundation\Request; |
12 | 13 | use Symfony\Component\HttpFoundation\Response; |
13 | 14 | use Symfony\Component\Routing\Attribute\Route; |
14 | 15 |
|
15 | 16 | /** |
16 | | - * Admin singleton form for professional blog configuration. |
| 17 | + * Admin singleton form for professional blog configuration (one path per section). |
17 | 18 | */ |
18 | 19 | final class BlogSettingsController extends AbstractController |
19 | 20 | { |
| 21 | + /** @var array<string, string> */ |
| 22 | + private const array SECTION_ROUTES = [ |
| 23 | + BlogSettingsType::SECTION_LISTING => 'admin_blog_settings_listing', |
| 24 | + BlogSettingsType::SECTION_CARDS => 'admin_blog_settings_cards', |
| 25 | + BlogSettingsType::SECTION_INDEX_ASIDE => 'admin_blog_settings_index_aside', |
| 26 | + BlogSettingsType::SECTION_ARTICLE => 'admin_blog_settings_article', |
| 27 | + BlogSettingsType::SECTION_COMMENTS => 'admin_blog_settings_comments', |
| 28 | + ]; |
| 29 | + |
20 | 30 | public function __construct( |
21 | 31 | private readonly BlogSettingsRepository $blogSettingsRepository, |
22 | 32 | private readonly EntityManagerInterface $entityManager, |
23 | 33 | ) { |
24 | 34 | } |
25 | 35 |
|
26 | | - #[Route('/admin/blog/settings', name: 'admin_blog_settings', methods: ['GET', 'POST'])] |
27 | | - public function settings(Request $request): Response |
| 36 | + #[Route('/admin/blog/settings', name: 'admin_blog_settings', methods: ['GET'])] |
| 37 | + public function settings(): RedirectResponse |
| 38 | + { |
| 39 | + return $this->redirectToRoute('admin_blog_settings_listing'); |
| 40 | + } |
| 41 | + |
| 42 | + #[Route('/admin/blog/settings/listing', name: 'admin_blog_settings_listing', methods: ['GET', 'POST'])] |
| 43 | + public function listing(Request $request): Response |
| 44 | + { |
| 45 | + return $this->handleSection($request, BlogSettingsType::SECTION_LISTING); |
| 46 | + } |
| 47 | + |
| 48 | + #[Route('/admin/blog/settings/cards', name: 'admin_blog_settings_cards', methods: ['GET', 'POST'])] |
| 49 | + public function cards(Request $request): Response |
| 50 | + { |
| 51 | + return $this->handleSection($request, BlogSettingsType::SECTION_CARDS); |
| 52 | + } |
| 53 | + |
| 54 | + #[Route('/admin/blog/settings/index-aside', name: 'admin_blog_settings_index_aside', methods: ['GET', 'POST'])] |
| 55 | + public function indexAside(Request $request): Response |
| 56 | + { |
| 57 | + return $this->handleSection($request, BlogSettingsType::SECTION_INDEX_ASIDE); |
| 58 | + } |
| 59 | + |
| 60 | + #[Route('/admin/blog/settings/article', name: 'admin_blog_settings_article', methods: ['GET', 'POST'])] |
| 61 | + public function article(Request $request): Response |
| 62 | + { |
| 63 | + return $this->handleSection($request, BlogSettingsType::SECTION_ARTICLE); |
| 64 | + } |
| 65 | + |
| 66 | + #[Route('/admin/blog/settings/comments', name: 'admin_blog_settings_comments', methods: ['GET', 'POST'])] |
| 67 | + public function comments(Request $request): Response |
| 68 | + { |
| 69 | + return $this->handleSection($request, BlogSettingsType::SECTION_COMMENTS); |
| 70 | + } |
| 71 | + |
| 72 | + private function handleSection(Request $request, string $section): Response |
28 | 73 | { |
29 | 74 | $blogSettings = $this->blogSettingsRepository->getSingleton(); |
30 | | - $form = $this->createForm(BlogSettingsType::class, $blogSettings); |
| 75 | + $route = self::SECTION_ROUTES[$section]; |
| 76 | + $form = $this->createForm(BlogSettingsType::class, $blogSettings, [ |
| 77 | + BlogSettingsType::OPTION_SECTION => $section, |
| 78 | + ]); |
31 | 79 | $form->handleRequest($request); |
32 | 80 |
|
33 | 81 | if ($form->isSubmitted() && $form->isValid()) { |
34 | 82 | $this->entityManager->flush(); |
35 | 83 | $this->blogSettingsRepository->reset(); |
36 | 84 | $this->addFlash('success', 'admin.flash.settings_saved'); |
37 | 85 |
|
38 | | - return $this->redirectToRoute('admin_blog_settings'); |
| 86 | + return $this->redirectToRoute($route); |
39 | 87 | } |
40 | 88 |
|
41 | 89 | return $this->render('@NowoBlogKitBundle/admin/settings.html.twig', [ |
42 | | - 'page_title' => 'admin.settings.title', |
43 | | - 'form' => $form, |
| 90 | + 'page_title' => 'admin.settings.title', |
| 91 | + 'form' => $form, |
| 92 | + 'edit_section' => $section, |
44 | 93 | ]); |
45 | 94 | } |
46 | 95 | } |
0 commit comments