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.
For an example :
user@hostname:/var/www/html/laravel-testing/app/Http/Controller# touch HelloController user@hostname:/var/www/html/laravel-testing/app/Http/Controller#
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 ‘.
user@hostname:/var/www/html/laravel-testing/app/Http/Controller# pwd /var/www/html/laravel-testing/app/Http/Controller user@hostname:/var/www/html/laravel-testing/app/Http/Controller#
Following the above execution command, fill the content of the file named HelloController with the script shown below :
<?php namespace App\Http\Controllers; class HelloController extends Controller { public function index(){ return view('hello'); } } ?>
To test the above script whether it is actually running or not, first of all, create a new file named hello.blade.php in resources/views. Below is the content of the file itself :
<!DOCTYPE html> <html> <head> <title>Laravel</title> <link href="//fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css"> <style> html, body { height: 100%; } body { margin: 0; padding: 0; width: 100%; display: table; font-weight: 100; font-family: 'Lato'; } .container { text-align: center; display: table-cell; vertical-align: middle; } .content { text-align: center; display: inline-block; } .title { font-size: 96px; } </style> </head> <body> <div class="container"> <div class="content"> <div class="title">Hello, welcome to Laraland !</div> </div> </div> </body> </html>
The above snippet code is retrieved from the article titled ‘Laravel 5 Hello World from another with this link : http://www.tutorials.kode-blog.com/laravel-hello-world
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 :
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.
This is the example of the above command :
user@hostname:/var/www/html/laravel-testing$ php artisan make:controller FileController Controller created successfully. user@hostname:/var/www/html/laravel-testing$
The result can be seen where the command executed above is a success by checking whether the controller file has been created or not in app/Http/Controller as shown below :
user@hostname:/var/www/html/laravel-testing/app/Http/Controllers$ ls Auth Controller.php FileController.php user@hostname:/var/www/html/laravel-testing/app/Http/Controllers$
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 :
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; class FileController extends Controller { // }
It means, artisan tool has succeed on creating controller named FileController with the above template. The rest of the steps are the same with the manual created controller explained previously.
One thought on “Create Controller in Laravel using command”