I have been struggling all day to find a good convention to name view routes in Laravel (5.4).
Let's say that I am using resource routes. But I also need to declare a lot of view routes, what is a safe and best practice for declaring such routes?
I have the following resource routes:
GET /tickets/12/messages
GET /tickets/12/messages/5
POST /tickets/12/messages
PUT /tickets/12/messages/5
PATCH /tickets/12/messages/5
DELETE /tickets/12/messages/5
I also need to define routes for:
And many others... The question is: How would you define the routes I described?
Many thanks!
The Laravel way to build routes for CRUD is to use the Route::resource()
method:
Route::resource('tickets', 'TicketController');
You can see a list of generated routes with php artisan route:list
command.
https://laravel.com/docs/5.4/controllers#resource-controllers
IMHO you can try to use nested resources, in your case:
Route::resource('tickets.messages', 'TicketMessageController');
Route::resource('users.tickets', 'UserTicketController');
they generate this kind of routes:
tickets/{ticket}/messages/{message}
users/{user}/tickets/{ticket}
In the controller you will have both parameters injected in function calls, i.e.:
class TicketMessageController
// show message for ticket
public function show(Ticket $ticket, Message $message)
// show all messages for a ticket
public function index(Ticket $ticket)
Its documented only for Laravel 5.1 but it works with 5.4
As always you can review all the generated routes with php artisan route:list