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

_zac_'s avatar
Level 12

Livewire with Route::resource which option is better/safest?

When building using a Laravel controller and Livewire - I want to render a Livewire full-page component and this does not work from the controller. Is it better to:

a - add a new line in the route:

Route::resource('event', EventController::class);
Route::get('/event/create', EventForm::class);

b, force the controller to render the form:

  public function create() {
    $this->authorize('create', Event::class);
    return Router::convertToControllerAction((EventForm::class));
  }
0 likes
2 replies
LaryAI's avatar
Level 58

When integrating Livewire with Laravel's Route::resource, the goal is to ensure that your routes are clean and maintainable while leveraging Livewire's full-page components effectively. Let's evaluate the two options you provided:

Option A: Adding a New Line in the Route

Route::resource('event', EventController::class);
Route::get('/event/create', EventForm::class);

Pros:

  • Simplicity: This approach is straightforward. You directly map the /event/create route to the EventForm Livewire component.
  • Separation of Concerns: It keeps the controller and Livewire component responsibilities separate, which aligns with the single responsibility principle.
  • Flexibility: You can easily manage and update the Livewire component without affecting the controller logic.

Cons:

  • Route Duplication: You have to manually add routes for Livewire components, which can lead to duplication if not managed properly.

Option B: Forcing the Controller to Render the Form

public function create() {
    $this->authorize('create', Event::class);
    return Router::convertToControllerAction((EventForm::class));
}

Pros:

  • Centralized Logic: Keeps all route handling within the controller, which might be preferred if you want to maintain a single entry point for route logic.

Cons:

  • Complexity: This approach introduces additional complexity by forcing the controller to handle Livewire components, which can make the code harder to understand and maintain.
  • Tight Coupling: It couples the controller logic with Livewire, which can reduce flexibility and make future changes more cumbersome.

Recommendation

Option A is generally the better and safer choice. It maintains a clear separation between your controller logic and Livewire components, making your application easier to maintain and extend. It also aligns with Laravel's philosophy of keeping things simple and expressive.

If you find yourself needing to add many such routes, consider grouping them or using route macros to keep your route definitions clean and organized.

1 like
cookie's avatar

I've prefer to not forcing Larvel to do anything go with the first choice. I don't like duplications, but it's just in one file, the routes file. So I'm fine with it.

A few days ago I've "discovered" MaryUI and saw how the website and the docs are made so I've decided to give Volt a chance. What should I sa? - I really love it.

You also may should try it: https://livewire.laravel.com/docs/volt

Please or to participate in this conversation.