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

cwarne's avatar

Livewire Web App + REST API for Mobile?

Are there best practices around creating a Livewire based desktop/web app with separate REST API routes to support a mobile application without duplicate code? I had made a few assumptions and have a few concerns:

  1. Can Livewire components and API Controllers share request validation requirements? (Normally this would go in a Laravel Request class to be shared between web/api but this doesn't seem possible with Livewire. I haven't found a clean way to accomplish this)
  2. Share authorization logic easily as to not duplicate logic? (I'm assuming best practice is to write Policies and use that shared logic for both Livewire and API Controller)
  3. Share application logic? (Using a Repository pattern to move logic out of Controllers and Livewire Components to a central place)

I'd love to hear people's opinions surrounding the need for API routes in conjunction with a Livewire web app. Thank you!

1 like
8 replies
KSU_Laracasts's avatar

Was wondering the same thing. What you said in your original post are my thoughts too. I have more questions than answers though

Mangal Singh's avatar

Hi @cwarne & @ksu_laracasts

I did some research and concluded with the below, please have a look and share your opinion as well.

  1. User spatia fo reauthorization logic as explained in the below video:
https://spatie.be/docs/laravel-permission/v4/basic-usage/basic-usage
https://www.youtube.com/watch?v=jEfGl1R6u3Q

I have implemented it and it worked fabulously.

  1. For application logic use a single-action approach, see below reference:
https://medium.com/@remi_collin/keeping-your-laravel-applications-dry-with-single-action-classes-6a950ec54d1d

If you notice the Fortify & Jetstream action files under app >> Actions folder, they are built like this only.

Fortify is using request validation in other ways and it works out of the box, so I found this approach is very much useful in our case.

  1. Now the combined approach is Livewire components and API resources would be using the same single actions. So, no need to write duplicate code in the system.

Hope this will help!!

Let me know if there are any queries, would be happy to resolve :)

Enjoy!!

martinbean's avatar

@cwarne @ksu_laracasts @mangal singh Take Livewire out of the equation and the same principles apply: if you need to share common logic, extract it to its own class and then re-use as you need to. Livewire doesn’t change anything about this.

johan.vdbroek's avatar

@martinbean I disagree. Due to the fact that Livewire components cannot reuse FormRequest based classes it introduces a design challenge which is unique to Livewire.

martinbean's avatar

@johan.vdbroek If you’re going to bump a question that’s a year old, at least go to the effort of actually reading it.

  1. You don’t use form requests in Livewire components.
  2. I never said anything about form requests.
johan.vdbroek's avatar

@martinbean just pointing out that a big part of the common logic is in validation rules. It's not straightforward to me how to share these rules because Livewire handles them so differently.

alisalehi's avatar

Hi guys. My question was, is it possible to use form requests in Livewire? And the chatGPT offered this to me. 😀

class UserForm extends Form
{
    public $name;
    public $email;
    public $password;

    public function submit()
    {
        $validatedData = $this->validate((new StoreUserRequest)->rules());

        // Or if you want to apply custom messages as well:
        // $validatedData = $this->validate((new StoreUserRequest)->rules(), (new StoreUserRequest)->messages());

        // Save the data
        User::create($validatedData);

        // Additional operations
    }

I have not tried it myself yet. I hope it will be useful

1 like

Please or to participate in this conversation.