Laravel API Development Flashcards

(25 cards)

1
Q

What is API development in Laravel?

A

API development in Laravel involves creating RESTful APIs to enable data exchange between clients and a LAMP-based application.

Laravel’s API routes and resources facilitate data access, similar to WordPress’s REST API (/wp-json/). For example, an API can serve user data. Freelancers build APIs for client apps, while enterprise architects design scalable APIs for system integration, aligning with your PHP APIs and WordPress APIs experience.

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

Where are API routes defined in Laravel?

A

API routes are defined in routes/api.php, like Route::get(‘/users’, [UserController::class, ‘index’]).

This file handles API endpoints, prefixed with /api, like WordPress’s API routes. Freelancers define API routes, while enterprise architects organize them for scalability, per your Routing and Middleware deck.

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

How do you create an API route in Laravel?

A

Use Route::apiResource(‘users’, UserController::class); for RESTful routes.

Generates CRUD endpoints (e.g., /api/users, /api/users/{id}), like WordPress’s REST API resources. Freelancers build APIs quickly, while enterprise architects customize routes, per your REST API knowledge.

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

What is a resource controller for APIs in Laravel?

A

A resource controller handles CRUD operations, created with php artisan make:controller UserController –api.

Excludes create and edit methods for APIs, like WordPress’s REST controllers. Freelancers implement CRUD, while enterprise architects optimize logic, per your Controllers and Requests deck.

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

How do you return JSON from a Laravel API?

A

Use return response()->json([‘data’ => $data], 200); in a controller.

Returns JSON, like WordPress’s wp_send_json(). Freelancers format API responses, while enterprise architects set status codes, per your PHP APIs deck.

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

What is a Laravel API resource?

A

An API resource transforms data for JSON, created with php artisan make:resource UserResource. Explanation: Like WordPress’s REST response formatting, it shapes data (e.g., return new UserResource($user);). Freelancers customize responses, while enterprise architects standardize formats, per your API development skills.

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

How do you use a Laravel API resource?

A

Return a resource in a controller, like return new UserResource(User::find(1));.

Transforms model data, like WordPress’s rest_prepare_post. Freelancers simplify output, while enterprise architects ensure consistent data, per your Eloquent ORM deck.

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

What is a resource collection in Laravel?

A

A resource collection handles multiple models, like return UserResource::collection(User::all());.

Like WordPress’s post collection in REST API, it formats lists. Freelancers return collections, while enterprise architects paginate results, per your database interaction experience.

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

How do you paginate API results in Laravel?

A

Use paginate(), like return UserResource::collection(User::paginate(10));.

Limits results per page, like WordPress’s WP_Query pagination. Freelancers paginate data, while enterprise architects optimize performance, per your performance deck.

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

What is Laravel Sanctum for API authentication?

A

Laravel Sanctum provides token-based authentication for APIs, installed with composer require laravel/sanctum.

Like WordPress’s API keys, Sanctum secures endpoints. Freelancers use Sanctum for client APIs, while enterprise architects integrate with OAuth, per your Authentication and Authorization deck.

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

How do you set up Laravel Sanctum?

A

Install with composer require laravel/sanctum, publish config with php artisan vendor:publish –provider=”Laravel\Sanctum\SanctumServiceProvider”, and migrate.

Enables token authentication, like WordPress’s Application Passwords. Freelancers configure Sanctum, while enterprise architects secure tokens, per your security deck.

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

How do you generate a Sanctum token?

A

Use createToken(), like $user->createToken(‘api’)->plainTextToken.

Issues a token for API access, like WordPress’s API key generation. Freelancers provide tokens, while enterprise architects manage token lifecycle, per your authentication experience.

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

How do you protect an API route with Sanctum?

A

Apply auth:sanctum middleware, like Route::middleware(‘auth:sanctum’)->get(‘/user’, [UserController::class, ‘index’]).

Restricts access, like WordPress’s rest_authentication_errors. Freelancers secure routes, while enterprise architects enforce authentication, per your middleware deck.

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

What is Laravel Passport for API authentication?

A

Laravel Passport provides OAuth2 authentication for APIs, installed with composer require laravel/passport.

More robust than Sanctum, like WordPress’s OAuth plugins. Freelancers use Passport for complex APIs, while enterprise architects integrate with enterprise OAuth, per your PHP APIs deck.

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

How do you rate limit API requests in Laravel?

A

Use throttle middleware, like Route::middleware(‘throttle:60,1’)->group(function() { /* routes */ });.

Limits requests, like WordPress’s brute-force protection. Freelancers prevent abuse, while enterprise architects scale throttling, per your Routing and Middleware deck.

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

How do you validate API requests in Laravel?

A

Use $request->validate(), like $request->validate([‘email’ => ‘required|email’]).

Ensures valid input, like WordPress’s sanitize_email(). Freelancers validate data, while enterprise architects customize rules, per your Controllers and Requests deck.

17
Q

How do you handle API errors in Laravel?

A

Return JSON errors with status codes, like return response()->json([‘error’ => ‘Not found’], 404);.

Like WordPress’s wp_send_json_error(). Freelancers handle errors, while enterprise architects log them, per your error handling deck.

18
Q

What is CORS in Laravel API development?

A

CORS (Cross-Origin Resource Sharing) allows cross-domain API requests, configured in config/cors.php.

Like WordPress’s REST API CORS headers, it enables frontend access. Freelancers configure CORS, while enterprise architects restrict origins, per your PHP APIs deck.

19
Q

How do you configure CORS in Laravel?

A

Set allowed_origins in config/cors.php, like ‘allowed_origins’ => [‘http://frontend.com’].

Controls API access, like WordPress’s rest_pre_serve_request. Freelancers allow specific domains, while enterprise architects secure CORS, per your security deck.

20
Q

How do you cache API responses in Laravel?

A

Use Cache::remember(), like Cache::remember(‘users’, 3600, function() { return User::all(); });.

Improves performance, like WordPress’s transients. Freelancers cache data, while enterprise architects scale caching, per your LAMP performance deck.

21
Q

How do you test a Laravel API?

A

Use PHPUnit or tools like Postman, like $response = $this->get(‘/api/users’); $response->assertStatus(200);.

Validates endpoints, like WordPress’s REST API tests. Freelancers test manually, while enterprise architects automate tests, per your debugging deck.

22
Q

What is API versioning in Laravel?

A

API versioning organizes endpoints by version, like Route::prefix(‘v1’)->group(…).

Like WordPress’s /wp/v2, it supports backward compatibility. Freelancers version APIs, while enterprise architects manage multiple versions, per your API development goals.

23
Q

How do you document a Laravel API?

A

Use Laravel API Documentation Generator or Swagger, like php artisan l5-swagger:generate.

Documents endpoints, like WordPress’s REST API docs. Freelancers create docs, while enterprise architects standardize documentation, per your REST API deck.

24
Q

How do you optimize Laravel API performance?

A

Use eager loading (with()), pagination, and caching (Cache::remember()).

Reduces WordPress-like query overhead. Freelancers optimize responses, while enterprise architects scale APIs, per your Eloquent ORM and performance decks.

25
How do you deploy a Laravel API in LAMP?
Sync code with rsync, configure Apache with .htaccess, and run php artisan optimize. Deploys WordPress-like APIs. Freelancers manage deployments, while enterprise architects use CI/CD, per your LAMP deployment deck.