get previous url symfony 4 in formpage

To get the previous URL in Symfony 4 within a form page, you can make use of the Symfony's RequestStack service. Here's an example of how you can achieve this:

use Symfony\Component\HttpFoundation\RequestStack;

// Inject the RequestStack service into your controller or form type
public function __construct(RequestStack $requestStack)
{
    $this->requestStack = $requestStack;
}

// Within your controller or form type method, retrieve the previous URL
public function getPreviousUrl()
{
    $request = $this->requestStack->getCurrentRequest();
    $referer = $request->headers->get('referer');

    return $referer;
}

In the above code, we inject the RequestStack service into our controller or form type constructor. Then, within the getPreviousUrl method, we retrieve the current request using the RequestStack service and extract the referer (previous URL) from the request headers.

By using this approach, you can easily obtain the previous URL in Symfony 4 within your form page.