使用资源路由时命名Laravel视图路由

Problem

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?

Example:

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:

  • Displaying the view to edit a particular message.
  • Displaying the view to add a particular message to a ticket.
  • Displaying the view to create a particular ticket.
  • Displaying the view to show all the tickets in the system.
  • Displaying the view to show all the tickets that belong to a particular user.

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