Laravel

API Resources & Authentication

API routes, resource controllers, API resources, pagination, rate limiting, CORS, Sanctum (SPA/tokens), Passport/OAuth2, scopes

26 питань зі співбесід·
Mid-Level
1

In Laravel, where to define API routes by default?

Відповідь

API routes are defined in the routes/api.php file. These routes are automatically prefixed with /api and have the api middleware applied by default. Unlike web routes defined in routes/web.php, API routes don't use sessions and are designed for stateless calls (REST, GraphQL, etc.).

2

Which Artisan command generates a resource controller for an API?

Відповідь

The php artisan make:controller PostController --api command generates a resource controller optimized for APIs. This controller contains the index, store, show, update and destroy methods, but WITHOUT the create and edit methods (which serve to display forms in web context, useless for a REST API). This --api option avoids superfluous code in an API-only context.

3

What is an API Resource in Laravel?

Відповідь

An API Resource is a transformation class that converts an Eloquent model into a JSON array for API responses. It allows precise control over which attributes are exposed, adding computed fields, hiding sensitive data (passwords), and managing relationships. This decouples the database structure from the API response and ensures consistency in output formats.

4

How to create an API Resource for a User model?

5

How to return a paginated collection with an API Resource?

+23 питань зі співбесід

Опануй Laravel для наступної співбесіди

Отримай доступ до всіх питань, flashcards, технічних тестів, вправ code review та симуляторів співбесід.

Почни безкоштовно