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

lararara's avatar

Call Controller Method on Button Click (After initial page render) to return Temp Signed URL

I have a share button that should call the share method in the controller after clicking it. This would generate a temporary signed url and then the frontend react code would get that string and save it to the clipboard.

// Controller
    public function share(Room $room)
    {
        $this->authorize('share', $room);

        return URL::temporarySignedRoute('room.show', now()->addHour(1), [
            'room' => $room,
        ]);
    }

// React Inertia Frontend
    const handleShare = () => {
         // Using router.post (from inertia) will not give me the return response of the signed url so I can use it
         //router.postroute('room.share', room.data.id));
         //navigator.clipboard.writeText(??));
    };

<button className="btn btn-primary" onClick={handleShare}>
  Share
</button>

// web.php routes file

 Route::post('rooms/{room}/share',
    [RoomController::class, 'shareRoom'])->name('room.share');

Using inertia's router does not give me to return response for the controller method, a signed temporary url. The success callbacks would return the entire page objects, when I don't want to refresh the page, as that'd refresh all the other data.

Nor do I want to pass the temporary signed url in the initial page load, as I want to generate it when the user clicks the share button.

Would I need to setup Axios and then do a standard post call to get the results of my controller method?

1 like
4 replies
vincent15000's avatar

That's just strange. Have you checked that you call the right route ?

1 like
lararara's avatar
lararara
OP
Best Answer
Level 2

So I don't believe what I want really is possible per say. Although you can call routes, it doesn't seem like you can return arbitrary data like a standard POST call, since it's necessary to redirect back to some inertia route.

That is to say, a button that would trigger a controller action to return data to use in the React component is not possible without some work arounds.

  1. Return data in the initial render and then use that as the props link. This works, but if I wanted dynamic temporary signed routes for N items, and the user would only really interact with a few of them, it's not the greatest design.
  2. Setup a traditional Post request and use the data after the Post response. This is fine, just the inertia-react starter kit does not include an axios setup so need to deal with that.
  3. Partial Reloads for only the shareURL I can set the share url to be optional and then do a partial reload with only the shareUrl requested. This means all the other data isn't refreshed and the shareUrl is dynamic as initially desired. And the solution I ended up going with
// React Frontend
    const handleShare = () => {
        router.reload({
            only: ['shareUrl'],
            onSuccess: (page) => {
                console.log('Share URL: ' + page.props.shareUrl);

                if (!navigator.clipboard || !page.props.shareUrl) return;

                console.log('Share URL After Check: ' + page.props.shareUrl);
                navigator.clipboard.writeText(page.props.shareUrl as string);
            },
        });
    };

Notice how I use onSuccess page props instead of directly using the shareUrl prop? Because, the shareUrl prop only is populated after I click the shareUrl button twice, a double reload, no idea why.

1 like
vincent15000's avatar

@lararara

That is to say, a button that would trigger a controller action to return data to use in the React component is not possible without some work arounds.

No, you are wrong.

Even if you are using InertiaJS, you can trigger controller actions without Inertia and just return JSON datas as you would do without InertiaJS.

lararara's avatar

@vincent15000 Was there any documentation for this in InertiaJS? I don't see anything where the react frontend triggers the controller action (via router.post) and then gets back a json response.

If I were to call a controller action that returns a json repsonse, there's an error in the frontend saying it requires a valid InertiaJS response instead of a json object.

// React
        router.post(route('room.test-action'));

// Controller
    public function testAction()
    {
        return response()->json([
            'success' => true,
            'message' => 'Action successful',
        ]);
    }

It's also an open issue, https://github.com/inertiajs/inertia-laravel/issues/103, to return raw json format. From my searching around, the easiest solution would be just use axios, but then you'd need to setup all the auth stuff

Please or to participate in this conversation.