Create Controller in Laravel using command

How to create a new Controller in Laravel Framework web-based application ?. It is actually quite easy and it can be done in a several ways. In my opinion there is two ways to done it :

1. The first one is manually create the Controller file.

This step requires everything to be done in manual. Several tasks need to be done in order to accomplish this step. Below are the sequence of the steps :

Create a new File in controller folder of Laravel web-based application. It exists in app/Http/Controller and the execution can be done from the command line by typing the following command :

touch controller_name
Description :
touch : It is a tool used to create an empty file if it is not exist in the first time.
controller_name : It is the parameter of touch command which also represent name of the which is going to be created and associated with the controller name.

Based on the above output, the controller and also the name of file is HelloController.

But first of all, check the current working directory of the controller file which is going to be created is actually in app/Http/Controller of the project by typing ‘pwd’ command. The usage about the command on detail can be read in article named ‘Check current working directory in Linux with pwd command ‘.

The last thing is to create a route definition which can be defined in routes/web.php whenever Laravel 5.3 is used in the development. But the route definition can be specified in app/Http/routes.php if the version of the Laravel used for the development is below Laravel 5.3. The line is shown as follows :

Route::get('hello', 'HelloController@index');

For testing purpose, a local URL has already been prepared. This is the output’s execution when the URL for an example typed in URL Web browser : http://localhost.xxx.xxx/hello :
Create Controller in Laravel using command

2. The second one is by executing a certain command which is used in a command line using the artisan tool.

By creating Controller file using artisan utility, it can be done by executing the following command with certain pattern as follows :

php artisan make:controller Controller_File_Name
Description : php : It is a utility used to execute php program via command line.
artisan :It is a command-line interface included with Laravel. Provides number of helpful commands that can assist you while you build your Laravel web-based application.
make:controller : It is a additional option command used to create a new Controller.
Controller_File_Name : It is the argument of the command provided as the name of the new Controller which is going to be created.

The above shown that there is a file successfully created and the name is match with the one given in the command executed to create controller file which is FileController. If the file itself is checked, the content which can be found is shown as follows :