Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why APIs Serve Modern Applications
📖 Scenario: You are building a simple Laravel backend that provides data to a frontend app through an API. This setup is common in modern web development where the backend and frontend are separate but communicate smoothly.
🎯 Goal: Create a basic Laravel API endpoint that returns a list of products in JSON format. This will show how APIs serve data to modern applications.
📋 What You'll Learn
Create a products array with exact product names and prices
Add a configuration variable currency set to 'USD'
Write a function getProductsApi() that returns products with prices including the currency
Define a route /api/products that calls getProductsApi() and returns JSON
💡 Why This Matters
🌍 Real World
Modern web and mobile apps often get their data from backend APIs. This separation allows teams to build flexible, scalable applications where frontend and backend can evolve independently.
💼 Career
Understanding how to create and serve APIs with Laravel is essential for backend developers. It is a common task in many web development jobs to build APIs that serve data to various clients.
Progress0 / 4 steps
1
DATA SETUP: Create the products array
Create a PHP array called products with these exact entries: ['Laptop' => 1200, 'Smartphone' => 800, 'Tablet' => 450].
Laravel
Hint
Use a PHP associative array with product names as keys and prices as values.
2
CONFIGURATION: Add currency variable
Add a variable called currency and set it to the string 'USD'.
Laravel
Hint
This variable will help show prices with the currency symbol.
3
CORE LOGIC: Create API function to return products with currency
Write a function called getProductsApi() that returns an array where each product price is a string combining the price and the currency variable. Use a foreach loop with variables $name and $price to iterate over $products.
Laravel
Hint
Use global to access variables inside the function. Build a new array with prices as strings including currency.
4
COMPLETION: Define API route to return JSON response
Add a Laravel route in routes/api.php for /api/products that returns the JSON response from getProductsApi() using response()->json().
Laravel
Hint
Use Laravel's Route facade to define a GET route that returns JSON data.
Practice
(1/5)
1. Why do modern applications often use APIs to communicate? APIs help apps to:
easy
A. Avoid using the internet
B. Run only on desktop computers
C. Share data and work together smoothly
D. Replace databases completely
Solution
Step 1: Understand the role of APIs in app communication
APIs allow different applications to exchange data and work together easily, not limiting to desktops, avoiding internet, or replacing databases.
Final Answer:
Share data and work together smoothly -> Option C
Quick Check:
APIs enable data sharing [OK]
Hint: APIs connect apps by sharing data smoothly [OK]
Common Mistakes:
Thinking APIs only work on desktops
Believing APIs avoid internet use
Confusing APIs with databases
2. Which Laravel code snippet correctly defines a simple API route that returns JSON data?
easy
A. Route::get('/api/data', function() { echo json_encode(['name' => 'Laravel']); });
B. Route::post('/api/data', 'return json(['name' => 'Laravel']);');
C. Route::api('/data', function() { return ['name' => 'Laravel']; });
D. Route::get('/api/data', function() { return response()->json(['name' => 'Laravel']); });
Solution
Step 1: Check Laravel route syntax for API returning JSON
Laravel uses Route::get with a closure returning response()->json() for proper JSON API responses; others misuse echo, have invalid syntax, or use non-existent methods.
A. Missing semicolon after response()->json() call
B. Route method should be post, not get
C. response()->json() cannot return arrays
D. Route path must start with /api/
Solution
Step 1: Check syntax of the return statement
The return response()->json(['product' => 'Book', 'price' => 15]) lacks a semicolon, causing a syntax error; Route::get, arrays in json, and path are valid.
Final Answer:
Missing semicolon after response()->json() call -> Option A
Quick Check:
Missing semicolon after response()->json() [OK]
Hint: Check for missing semicolons in Laravel closures [OK]
Common Mistakes:
Assuming wrong HTTP method
Thinking response()->json() can't return arrays
Believing route path must start with /api/
5. You want your Laravel app to serve data to both web and mobile apps using APIs. Which approach best supports this goal?
hard
A. Create API routes that return JSON responses for data requests
B. Build separate apps for web and mobile with no shared backend
C. Use only Blade templates to render HTML for all clients
D. Store data only on the mobile device, not on the server
Solution
Step 1: Understand the need for shared data access
APIs returning JSON allow multiple clients like web and mobile to access one backend; separate apps, HTML-only, or device storage do not support this.
Final Answer:
Create API routes that return JSON responses for data requests -> Option A
Quick Check:
APIs with JSON support multiple clients [OK]
Hint: Use JSON APIs to serve multiple device types [OK]