Laravel — P60: Resource Route Names

Name routes your way

One advantage of creating routes in Laravel is that you also get to name those routes. So, when you need to reference them, you simply reference the name and not the route itself. If you later decide to change the route, your code still works since you’re referencing the name and not the route itself.
If you remember from an earlier article, we used the name method to name our route.
Route::get('/get-started', function() {
    return "Get Started";
})->name('getting-started');

To name a resource route is not too different. We just need to override the names that we want to assign a name to.

use App\Http\Controllers\CameraController;
 
Route::resource('cameras', CameraController::class)->names([
    'create' => 'cameras.form'
]);
What if we wanted to pass arguments? When you create a resource controller, the parameters are predefined for you. The convention is that the argument will be the singularized version of your resource name. To override it, you simply pass an array to the parameters method.
use App\Http\Controllers\BestCameraController;
 
Route::resource('cameras', BestCameraController::class)->parameters([
    'cameras' => 'best_camera'
]);

This generates the following route: /cameras/{best_camera}.

You can now access the routes the exact same way as you’re used to with regular route names.

<div class="flex justify-center lg:justify-start mt-6">
    <a class="px-4 py-3 bg-gray-900 text-gray-200 text-xs font-semibold rounded hover:bg-gray-800"
       href="{{ route('cameras.form') }}">
        Check out the camera form
    </a>
</div>

Laravel Series

Continue your Laravel Learning.

Laravel — P59: Partial Resource Routes

Register only what you need with partial resource routes

Laravel – P59: Partial Resource Routes

In part 59 of our Laravel fundamentals series, discover how partial resource routes let you register only the endpoints you need. Learn the only and except options, group route patterns, and keep your routing file lean and secure.

Laravel — P60: Resource Route Names

Name routes your way

Laravel – P60: Resource Route Names

Part 60 of our Laravel fundamentals series unpacks how to customize resource route names for maximum clarity. Override default naming, prevent helper collisions, and keep large-scale projects organized with readable, intuitive route helpers and API endpoints.

 

Laravel — P61: Sharing Data With All Views

Keep every Blade view in sync—share once, use everywhere

Laravel – P61: Sharing Data With All Views

In part 61 of our Laravel fundamentals series, learn techniques to share global data with every Blade view. Explore View::share, service-provider boot methods, view composers, and caching tips for consistent, performant layouts across your app.

Leave a Reply