#20_save-username-to-cookie #24

Merged
lubiana merged 3 commits from #20_save-username-to-cookie into main 2024-06-30 07:58:09 +00:00
2 changed files with 41 additions and 1 deletions
Showing only changes of commit 9d81464fd8 - Show all commits

View file

@ -2,16 +2,32 @@
namespace App\Controller; namespace App\Controller;
use App\Form\UserNameFormType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
final class HomeController final class HomeController extends AbstractController
{ {
public const string DEFAULT_USERNAME = 'nobody';
#[Route('/', name: 'home')] #[Route('/', name: 'home')]
public function home(UrlGeneratorInterface $router): Response public function home(UrlGeneratorInterface $router): Response
{ {
return new RedirectResponse($router->generate('app_food_order_index')); return new RedirectResponse($router->generate('app_food_order_index'));
} }
public function usernameForm(Request $request): Response
{
$username = $request->cookies->get('username', self::DEFAULT_USERNAME);
$form = $this->createForm(UsernameFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$username = $form->getData()['username'] ?? self::DEFAULT_USERNAME;
if ($username === '') {}
}
}
} }

View file

@ -0,0 +1,24 @@
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class UserNameFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('username')
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
// Configure your form options here
]);
}
}