mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-28 21:16:13 +00:00
Add ChillUrlGenerator and ChillUrlGeneratorInterface
Introduced a new interface ChillUrlGeneratorInterface for URL generation with return path handling. Implemented this interface in the ChillUrlGenerator class, which uses Symfony components to manage URL generation and request information.
This commit is contained in:
parent
75005b4ed6
commit
5a467ae38d
43
src/Bundle/ChillMainBundle/Routing/ChillUrlGenerator.php
Normal file
43
src/Bundle/ChillMainBundle/Routing/ChillUrlGenerator.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Routing;
|
||||
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
final readonly class ChillUrlGenerator implements ChillUrlGeneratorInterface
|
||||
{
|
||||
public function __construct(private UrlGeneratorInterface $urlGenerator, private RequestStack $requestStack) {}
|
||||
|
||||
public function generate(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
|
||||
{
|
||||
return $this->urlGenerator->generate($name, $parameters, $referenceType);
|
||||
}
|
||||
|
||||
public function generateWithReturnPath(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
|
||||
{
|
||||
$uri = $this->requestStack->getCurrentRequest()->getRequestUri();
|
||||
|
||||
return $this->urlGenerator->generate($name, [$parameters, 'returnPath' => $uri], $referenceType);
|
||||
}
|
||||
|
||||
public function returnPathOr(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
|
||||
{
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
|
||||
if ($request->query->has('returnPath')) {
|
||||
return $request->query->get('returnPath');
|
||||
}
|
||||
|
||||
return $this->urlGenerator->generate($name, $parameters, $referenceType);
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Routing;
|
||||
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
/**
|
||||
* Interface for generating URLs with returnPath information.
|
||||
*/
|
||||
interface ChillUrlGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* Generate an URL without any return path. This is the same as using @see{UrlGeneratorInterface::generate}.
|
||||
*/
|
||||
public function generate(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string;
|
||||
|
||||
/**
|
||||
* Generate an url, append a return path in it.
|
||||
*/
|
||||
public function generateWithReturnPath(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string;
|
||||
|
||||
/**
|
||||
* Get the return path or, if any, generate an url.
|
||||
*/
|
||||
public function returnPathOr(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string;
|
||||
}
|
@ -22,3 +22,8 @@ services:
|
||||
class: Chill\MainBundle\Routing\MenuTwig
|
||||
tags:
|
||||
- { name: twig.extension }
|
||||
|
||||
Chill\MainBundle\Routing\ChillUrlGenerator: ~
|
||||
|
||||
Chill\MainBundle\Routing\ChillUrlGeneratorInterface:
|
||||
alias: 'Chill\MainBundle\Routing\ChillUrlGenerator'
|
||||
|
Loading…
x
Reference in New Issue
Block a user