#20_save-username-to-cookie #24
2 changed files with 41 additions and 1 deletions
|
@ -2,16 +2,32 @@
|
|||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Form\UserNameFormType;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
final class HomeController
|
||||
final class HomeController extends AbstractController
|
||||
{
|
||||
public const string DEFAULT_USERNAME = 'nobody';
|
||||
#[Route('/', name: 'home')]
|
||||
public function home(UrlGeneratorInterface $router): Response
|
||||
{
|
||||
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 === '') {}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
24
src/Form/UserNameFormType.php
Normal file
24
src/Form/UserNameFormType.php
Normal 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
|
||||
]);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue