Routing

Accessing current route

use Illuminate\Support\Facades\Route;
// Illuminate\Routing\Route
$route = Route::current();
// string
$name = Route::currentRouteName();
// string
$action = Route::currentRouteAction();

Route Groups

#Middleware

Route::middleware(['first', 'second'])->group(function () {
    Route::get('/', function () {
        // Uses first & second middleware...
    });
    Route::get('/user/profile', function () {
        // Uses first & second middleware...
    });
});

#URI Prefixes

Route::prefix('admin')->group(function () {
    Route::get('/users', function () {
        // Matches The "/admin/users" URL
    });
});

#Name Prefix

Route::name('admin.')->group(function () {
    Route::get('/users', function () {
        // Route assigned name "admin.users"...
    })->name('users');
});

Share attributes across routes

Fallback Routes

Route::fallback(function () {
    //
});

Executed when no other routes match

Named Routes

Route names should always be unique

Route::get('/user/profile', function () {
    //
})->name('profile');

See: Helpers

Regular Expression Constraints

Route::get('/user/{name}', function ($name) {
    //
})->where('name', '[A-Za-z]+');
Route::get('/user/{id}', function ($id) {
    //
})->where('id', '[0-9]+');
Route::get('/user/{id}/{name}', function ($id, $name) {
    //
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);

See also: Regex Cheatsheet

Redirect Routes

HTTP 302 status

Route::redirect('/here', '/there');

Set the status code

Route::redirect('/here', '/there', 301);

Permanent 301 redirect

Route::permanentRedirect('/here', '/there');

Route Parameters

Capture segments of the URI within your route

#Required parameters

Route::get('/user/{id}', function ($id) {
    return 'User '.$id;
});

With dependency injection

use Illuminate\Http\Request;
Route::get('/user/{id}', function (Request $request, $id) {
    return 'User '.$id;
});

#Optional Parameters

Route::get('/user/{name?}', function ($name = null) {
    return $name;
});
Route::get('/user/{name?}', function ($name = 'John') {
    return $name;
});

Route Model Binding

#Implicit binding

With closure

use App\Models\User;
Route::get('/users/{user}', function (User $user) {
    return $user->email;
});
// /user/1 --> User::where('id', '=', 1);

With controller action

use App\Http\Controllers\UserController;
use App\Models\User;
// Route definition...
Route::get('/users/{user}', [UserController::class, 'show']);
// Controller method definition...
public function show(User $user)
{
    return view('user.profile', ['user' => $user]);
}

With custom resolution column

use App\Models\Post;
Route::get('/posts/{post:slug}', function (Post $post) {
    return $post;
});
// /posts/my-post --> Post::where('slug', '=', 'my-post');

Always use a different column to resolve

// in App\Models\Post
public function getRouteKeyName()
{
    return 'slug';
}

Multiple models - second is child of first

use App\Models\Post;
use App\Models\User;
Route::get('/users/{user}/posts/{post:slug}', function (User $user, Post $post) {
    return $post;
});

Convenient way to automatically inject the model instances directly into your routes

View Routes

// Argument 1: URI, Argument 2: view name
Route::view('/welcome', 'welcome');
// with data
Route::view('/welcome', 'welcome', ['name' => 'Taylor']);

Route only needs to return a view.

Dependency Injection

use Illuminate\Http\Request;
Route::get('/users', function (Request $request) {
    // ...
});

Type hint concrete dependencies for auto-injection

Basic Definition

use Illuminate\Support\Facades\Route;
// closure
Route::get('/greeting', function () {
    return 'Hello World';
});
// controller action
Route::get(
    '/user/profile',
    [UserProfileController::class, 'show']
);

Router HTTP Methods

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

Multiple HTTP methods

Route::match(['get', 'post'], '/', function () {
    //
});
Route::any('/', function () {
    //
});
Comments