skip to content

In Laravel, how do names(), parameters() and Route::resourceVerbs customise the route names, placeholders and URI verbs a resource registers?

level: middleimportance: nice to knowfreq 22%

answer

  1. names(['create' => ...]) per action
  2. names('catalogue.books') as a base
  3. parameters(['books' => 'volume'])
  4. resourceVerbs in AppServiceProvider::boot
  5. hyphens become underscores

basics

~20 s

names() overrides route names, per action with an array or for every action with a base string; parameters() renames the {book} placeholder; Route::resourceVerbs(), called in a service provider's boot, replaces the create and edit URI segments for every resource registered afterwards.

solid answer

~30 s

Three knobs change a resource's generated routes. `->names(['create' => 'books.add'])` overrides chosen route names, `->names('catalogue.books')` swaps the base so every action becomes `catalogue.books.index` and so on, and `->name('create', 'books.add')` sets one. `->parameters(['books' => 'volume'])` (or `->parameter('books', 'volume')`) renames the placeholder to `{volume}`, so actions must type-hint `Book $volume` for binding to match; `Route::resourceParameters([...])` does it globally, and hyphens always become underscores. `Route::resourceVerbs(['create' => 'nuevo', 'edit' => 'editar'])` replaces the `create` and `edit` URI segments; it is global static state, so call it early, in `AppServiceProvider::boot()`, before the routes load. The actions and their method names never change.

code

php · 11 lines
php
<?php

use App\Http\Controllers\BookController;
use App\Models\Book;
use Illuminate\Support\Facades\Route;

Route::resource('books', BookController::class)
    ->names(['create' => 'books.add'])
    ->parameters(['books' => 'volume']);

// BookController::show(Book $volume) now matches /books/{volume}

go deeper

for a junior

Know that resource route names and placeholders can be renamed without writing the routes by hand.

for a middle

Explain the array versus string forms of names(), how parameters() interacts with binding, and why resourceVerbs belongs in boot().

for a senior

Use these overrides to preserve legacy or localised URLs without breaking named-route references, and avoid them where a better resource name suffices.

for a principal

Set naming conventions for route names and URLs so overrides stay rare and documented.

## What can be customised `Route::resource()` generates **route names**, **URI placeholders** and two **URI verbs** (`create`, `edit`) from conventions. Each can be overridden without giving up the resource helper. | Convention | Default for `books` | Override | |---|---|---| | Route names | `books.index`, `books.show`, ... | `->names()`, `->name()` | | Placeholder | `{book}` | `->parameters()`, `->parameter()`, `Route::resourceParameters()` | | Form-page URI segments | `/books/create`, `/books/{book}/edit` | `Route::resourceVerbs()` | The controller's **method names** (`index`, `create`, `store`, ...) never change; only the routes around them do. ## Route names - **Per action**: `->names(['create' => 'books.add', 'store' => 'books.save'])`. Listed actions use the given name verbatim; the others keep the default. - **One action**: `->name('create', 'books.add')` is the single-entry form. - **New base for all**: `->names('catalogue.books')` produces `catalogue.books.index`, `catalogue.books.show`, and so on. Changing names breaks every place that generates a URL from the old name, so it is best done before views and redirects reference them. ## Placeholders The placeholder defaults to the singular of the resource segment, with hyphens replaced by underscores (`audio-books` → `{audio_book}`). ```php Route::resource('books', BookController::class) ->parameters(['books' => 'volume']); // /books/{volume} ``` - Keys are **resource segment names**, values the new placeholder. - Implicit model binding matches placeholder and method variable by name, so actions must become `show(Book $volume)`. - `Route::resourceParameters(['books' => 'volume'])` sets a global map for every resource registered afterwards. - `Route::singularResourceParameters(false)` turns off singularising, giving `{books}`. ## URI verbs `create` and `edit` are the only literal words in resource URIs. `Route::resourceVerbs()` replaces them, for instance to localise URLs: ```php public function boot(): void { Route::resourceVerbs([ 'create' => 'nuevo', 'edit' => 'editar', ]); } ``` `/libros/nuevo` and `/libros/{libro}/editar` follow from `Route::resource('libros', LibroController::class)`, since the placeholder is the singular of the segment. Key points: 1. The verbs are stored in **static** state on the resource registrar, so they affect **every** resource registered after the call. 2. Call it in `AppServiceProvider::boot()`, which runs before the route files are loaded. 3. Calling it with no arguments returns the current verb map. 4. Only URIs change; route names stay `libros.create` and `libros.edit`. ## When to use these - **Legacy URLs** that must keep their shape during a rewrite. - **Localised** public URLs. - **Domain wording** where the singular of the segment reads badly, such as renaming a `{datum}` placeholder. - **Admin areas** whose names need a distinct base to avoid collisions with public routes. Each override makes the app a little less conventional, so prefer choosing a resource name that produces good defaults, and reach for these knobs only where the default genuinely does not fit. ## Common mistakes - **Renaming the placeholder but not the action variables**, which leaves the model unbound and injects an empty instance instead. - **Calling `resourceVerbs()` inside a routes file after some resources are already registered**, producing a mix of English and localised URIs. - **Expecting `names()` to change URIs**: it changes only names; the URI shape comes from the resource name, the placeholder and the verbs. - **Using `names()` to fix a collision** that really comes from registering the same resource twice; remove the duplicate instead. - **Forgetting that `resourceParameters()` is global**, so a map added for one resource silently renames placeholders on another with the same segment name.

  • What breaks if you rename the placeholder with parameters() but keep show(Book $book)?
    Implicit binding matches the placeholder name to the variable name. With `{volume}` in the URI and `$book` in the signature, nothing is bound to `$book`, so the container injects a fresh, empty `Book` instead of the record from the URL. Rename the variable to `$volume`.
  • Why must Route::resourceVerbs be called before the routes file loads?
    The verbs are read from static registrar state when each resource's routes are built. Resources registered before the call keep `create` and `edit`; only later registrations use the new words. `AppServiceProvider::boot()` runs before the route files, so it is the conventional place.

saying these in an interview costs you the question

  • resourceVerbs also renames the create and edit controller methods.
  • parameters() changes the placeholder but binding still uses the old variable name.
  • names('catalogue.books') renames only the index route.
  • resourceVerbs can be called anywhere, even after the routes are registered.
  • Hyphenated resource names keep the hyphen in the placeholder.