Bird
Raised Fist0
Laravelframework~30 mins

Creating controllers with Artisan in Laravel - Try It Yourself

Choose your learning style10 modes available
Creating Controllers with Artisan
📖 Scenario: You are building a simple Laravel web application that manages books. To organize your code, you need to create controllers that handle requests related to books.
🎯 Goal: Learn how to create Laravel controllers using the Artisan command-line tool step-by-step.
📋 What You'll Learn
Use the Artisan command php artisan make:controller to create controllers
Create a controller named BookController
Add a method index inside the controller
Add a method show inside the controller that accepts a parameter $id
💡 Why This Matters
🌍 Real World
Controllers organize how your Laravel app responds to user requests, making your code clean and maintainable.
💼 Career
Knowing how to create and use controllers with Artisan is essential for Laravel developers to build scalable web applications.
Progress0 / 4 steps
1
Create the BookController using Artisan
Use the Artisan command php artisan make:controller BookController to create a new controller named BookController inside the app/Http/Controllers directory.
Laravel
Hint

Open your terminal and type exactly php artisan make:controller BookController to create the controller.

2
Add an index method to BookController
Open the BookController.php file and add a public method named index that returns a simple string 'List of books'.
Laravel
Hint

Inside the BookController class, write public function index() and return the string 'List of books'.

3
Add a show method with an ID parameter
Inside BookController, add a public method named show that accepts a parameter $id and returns the string 'Showing book with ID: ' . $id.
Laravel
Hint

Define public function show($id) and return the string concatenating 'Showing book with ID: ' and $id.

4
Register routes for the controller methods
Open the routes/web.php file and add two routes: one GET route for /books that uses BookController@index, and one GET route for /books/{id} that uses BookController@show.
Laravel
Hint

Use Route::get with the URL and controller method array syntax to register the routes.

Practice

(1/5)
1. What does the Artisan command php artisan make:controller do in a Laravel project?
easy
A. It runs the Laravel server to test the application.
B. It creates a new controller file in the app/Http/Controllers directory.
C. It installs Laravel dependencies via Composer.
D. It creates a new database migration file.

Solution

  1. Step 1: Understand the purpose of the make:controller command

    The command php artisan make:controller is designed to generate a new controller file automatically.
  2. Step 2: Identify the location of the created controller

    By default, the new controller is placed in the app/Http/Controllers directory to organize application logic.
  3. Final Answer:

    It creates a new controller file in the app/Http/Controllers directory. -> Option B
  4. Quick Check:

    make:controller creates controller file [OK]
Hint: Remember: make:controller creates controller files [OK]
Common Mistakes:
  • Confusing make:controller with server start commands
  • Thinking it creates migrations or installs packages
  • Assuming it runs the app instead of creating files
2. Which of the following is the correct Artisan command to create a resource controller named ProductController?
easy
A. php artisan make:controller ProductController --resource
B. php artisan create:controller ProductController --resource
C. php artisan make:controller ProductController -r
D. php artisan generate:controller ProductController --resource

Solution

  1. Step 1: Recall the correct Artisan syntax for resource controllers

    The correct command uses make:controller with the --resource option to generate all resource methods.
  2. Step 2: Verify the command spelling and options

    Only php artisan make:controller ProductController --resource matches Laravel's official syntax exactly.
  3. Final Answer:

    php artisan make:controller ProductController --resource -> Option A
  4. Quick Check:

    Use make:controller with --resource option [OK]
Hint: Use make:controller with --resource for full methods [OK]
Common Mistakes:
  • Using create:controller or generate:controller commands
  • Using short option -r which is invalid
  • Misspelling make:controller
3. What will be the result of running php artisan make:controller UserController --resource in a Laravel project?
medium
A. Creates a UserController with methods like index, create, store, show, edit, update, and destroy.
B. Creates a UserController with only an index method.
C. Creates a UserController without any methods inside.
D. Throws an error because --resource is not a valid option.

Solution

  1. Step 1: Understand the --resource option effect

    The --resource flag tells Artisan to generate a controller with all standard resource methods.
  2. Step 2: List the methods generated

    These methods include index, create, store, show, edit, update, and destroy.
  3. Final Answer:

    Creates a UserController with methods like index, create, store, show, edit, update, and destroy. -> Option A
  4. Quick Check:

    --resource adds all resource methods [OK]
Hint: --resource adds all common controller methods [OK]
Common Mistakes:
  • Thinking it creates empty controller without methods
  • Assuming only index method is created
  • Believing --resource is invalid option
4. You run php artisan make:controller AdminController -resource but get an error. What is the likely cause?
medium
A. AdminController name is reserved and cannot be used.
B. The make:controller command requires a namespace option.
C. You must run the command inside the database directory.
D. The short option -resource is invalid; it should be --resource.

Solution

  1. Step 1: Check the option syntax used

    The command uses -resource which is incorrect; Laravel requires double dashes for options.
  2. Step 2: Confirm correct option format

    The correct option is --resource with two dashes, not one.
  3. Final Answer:

    The short option -resource is invalid; it should be --resource. -> Option D
  4. Quick Check:

    Options need double dashes like --resource [OK]
Hint: Use double dashes for options like --resource [OK]
Common Mistakes:
  • Using single dash for long options
  • Thinking controller names are restricted
  • Running commands outside project root
5. You want to create a controller named OrderController that includes resource methods and also API-specific methods without views. Which Artisan command should you use?
hard
A. php artisan make:controller OrderController --resource
B. php artisan make:controller OrderController --api
C. php artisan make:controller OrderController --resource --api
D. php artisan make:controller OrderController --viewless

Solution

  1. Step 1: Understand the need for resource and API methods

    You want all resource methods but tailored for API use, which excludes view-related methods.
  2. Step 2: Identify the correct Artisan options

    The --resource option adds resource methods, and --api modifies them to exclude create/edit views.
  3. Step 3: Combine options correctly

    Using both --resource --api together creates a resource controller optimized for API.
  4. Final Answer:

    php artisan make:controller OrderController --resource --api -> Option C
  5. Quick Check:

    Use --resource and --api for API resource controllers [OK]
Hint: Combine --resource and --api for API-only controllers [OK]
Common Mistakes:
  • Using only --api which creates minimal controller
  • Using --resource alone which includes view methods
  • Assuming --viewless is a valid option
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard