Commit 3325d0f6 authored by Florian Vick's avatar Florian Vick
Browse files

add support for redirects

parent a49bec0a
......@@ -13,6 +13,7 @@ use RoflCopter24\SymfonyLivewireBundle\Exception\PropertyNotFoundException;
use RoflCopter24\SymfonyLivewireBundle\Service\ComponentServicesInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\TemplateWrapper;
abstract class LivewireComponent implements ContainerAwareInterface
......@@ -40,6 +41,10 @@ abstract class LivewireComponent implements ContainerAwareInterface
private ?string $id;
public string $redirectTo;
public int $redirectStatus;
public function __construct(ComponentServicesInterface $componentServices)
{
......@@ -148,6 +153,40 @@ abstract class LivewireComponent implements ContainerAwareInterface
return $this->services->get('twig')->load($name);
}
/**
* Redirect the user to a url like 'https://example.com' or
* '/admin/dashboard'.
* If you want to use a Symfony route, use $this->redirectRoute!
*
* @param string $url The URL to redirect to.
* @param int $status The optional statusCode of the redirect response. Defaults to 302.
*/
protected function redirect(string $url, int $status = 302): void
{
$this->redirectTo = $url;
$this->redirectStatus = $status;
$this->shouldSkipRender = true;
}
/**
* Redirect the user to a Symfony route with the given parameters.
*
* @param string $name The name of the symfony route like 'app.login'
* @param array $parameters Optional GET parameters for the route like [uid => 2]
* @param bool $absolute Whether the generated URL should be absolute or relative
* @param int $status The optional statusCode of the redirect response. Defaults to 302.
*/
protected function redirectRoute(string $name, array $parameters = [], bool $absolute = true, int $status = 302)
{
$this->redirectTo = $this->services
->urlGenerator()->generate($name, $parameters,
$absolute ? UrlGeneratorInterface::ABSOLUTE_PATH : UrlGeneratorInterface::RELATIVE_PATH);
$this->redirectStatus = $status;
$this->shouldSkipRender = true;
}
public function hydrate(LivewireRequestData $requestData): void {}
public function dehydrate(LivewireResponseData $responseData): void {}
......
<?php
/**
* based on https://github.com/livewire/livewire/blob/v2.3.8/src/RenameMe/SupportRedirects.php
*/
declare(strict_types=1);
namespace RoflCopter24\SymfonyLivewireBundle\EventListener;
use RoflCopter24\SymfonyLivewireBundle\Event\MiddlewareDehydrationEvent;
use RoflCopter24\SymfonyLivewireBundle\Event\MiddlewareHydrationEvent;
class SupportRedirects implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
{
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
'livewire.component.hydrate' => ['onHydration', 0],
'livewire.component.dehydrate' => ['onDehydration', 0]
];
}
public function onHydration(MiddlewareHydrationEvent $hydrationEvent): void
{
// ?
}
public function onDehydration(MiddlewareDehydrationEvent $dehydrationEvent): void
{
$component = $dehydrationEvent->getComponent();
if (empty($component->redirectTo)) {
return;
}
$response = $dehydrationEvent->getResponseData();
$response->effects['redirect'] = $component->redirectTo;
$response->effects['html'] = $response->effects['html'] ?? '<div></div>';
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment