📖 Scenario: You are building a simple web application using Laravel. You want to create routes for your pages and give each route a unique name. This helps you refer to routes easily in your views and controllers.
🎯 Goal: Create named routes in Laravel for the home page, about page, and contact page. Use the route names to generate URLs later.
📋 What You'll Learn
Create routes for '/', '/about', and '/contact' URLs
Assign route names 'home', 'about', and 'contact' respectively
Use Laravel's Route facade and the name() method
💡 Why This Matters
🌍 Real World
Named routes help you manage URLs easily in Laravel applications, making your code cleaner and easier to maintain.
💼 Career
Understanding route naming is essential for Laravel developers to build scalable and maintainable web applications.
Progress0 / 4 steps
1
Create basic routes for pages
Create three routes using Laravel's Route::get() method for the URLs '/', '/about', and '/contact'. Each route should return a simple string response: 'Home Page', 'About Us', and 'Contact Us' respectively.
Laravel
Hint
Use Route::get() with a closure that returns a string for each URL.
2
Add route names using name()
Add route names to each route using the name() method. Name the routes 'home', 'about', and 'contact' for the URLs '/', '/about', and '/contact' respectively.
Laravel
Hint
Chain the name() method after each route definition to assign a name.
3
Use route names to generate URLs
Create a new route for '/links' that returns a string with URLs generated by Laravel's route() helper function using the route names 'home', 'about', and 'contact'. Return a string that lists these URLs separated by commas.
Laravel
Hint
Use the route() helper inside the closure to get URLs by route names.
4
Add a route group with a name prefix
Create a route group with the name prefix 'admin.'. Inside this group, add a route for '/dashboard' that returns 'Admin Dashboard' and name it 'dashboard'. This will create a full route name 'admin.dashboard'.
Laravel
Hint
Use Route::name('admin.')->group() to prefix route names inside the group.
Practice
(1/5)
1. What is the main purpose of naming routes in Laravel using ->name('routeName')?
easy
A. To define the controller method for the route
B. To change the HTTP method of the route
C. To add middleware to the route
D. To easily refer to routes by a simple name instead of hardcoding URLs
Solution
Step 1: Understand route naming purpose
Route naming allows developers to assign a simple, memorable name to a route instead of using the full URL.
Step 2: Identify the benefit in code
This makes linking and redirects easier and less error-prone because you use the route name, not the URL string.
Final Answer:
To easily refer to routes by a simple name instead of hardcoding URLs -> Option D
Quick Check:
Route naming = simple reference [OK]
Hint: Route names replace URLs for easy linking [OK]
Common Mistakes:
Thinking route names change HTTP methods
Confusing route names with middleware assignment
Believing route names define controller methods
2. Which of the following is the correct syntax to name a route in Laravel?
easy
A. Route::get('/home', 'HomeController@index')->name('home');
B. Route::name('home')->get('/home', 'HomeController@index');
C. Route::get('/home', 'HomeController@index')->setName('home');
D. Route::get('/home', 'HomeController@index')->routeName('home');
Solution
Step 1: Recall Laravel route naming syntax
Laravel uses the ->name('name') method chained after route definition to assign a name.
Step 2: Check each option's syntax
Only Route::get('/home', 'HomeController@index')->name('home'); correctly chains ->name('home') after the route definition.
Final Answer:
Route::get('/home', 'HomeController@index')->name('home'); -> Option A
Quick Check:
Correct syntax uses ->name('name') [OK]
Hint: Use ->name('name') after route definition [OK]
Common Mistakes:
Placing ->name() before route method
Using incorrect method names like setName or routeName
Not chaining ->name() properly
3. Given the route definition:
Route::get('/profile', function () { return 'User Profile'; })->name('profile');
What will route('profile') return?
medium
A. profile
B. /profile
C. User Profile
D. An error because route() cannot be used here
Solution
Step 1: Understand what route('profile') does
The route() helper returns the URL for the named route 'profile'.
Step 2: Match the route name to URL
The route named 'profile' points to URL '/profile', so route('profile') returns '/profile'.
Final Answer:
/profile -> Option B
Quick Check:
route('name') returns URL [OK]
Hint: route('name') returns the URL, not content [OK]
Common Mistakes:
Expecting route() to return the route's response content