Laravel Routing: 17 flashcards
All 17 flashcards from this kit, free and with no sign-up.
1. Where are all Laravel routes defined by default?
Show the answer
In the `routes/web.php` file for web routes and `routes/api.php` for API routes.
2. Which HTTP verbs can be used with the `Route::` facade?
Show the answer
`get`, `post`, `put`, `patch`, `delete`, `options`, and `any` (to match all verbs).
3. How do you define a basic GET route that returns a view?
Show the answer
`Route::get('/path', function () { return view('view.name'); });`
4. What is the purpose of route parameters?
Show the answer
To capture segments of the URI and pass them to the route's callback or controller.
5. How do you define a required route parameter?
Show the answer
Enclose the parameter name in curly braces: `Route::get('/user/{id}', ...);`
6. How do you define an optional route parameter?
Show the answer
Add a `?` after the parameter name and provide a default value in the callback: `Route::get('/user/{name?}', function ($name = 'Guest') { ... });`
7. What are named routes and why are they useful?
Show the answer
Named routes assign a name to a route, allowing you to generate URLs or redirects without hardcoding URIs. Define with `->name('profile')`.
8. How do you generate a URL for a named route?
Show the answer
Use the `route()` helper: `route('profile', ['id' => 1])`.
9. What is route grouping and what are its benefits?
Show the answer
Route groups allow you to share attributes like middleware, prefixes, or namespaces across multiple routes, reducing repetition.
10. How do you apply a middleware to a group of routes?
Show the answer
Use the `middleware` method on the group: `Route::middleware(['auth'])->group(function () { ... });`
11. How do you add a prefix to a group of routes?
Show the answer
Use the `prefix` method: `Route::prefix('admin')->group(function () { ... });`
12. What is route model binding?
Show the answer
A feature that automatically injects model instances into route callbacks based on the route parameter. For example, `Route::get('/user/{user}', function (User $user) { ... });`
13. How do you define a fallback route?
Show the answer
Use `Route::fallback(function () { ... });` to handle undefined routes.
14. What is the difference between `Route::resource` and `Route::apiResource`?
Show the answer
`Route::resource` generates routes for all CRUD operations including `create` and `edit` (which show forms). `Route::apiResource` excludes those two routes, making it suitable for APIs.
15. How do you list all registered routes in Laravel?
Show the answer
Run the Artisan command `php artisan route:list`.
16. What is the purpose of the `where` method on a route?
Show the answer
To constrain the format of a route parameter using a regular expression. Example: `->where('id', '[0-9]+')`.
17. How do you redirect a route to another URI?
Show the answer
Use `Route::redirect('/from', '/to');` or `Route::permanentRedirect('/from', '/to');` for 301 redirects.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages