📖 Scenario: You are building a simple Laravel web application to manage a list of books. You want to create a controller that handles showing all books and showing details of a single book.
🎯 Goal: Create a Laravel controller named BookController with two methods: index to list all books and show to display details of a single book by its ID.
📋 What You'll Learn
Create a controller class named BookController
Add a public method index that returns a view named books.index
Add a public method show that accepts a parameter $id and returns a view named books.show passing the $id
Use the correct namespace and extend the base Controller class
💡 Why This Matters
🌍 Real World
Controllers in Laravel handle user requests and return responses, such as views or JSON data. This project shows how to set up basic controller methods for common web pages.
💼 Career
Understanding controller methods is essential for backend web development with Laravel. It is a core skill for building maintainable and organized web applications.
Progress0 / 4 steps
1
Create the BookController class
Create a PHP class named BookController inside the namespace App\Http\Controllers that extends the base Controller class.
Laravel
Hint
Remember to declare the namespace and extend the Controller class.
2
Add the index method
Inside the BookController class, add a public method named index that returns the view books.index using return view('books.index');.
Laravel
Hint
The index method should be public and return the view books.index.
3
Add the show method with parameter
Inside the BookController class, add a public method named show that accepts a parameter $id and returns the view books.show passing the $id as data using return view('books.show', ['id' => $id]);.
Laravel
Hint
The show method should accept $id and pass it to the view books.show.
4
Complete the controller with both methods
Ensure the BookController class contains both the index and show methods exactly as specified, with correct namespace and class declaration.
Laravel
Hint
Check that both methods are present and the class is correctly declared.
Practice
(1/5)
1. What is the main purpose of a controller method (action) in Laravel?
easy
A. To handle a specific user request and return a response
B. To define database schema
C. To style the HTML output
D. To manage server configuration
Solution
Step 1: Understand controller role
Controllers group related logic into methods called actions.
Step 2: Identify action purpose
Each action handles a user request and returns a response like a view or JSON.
Final Answer:
To handle a specific user request and return a response -> Option A
Quick Check:
Controller action = handle request and respond [OK]
Hint: Controllers handle requests and responses, not styling or config [OK]
Common Mistakes:
Confusing controller methods with database or styling tasks
Thinking controllers manage server settings
Assuming controllers only return views
2. Which of the following is the correct syntax to define a controller method named show in Laravel?
easy
A. function show() public { /* code */ }
B. public function show() { /* code */ }
C. public show function() { /* code */ }
D. def show() { /* code */ }
Solution
Step 1: Recall PHP method syntax
In PHP, methods are defined with visibility, function keyword, name, and parentheses.
Step 2: Match Laravel controller method syntax
Laravel uses PHP syntax: public function methodName() {}.