---
title: "Routing"
description: "The most basic Laravel routes accept a URI and a closure, providing a very simple and expressive method of defining routes and behavior without complicated routing configuration files."
canonical_url: "https://handbook.propay.co.za/laravel/routing"
---
# Routing

> The most basic Laravel routes accept a URI and a closure, providing a very simple and expressive method of defining routes and behavior without complicated routing configuration files.

## URLs

Public-facing URLs must use kebab-case.

<code-group>

```text [URLs]
https://www.interaction-design.org/about/people-behind
https://www.interaction-design.org/my-private-profile
```

</code-group>

## Names

Routes MUST have names. Please use the `route()` helper to generate URLs from named routes. Route names MUST use camelCase.

<code-group>

```php [PHP]
Route::get('about', [AboutPageController::class, 'index'])->name('about.index');
```

</code-group>

Blade

<code-group>

```blade [Blade]
<a href="{{ route('about.index') }}">About</a>
```

</code-group>

Route names SHOULD include the plural form of the resource name and the action: `articles.show`, `articles.delete`.

## route()

There are a few valid options for using the route() helper with named routes:

Single parameter

<code-group>

```php [PHP]
// route: '/meetups/{meetupId}'
$meetup = \App\Modules\LocalGroup\Models\Meetup::query()->find($meetupId);

route('meetups.show', $meetup); // GOOD (RECOMMENDED) for routes with a single parameter
route('meetups.show', $meetupId); // GOOD for cases when you don’t have Meetup object but have an ID/key
route('meetups.show', [$meetup]); // BAD, don’t use array syntax without keys for single-parameter routes
route('meetups.show', ['id' => $meetup]); // GOOD (RECOMMENDED)
route('meetups.show', ['id' => $meetupId]); // GOOD
route('meetups.show', ['meetupId' => $meetup->id]); // ERROR: Missing required parameter "id"
```

</code-group>

2+ required parameters

<code-group>

```php [PHP]
// route: '/master-classes/{masterclass}/registrations/{registration}'
$registration = \App\Modules\Masterclass\Models\Registration::query()->first();

route('masterclasses.registrations.show', [$registration->masterclass, $registration]);
route('masterclasses.registrations.show', ['masterclass' => $registration->masterclass, 'registration' => $registration]); // GOOD, RECOMMENDED
route('masterclasses.registrations.show', ['registration' => $registration, 'masterclass' => $registration->masterclass]); // BAD, params mixed up (but still working as expected)
route('masterclasses.registrations.show', [$registration->masterclass, 'registration' => $registration]); // BAD: missing first key (Inconsistency)
route('masterclasses.registrations.show', ['masterclass' => $registration->masterclass, $registration]); // BAD: missing second key (Inconsistency)
```

</code-group>

## Method Chaining

When defining routes, use method chaining instead of an array of params:

<code-group>

```php [PHP]
// GOOD:
Route::get('about', [AboutPageController::class, 'index'])->name('about.index')->middleware(['cache:1day']);

// BAD:
Route::get('about', ['as' => 'about.index', 'uses' => [AboutPageController::class, 'index']])->middleware(['cache:1day']);
```

</code-group>

## Middleware

Use array syntax for Route::middleware()

<code-group>

```php [PHP]
// GOOD
Route::get('about', [AboutPageController::class, 'index'])->middleware(['cache:1day']);
Route::get('about', [AboutPageController::class, 'index'])->middleware(['cache:1day', 'CORS']);

// BAD
Route::get('about', [AboutPageController::class, 'index'])->middleware('cache:1day', 'CORS');
Route::get('about', [AboutPageController::class, 'index'])->middleware('cache:1day');
```

</code-group>

## Action notation

Controller + action notation

<code-group>

```php [PHP]
// GOOD
Route::get('about', [AboutPageController::class, 'index']);

// BAD
Route::get('about', 'AboutPageController@index');
```

</code-group>

## Route Parameters

Route parameters SHOULD use camelCase.

<code-group>

```php [PHP]
Route::get('members/{memberId}', [MembersController::class, 'show']);
```

</code-group>

## Verbs

All routes have an HTTP verb, which is why we put the verb first when defining a route. It makes a group of routes very readable. Any other route options MUST come after it.

<code-group>

```php [PHP]
// GOOD: all HTTP verbs come first
Route::get('/', [HomeController::class, 'index'])->name('home');

// BAD: HTTP verbs not easily scannable
Route::name('home')->get('/', [HomeController::class, 'index']);
```

</code-group>


## Sitemap

See the full [sitemap](https://handbook.propay.co.za/sitemap.md) for all pages.
