Routing and Middleware Flashcards

(25 cards)

1
Q

What is routing in Laravel?

A

Routing in Laravel maps HTTP requests to specific controller actions or closures.

Routes, defined in routes/web.php or routes/api.php, direct URLs to functions, similar to WordPress permalinks. For example, Route::get(‘/home’, [HomeController::class, ‘index’]) handles GET requests. Freelancers use routing for app navigation, while enterprise architects design structured routes for scalability, aligning with your PHP APIs experience.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the Route::get() method in Laravel?

A

Route::get() defines a route for GET requests, like Route::get(‘/about’, [PageController::class, ‘about’]).

It maps a URL to a controller or closure, like WordPress’s get_template_part() for pages. Freelancers create GET routes for static pages, while enterprise architects optimize them for performance, per your WordPress routing knowledge.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the Route::post() method in Laravel?

A

Route::post() defines a route for POST requests, like Route::post(‘/submit’, [FormController::class, ‘store’]).

Handles form submissions, similar to WordPress’s $_POST processing. Freelancers use POST routes for data creation, while enterprise architects secure them against CSRF, per your PHP core concepts and security decks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a route parameter in Laravel?

A

A route parameter captures URL segments, like Route::get(‘/user/{id}’, [UserController::class, ‘show’]).

{id} extracts dynamic values, like WordPress’s ?p=123. Freelancers use parameters for dynamic content, while enterprise architects validate them, per your WordPress APIs experience.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you make a route parameter optional in Laravel?

A

Use a question mark, like Route::get(‘/user/{id?}’, [UserController::class, ‘show’]).

Allows /user or /user/123, like WordPress’s optional query vars. Freelancers provide flexible routes, while enterprise architects ensure default handling, per your PHP routing skills.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a named route in Laravel?

A

A named route assigns a name to a route, like Route::get(‘/home’, [HomeController::class, ‘index’])->name(‘home’).

Enables URL generation (e.g., route(‘home’)), like WordPress’s get_permalink(). Freelancers simplify redirects, while enterprise architects use names for maintainability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you generate a URL for a named route?

A

Use the route() helper, like route(‘home’) or route(‘user.show’, [‘id’ => 1]).

Creates URLs dynamically, avoiding hardcoding, similar to WordPress’s site_url(). Freelancers use it for links, while enterprise architects ensure portability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is middleware in Laravel?

A

Middleware filters HTTP requests, like auth to check user login.

Like WordPress’s action hooks (e.g., restrict_manage_posts), middleware runs before/after requests. Freelancers apply middleware for security, while enterprise architects create custom middleware, per your WordPress security deck.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you apply middleware to a route?

A

Use the middleware() method, like Route::get(‘/dashboard’, [DashboardController::class, ‘index’])->middleware(‘auth’).

Restricts access, like WordPress’s is_user_logged_in(). Freelancers secure routes, while enterprise architects apply multiple middleware, per your PHP security knowledge.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the auth middleware in Laravel?

A

The auth middleware ensures a user is authenticated, redirecting unauthenticated users.

Similar to WordPress’s admin access checks. Freelancers use auth for protected pages, while enterprise architects customize authentication flows, per your Laravel authentication interest.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you create a custom middleware in Laravel?

A

Use php artisan make:middleware MyMiddleware, then define logic in handle().

Middleware can check roles, like WordPress’s current_user_can(). Freelancers create custom checks, while enterprise architects ensure reusable logic, per your PHP OOP deck.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Where do you register middleware in Laravel?

A

Register middleware in app/Http/Kernel.php, under $middleware or $routeMiddleware.

Like WordPress’s add_filter(), it enables middleware globally or per route. Freelancers add middleware, while enterprise architects organize them, per your WordPress hooks experience.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a route group in Laravel?

A

A route group applies shared attributes, like Route::middleware(‘auth’)->group(function() { /* routes */ });.

Simplifies middleware or prefix application, like WordPress’s admin routes. Freelancers group related routes, while enterprise architects structure APIs, per your PHP APIs deck.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you define a route prefix in Laravel?

A

Use prefix(), like Route::prefix(‘admin’)->group(function() { /* routes */ });.

Adds /admin to routes, like WordPress’s /wp-admin. Freelancers organize routes, while enterprise architects ensure consistent URL structures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the namespace attribute in a route group?

A

The namespace attribute sets the controller namespace, like Route::namespace(‘App\Http\Controllers\Admin’)->group(…).

Organizes controllers, like WordPress’s plugin namespaces. Freelancers simplify controller access, while enterprise architects maintain modularity, per your PHP namespaces knowledge.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you define a route for all HTTP methods in Laravel?

A

Use Route::any(), like Route::any(‘/test’, [TestController::class, ‘handle’]).

Handles any request type, less common than specific methods. Freelancers use it for flexible endpoints, while enterprise architects prefer explicit methods for clarity.

17
Q

What is Route::resource() in Laravel?

A

Route::resource() creates RESTful routes for CRUD, like Route::resource(‘users’, UserController::class).

Generates routes for index, create, store, etc., like WordPress REST API endpoints. Freelancers build CRUD APIs, while enterprise architects customize resources, per your REST API deck.

18
Q

How do you view all Laravel routes?

A

Run php artisan route:list to display registered routes.

Lists routes, like WordPress’s admin menu inspection. Freelancers debug routing, while enterprise architects verify route configurations, per your debugging experience.

19
Q

What is route caching in Laravel?

A

Route caching compiles routes for faster loading, using php artisan route:cache.

Improves WordPress-like app performance. Freelancers cache routes in production, while enterprise architects automate caching, per your performance deck.

20
Q

How do you clear Laravel’s route cache?

A

Use php artisan route:clear to remove cached routes.

Resets routes during development, like clearing WordPress transients. Freelancers clear caches, while enterprise architects manage cache lifecycles.

21
Q

What is the throttle middleware in Laravel?

A

The throttle middleware limits request rates, like Route::middleware(‘throttle:60,1’)->group(…).

Protects APIs, like WordPress rate limiting. Freelancers prevent abuse, while enterprise architects scale throttling, per your REST API deck.

22
Q

How do you redirect in a Laravel route?

A

Use redirect(), like return redirect()->route(‘home’);.

Similar to WordPress’s wp_redirect(). Freelancers handle navigation, while enterprise architects ensure secure redirects, per your PHP core concepts.

23
Q

What is a fallback route in Laravel?

A

A fallback route handles unmatched URLs, like Route::fallback(function() { return view(‘404’); });.

Like WordPress’s 404 template. Freelancers create custom 404s, while enterprise architects log errors, per your error handling deck.

24
Q

How do you pass data to a route closure?

A

Use route parameters or dependency injection, like Route::get(‘/user/{id}’, function($id) { return $id; }).

Similar to WordPress’s query vars. Freelancers pass data, while enterprise architects validate inputs, per your PHP functions deck.

25
What is the can middleware in Laravel?
The can middleware checks user permissions, like Route::get('/edit', [PostController::class, 'edit'])->middleware('can:edit-posts'). Like WordPress’s current_user_can(). Freelancers secure actions, while enterprise architects define complex policies, per your authentication interest.