Stop hardcoding URLs in your views! Learn how to use named routes and the route() helper in Laravel to make your application easier to refactor and maintain.
Previously in this course, we covered deleting records by triggering specific HTTP methods. While that works, you likely noticed we often point our forms or links to string-based URLs like /tasks/1/edit.
In this lesson, we are moving away from those hardcoded strings. We’ll implement named routes, a critical practice for any professional Laravel developer. By the end of this lesson, you'll know how to decouple your URLs from your views, making future refactoring a breeze.
When you write <a href="/tasks/create"> in a Blade template, you are creating a "brittle" link. If you ever decide to change the URL structure—perhaps changing /tasks/create to /dashboard/new-task—you have to perform a manual search-and-replace across your entire codebase.
If you miss one instance, your application breaks silently. Named routes solve this by assigning an alias to a route. You reference the alias, and Laravel handles the generation of the actual URL.
To name a route, you chain the name() method onto your route definition in routes/web.php.
Take our Task Manager's "create" route as an example:
PHP#6A9955">// routes/web.php Route::get('/tasks/create', [TaskController::class, 'create']) ->name('tasks.create');
The naming convention is entirely up to you, but most Laravel developers use a "resource.action" format. This makes it intuitive to guess the names of other routes in your application.
route() HelperOnce a route is named, you no longer use the literal path. Instead, you use the global route() helper function. This function looks up the route definition by its name and returns the correct URL.
In your Blade view, replace your standard href attributes:
HTML<!-- Before (Hardcoded) --> style="color:#808080"><style="color:#4EC9B0">a href="/tasks/create">Create New Taskstyle="color:#808080"></style="color:#4EC9B0">a> <!-- After (Using Named Routes) --> style="color:#808080"><style="color:#4EC9B0">a href="{{ route('tasks.create') }}">Create New Taskstyle="color:#808080"></style="color:#4EC9B0">a>
What if your route requires an ID, like editing a specific task? The route() helper accepts an array as its second argument to fill in those placeholders.
PHP#6A9955">// routes/web.php Route::get('/tasks/{task}/edit', [TaskController::class, 'edit']) ->name('tasks.edit'); #6A9955">// In your Blade view <a href="{{ route('tasks.edit', ['task' => $task->id]) }}">Edit Task</a>
If the parameter name in your route matches the key in your array, Laravel intelligently injects the value into the URL.
Let’s apply this to our running project. Open your resources/views/tasks/index.blade.php file. You likely have a loop displaying your tasks with links to edit or delete them.
Exercise:
routes/web.php.index, create, edit, and destroy routes.index.blade.php file to use route() for every link or form action.InvalidArgumentException: Route [name] not defined if you try to use route() with a name that hasn't been assigned in your route file.tasks.Create is not the same as tasks.create.return redirect('/tasks'), use return redirect()->route('tasks.index');. This is much cleaner and safer.Named routes are the standard for professional Laravel development because they keep your application's internal links synchronized with your route definitions. By using the route() helper, you eliminate the risk of broken links when you decide to change your URL structure later.
Always name your routes as you define them in web.php, and use the route() helper in your views and redirects to ensure your code remains maintainable and professional.
Up next: We will finalize our Task Manager by ensuring all CRUD operations are fully functional and properly secured using the techniques we've learned so far.
Finalize your Task Manager CRUD functionality by implementing secure edit and delete features. Learn how to maintain data integrity in your Laravel application.
Read moreLearn how to use Form Requests in Laravel to move validation logic out of your controllers. Keep your code clean, DRY, and professional with this guide.
Using Named Routes
Using Flash Messages for User Feedback
Task Manager: Adding Status and Priorities
Introduction to Artisan Commands
Debugging with Laravel Tinker
Understanding Service Providers
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation