admin管理员组文章数量:1287561
I have a problem with redirectResponse on Symfony 7.2. It works fine with Symfony 6.4.
If I call the redirection directly, everything is OK.If the redirection is called by a form, it does not work.
Here is an example :
<?php
namespace App\Controller;
use App\Form\TestType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class TestController extends AbstractController
{
#[Route(path: '/redirection', name: 'redirection')]
public function redirection(): RedirectResponse
{
return new RedirectResponse('');
}
#[Route(path: '/formulaire', name: 'formulaire')]
public function index(Request $request): RedirectResponse|Response
{
$form = $this->createForm(TestType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
return new RedirectResponse('');
}
return $this->render('test.html.twig', [
'form' => $form,
]);
}
}
// The form (a simple submit button).
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TestType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('submit', SubmitType::class, [])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
]);
}
}
If I consult the "/redirection" route, the redirect happens as expected. If I consult the "/formulaire" route, it gets stuck. I have an error message "Cross-Origin Request Blocked: The "Same Origin" policy does not allow viewing the remote resource located at /. Reason: The CORS header "Access-Control-Allow-Origin" is missing. Status code: 501."
Am I wrong somewhere? Is there a configuration missing somewhere?
I have a problem with redirectResponse on Symfony 7.2. It works fine with Symfony 6.4.
If I call the redirection directly, everything is OK.If the redirection is called by a form, it does not work.
Here is an example :
<?php
namespace App\Controller;
use App\Form\TestType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class TestController extends AbstractController
{
#[Route(path: '/redirection', name: 'redirection')]
public function redirection(): RedirectResponse
{
return new RedirectResponse('https://example');
}
#[Route(path: '/formulaire', name: 'formulaire')]
public function index(Request $request): RedirectResponse|Response
{
$form = $this->createForm(TestType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
return new RedirectResponse('https://example');
}
return $this->render('test.html.twig', [
'form' => $form,
]);
}
}
// The form (a simple submit button).
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TestType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('submit', SubmitType::class, [])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
]);
}
}
If I consult the "/redirection" route, the redirect happens as expected. If I consult the "/formulaire" route, it gets stuck. I have an error message "Cross-Origin Request Blocked: The "Same Origin" policy does not allow viewing the remote resource located at https://example/. Reason: The CORS header "Access-Control-Allow-Origin" is missing. Status code: 501."
Am I wrong somewhere? Is there a configuration missing somewhere?
Share Improve this question edited Feb 26 at 22:46 AshKusa asked Feb 24 at 18:17 AshKusaAshKusa 434 bronze badges 5- Can you share more details about the problem? I don't see how that CORS erroris related to the given code – Nico Haase Commented Feb 26 at 12:31
- Thanks for your help. @hakre It's done. Thanks! – AshKusa Commented Feb 26 at 23:02
- @NicoHaase Me neither. I guess Symfony UX Turbo intercepts the form result (here an external redirect url) to submit it in AJAX or something like that. – AshKusa Commented Feb 26 at 23:02
- Please share more details about this. How is Symfony UX related to the problem at all? The code you've shared doesn't mention it at all – Nico Haase Commented Feb 27 at 7:24
- @NicoHaase I did this test in a brand new installation of symfony 7.2. It seems that "symfony/ux-turbo" is installed by default (webapp). I didn't mention it because at that time, I didn't know about it. It was while looking for why the redirection wasn't working that I found this clue. – AshKusa Commented Feb 27 at 17:13
1 Answer
Reset to default 2I found a solution:
Just modify the form call to disable data-turbo in template:
{{ form_start(form, { 'attr': {'data-turbo': 'false'} } ) }}
This time the redirection goes normally
本文标签: phpRedirect with a form is blockedStack Overflow
版权声明:本文标题:php - Redirect with a form is blocked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741248222a2365295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论