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

Cannot wp_redirect inside Components anymore #349

Closed
eavonius opened this issue Feb 6, 2024 · 3 comments
Closed

Cannot wp_redirect inside Components anymore #349

eavonius opened this issue Feb 6, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@eavonius
Copy link
Contributor

eavonius commented Feb 6, 2024

Version

4.0.2

What did you expect to happen?

Previously if I added a wp_redirect call in the constructor of a component, it would go to the new page. I usually do a check to know if it's a post or not (this is for a form post being handled by the component). Regardless, wp_redirect doesn't do anything. I do have exit; called right after it.

What actually happens?

The same page appears.

Steps to reproduce

  1. Create an acorn component
  2. Add it to a blade template
  3. Add a call to wp_redirect to the constructor
  4. Hit the page

System info

No response

Log output

No response

Please confirm this isn't a support request.

No

@eavonius eavonius added the bug Something isn't working label Feb 6, 2024
@retlehs
Copy link
Member

retlehs commented Feb 6, 2024

Can you please post some actual code needed to reproduce this?

@eavonius
Copy link
Contributor Author

eavonius commented Feb 7, 2024

  1. Create a file under resources/views/components/forms/test-form.blade.php with the following:

    <form method="post">
       <input type="text" name="test" />
       <input type="submit" />
    </form>
  2. Create a file under app/View/Components/Forms/TestForm.php with the contents:

    <?php
    declare(strict_types=1);
    
    namespace App\View\Components\Forms;
    
    use Roots\Acorn\View\Component;
    use Illuminate\Http\Request;
    
    class TestForm extends Component
    {
        public function __construct(Request $request)
        {
            if ($request->isMethod("post")) {
                error_log("Redirecting");
    
                wp_redirect('/thank-you/');
                exit;
            }
        }
    
        public function render()
        {
            error_log("Rendering");
    
            return $this->view('components.forms.test-form');
        }
    }
  3. Add the component to the index.blade.php for your theme:

    <x-forms.test-form />
    
  4. Bring up the page. Type "test" in the input field and click submit. The following error occurs in Wordpress (where this would simply redirect to the URL /thank-you/ before:

    [07-Feb-2024 19:51:29 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /workspaces/**redacted**/wordpress/web/wp/wp-includes/class-wp-styles.php:214) in /workspaces/**redacted**/wordpress/web/wp/wp-includes/pluggable.php on line 1435
    [07-Feb-2024 19:51:29 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /workspaces/**redacted**/wordpress/web/wp/wp-includes/class-wp-styles.php:214) in /workspaces/**redacted**/wordpress/web/wp/wp-includes/pluggable.php on line 1438
    

@Log1x
Copy link
Member

Log1x commented Mar 18, 2024

I am going to close this issue as nothing has changed in Acorn related to how view components are handled. If this was broken/changed at some point, it would've been done in an upstream change in Laravel or it could possibly be related to roots/sage#3167

@Log1x Log1x closed this as not planned Won't fix, can't repro, duplicate, stale Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants