Skip to content

v7.0

Choose a tag to compare

@bjorvack bjorvack released this 04 Jan 12:33
· 155 commits to main since this release
e4352f1

Changes

Logout no longer needs a controller

Symfony 6.4 and up no longer need a controller and route for logout functionality. Use the following config in the security.yaml file to enable the logout

firewalls:
    main:
        logout:
            path: app_logout
            # target: app_any_route

Do make sure the routes/security.yaml file isn't empty

_security_logout:
  resource: security.route_loader.logout
  type: service

Map $_GET params using #[MapQueryParameters]

GET params no longer need to be got from the query parameters in request. Just add the MapQueryParameter attribute to the controller action parameter to populate it dynamically

    #[Route('/admin/users')]
    #[Breadcrumb('users')]
    public function __invoke(
        Request $request,
        UserRepository $userRepository,
    ): Response {
        $page = $request->query->getInt('page', 1);

        $form = $this->createForm(FilterType::class, new FilterDataTransferObject() );
        $form->handleRequest($request);

        $paginatedUsers = $userRepository->getAllFilteredUsers($form->getData());
        $paginatedUsers->paginate($page);

        return $this->render('user/admin/overview.html.twig', [
            'form' => $form->createView(),
            'users' => $paginatedUsers,
        ]);
    }

becomes

    #[Route('/admin/users')]
    #[Breadcrumb('users')]
    public function __invoke(
        Request $request,
        UserRepository $userRepository,
        #[MapQueryParameter] ?int $page = 1
    ): Response {
        $form = $this->createForm(FilterType::class, new FilterDataTransferObject() );
        $form->handleRequest($request);

        $paginatedUsers = $userRepository->getAllFilteredUsers($form->getData());
        $paginatedUsers->paginate($page);

        return $this->render('user/admin/overview.html.twig', [
            'form' => $form->createView(),
            'users' => $paginatedUsers,
        ]);
    }

Impersonating users

When impersonating users we no longer need to manually need to define magic get properties in the path. We kan now use the build in twig methods to generate te correct links

Impersonate

<a class="text-nowrap" href="{{ path('profile', {'_switch_user': user.username}) }}">
  <i class="fas fa-sign-in-alt" title="{{ 'Switch to user'|trans }}" data-toggle="tooltip" data-placement="top"></i>
</a>

becomes

<a class="text-nowrap" href="{{ impersonation_path(user.username) }}">
  <i class="fas fa-sign-in-alt" title="{{ 'Switch to user'|trans }}" data-toggle="tooltip" data-placement="top"></i>
</a>

Exit impersonation

<a href="{{ path('user_overview', {'_switch_user': '_exit'}) }}">{{ 'user.actions.switchback'|trans|ucfirst }}</a>

becomes

<a href="{{ impersonation_exit_path(path('user_overview')) }}" class="dropdown-item">{{'user.actions.switchback'|trans|ucfirst }}</a>

Routes can be accessed by their FQCN

Routes no longer need to define a name to be accessed bij the router. When no name is provided a generated name based on the FQCN is used.

For example the App\Controller\User\Admin\OverviewController file would us the path name user_overview since it's defined

#[Route('/admin/users', name: 'user_overview')]
#[Breadcrumb('users')]
public function __invoke(
    Request $request,
    UserRepository $userRepository,
    #[MapQueryParameter]
    ?int $page = 1
): Response ...

If the name isn't defined it would be app_user_admin_overview

#[Route('/admin/users')]
#[Breadcrumb('users')]
public function __invoke(
    Request $request,
    UserRepository $userRepository,
    #[MapQueryParameter]
    ?int $page = 1
): Response ...

In both cases the FQCN can be used in the router to point at the correct path

return $this->redirectToRoute(OverviewController::class);

if a specific method needs to be called this can be added as well

return $this->redirectToRoute(OverviewController::class.'::index');

New Contributors

Full Changelog: https://github.com/sumocoders/Framework-User-Implementation-Example/commits/v7.0