Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.13][Admin] Referer redirection after a custom crud update seems broken #988

Open
jeromeengeln opened this issue Feb 21, 2025 · 2 comments

Comments

@jeromeengeln
Copy link

jeromeengeln commented Feb 21, 2025

Sylius version affected: 1.13.10

Issue also created here : Sylius/Sylius#17686

Description
Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration manage crud redirection after update.
According to the function :

public function getRedirectRoute($name)
    {
        $redirect = $this->parameters->get('redirect');

        if (null === $redirect) {
            return $this->getRouteName($name);
        }

        if (is_array($redirect)) {
            if (!empty($redirect['referer'])) {
                return 'referer';
            }

            return $redirect['route'];
        }

        return $redirect;
    }

There is a possibility to configure custom ressource crud redirection called 'referer'.
And allow user to go back on the previous request url.
Actually, by default, the user is redirected to the show route, according to Sylius\Bundle\ResourceBundle\Controller\RedirectHandler file :

public function redirectToResource(RequestConfiguration $configuration, ResourceInterface $resource): Response
    {
        try {
            return $this->redirectToRoute(
                $configuration,
                (string) $configuration->getRedirectRoute(ResourceActions::SHOW),
                $configuration->getRedirectParameters($resource),
            );
        } catch ...
    }

The expected behavior, after configuring my referer redirection in my route configuration is that the function getRedirectRoute return referer.

public function redirectToRoute(RequestConfiguration $configuration, string $route, array $parameters = []): Response
    {
           if ('referer' === $route) {
                 return $this->redirectToReferer($configuration);
           }
....

But it's not working, because now Sylius rework the route name and add a prefix.
For exemple :

public function getRedirectRoute($name)
    {
        $redirect = $this->parameters->get('redirect');
        dump($redirect);

The dump return 'app_admin_test_referer' instead of the configured value.

Steps to reproduce
Create a custom resource admin and add a referer redirection and in your routes.yaml add redirection: referer:

custom_resource_test_admin:
  resource: |
    alias: custom_resource.test
    section: admin
    redirect: referer

Possible Solution
Into Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration change way to detect 'referer' value.

if (is_string($redirect) and str_ends_with($redirect, 'referer')) 
@loic425
Copy link
Member

loic425 commented Feb 25, 2025

@jeromeengeln thx for opening that issue

But it's not working, because now Sylius rework the route name and add a prefix.

Where is it?

@jeromeengeln
Copy link
Author

@jeromeengeln thx for opening that issue

But it's not working, because now Sylius rework the route name and add a prefix.

Where is it?

There is another problem :
In the getRedirectRoute function, this code is not reachable :

if (is_array($redirect)) {
            if (!empty($redirect['referer'])) {
                return 'referer';
            }
            return $redirect['route'];
        }

Because $this->parameters->get('redirect') only accept scalar values. so is_array($redirect) will always return false.

Second problem :

If you configure $redirect = $this->parameters->get('redirect'); as :

custom_resource_test_admin:
  resource: |
    alias: custom_resource.test
    section: admin
    redirect: referer

instead of get a string with 'referer' value as configured, you'll get a reworked and prefixed string value. I don't know why this configuration value is now considered as a route name.

dd($redirect);
src/Bundle/Controller/RequestConfiguration.php on line 163:
"sylius_admin_toto_referer"

Conclusion :

To make referer redirection working, we have to take care about a string value and not array anymore.
And test value ending with "referer" string instead of testing if it's equal to 'referer' as declared in my configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants