Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LaCoder's avatar

Listen to events while redirect

Hello, I have X and Y component, in X component, I am dispatching event with data, but with redirect to another route, where another Y component is loading.

    // Method to emit event with table name and navigate to the desired route
    public function navigateToOrdering($tableName)
    {
        // Emit event to OrderItemBlock component with tableName
        $this->dispatch('navigateToOrdering', $tableName)->to(OrderItemBlock::class);

        // Navigate to the pos.ordering route without passing tableName in URL
        return redirect()->route('pos.ordering');
    }

in Y component I am listening as below,

    #[On('navigateToOrdering')] 
    public function receiveTableName($tableName)
    {
        $this->tableName = $tableName;
        dd($this->tableName);
        // Handle the received tableName as needed in this component
    }

But I could not get output of dd means tableName,

will this dispatch work in redirect? or any other way to move to new page with dispatch?

0 likes
10 replies
renatoniola's avatar

Hi ->to(OrderItemBlock::class); means that only that type of component will listen to the event. is the Y component of that type?

renatoniola's avatar

@LaCoder what about named parameters? $this->dispatch('navigateToOrdering', tableName: $tableName)->to(OrderItemBlock::class);

Snapey's avatar

no, this will not work. When you dispatch the event the code runs immediately, and only registered listeners will hear it. You then issue a redirect which ends the request and starts a new request. Your event has long gone by this point.

You should redirect with the data you need to pass in the route.

LaCoder's avatar

@Snapey yep, i thought so initially, but I don't want to pass it in url, is there other way to pass without using route?

LaCoder's avatar

@Snapey Nothing afraid, Ok, if i pass it in route with return $this->redirectRoute('pos.ordering', ['tableName' => $tableName]);, can I get it access in another component directly? or I need to get in the controller, and then pass to view - to another component?

Snapey's avatar

@LaCoder of course. but you might want to amend your pos.ordering route to expect tablename

renatoniola's avatar

what about moving the redirect into receiveTableName function?

Please or to participate in this conversation.