Redirect really does not works at all!

in web.php

            Route::get('/', function () {
                return view('main');
            })->name('main');  //  i have 'name' route 

in PostComponent.php


 function mount( Request $request) {

        $this->post_cat_id = $request->post_cat_id;
                
        if( ! $this->post_cat_id){
            return $this->redirect('/'); // fail 
            return $this->redirect()->route('main') ; // fail 
            return redirect()->route('main') ; // fail 
            return redirect()->to('/'); // fail 
            
        } 
}

All goes failed.

How can I redirect in mount function ?

I just used Request class for getting query string from url

 function mount( Request $request) {
}

Thanks.

I have tried this just now in my mount method and the redirect worked.

Have you tried testing the redirect as the first line in the mount method?

Not working in my case.

I tried even this.
but not luck.

protected $listeners = [
        'refreshComponent' => '$refresh'
    ];
    
function mount(){
    $this->emit('refreshComponent');
}

And. I found another weird thing…

Chrome blocking Ajax call .

Is there a reason to redirect from the mount() method? Maybe I am completely wrong, but my guess is that Livewire just ignores what is returned from the mount() method.
Try to redirect from the render() method.

Have you tried
return redirect(route('main'));

@flex-park

In render function it failed too…

I just wan to redirect to main page when the necessary parameters does not come together .

I guess Mount function run before sending HTML headers or HTML body. and it is good to redirect

before display any information.

Anyway thanks for reply.

Yes. I tried but failed…

 $this->redirect(route('route-name', $params));

I got solved it.

Sorry for all guys.

There was some miss match HTML tag. That caused error.

I found that livewire is so strict to parse HTML tags, specially matching with end tag.

After fixing the tags trouble , even the blocking and canceling the request of ajax to server was gone away.

It was not problem of livewire or something.

Thanks for all guys who had concerned with my post.

1 Like