In the previous article, We installed and configured a Laravel application. And in this article We will build on the same project to create a simple Hello Laravel application and look at the key components of Laravel framework.
Artisan Command Line
Artisan is the command line that automates common tasks in Laravel framework. The artisan command line can be used to perform the following tasks and much more:
- Generate boilerplate code – it easily create controllers, models… etc.
- Database migrations – migrations is used to manipulate database objects and can be used to create and drop tables etc.
- Seeding – seeding is a term used to add dummy records to the database.
- Routing
- Run unit tests.
The Way to Use the Artisan Command
Open the terminator and run the following command to view the list of available commands:
1
|
|
Artisan Command To Generate Codes for a Controller
Open the terminator and run the following command to generate codes for Hello Laravel controller:
1
|
|
- php artisan
is used to run the artisan command line.
- make:controller HelloLaravelController
specifies the command that the should run. This command will create codes for a controller HelloLaravelController in /app/Http/Controllers/HelloLaravelController.php.
And then open up the file HelloLaravelController.php
in folder /app/Http/Controllers
.
And you will get the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
- namespace App\Http\Controllers;
: defines the namespace for the controller.
- use Illuminate\Http\Request;
: imports namespaces with the required classes to use in the controller.
- class HelloLaravelController extends Controller
: defines the HelloLaravelController class which extends/inherit the base controller.
- public function index(){}
: defines the default function for the controller.
- public function create(){}
: defines the function that is used to render the create form view.
- public function store(Request $request)
: defines the function that is used to store/save a newly recode into the table/database.
- public function show($id)
: defines the function that is used to retrieves a single recode/resource based on the id.
- public function edit($id)
: defines the function that is used to render the edit form based on the id.
- public function update(Request $request, $id)
defines a function that is used to update a record in the table/database base on the id.
- public function destroy($id)
: defines the function that is used to remove a recode based on the id.
Routing
We will create a new route that will render Hello Laravel in the browser.
Open up file web.php
in folder routes
and add the following codes below:
1 2 3 |
|
Route::get('/hello',function(){...});
: responds to the GET method of the URI hello. function() defines an anonymous function that does the actual work for the requested URI.
return 'Hello Laravel!';
: returns and render Hello Laravel! to the requested browser.
And then go to ther browser and type the uri http://localhost:8000/hello
you will get the output “Hellow Laravel!”.
Route To Controller
Add the following codes in routes/web.php
.
1
|
|
And then open up app/Http/Controllers/HelloLaravelController.php
file and add the following codes below:
1 2 3 4 |
|
And then go to ther browser and type the uri http://localhost:8000/hello
you will get the output “Hello Laravel!”.
Loading the View from the Controller
Open up app/Http/Controllers/HelloLaravelController.php
file and edit the following codes below:
1 2 3 4 |
|
return view('home');
: loads a view named hello.blade.php.
And then create a new file home.blade.php
in folder /resources/views
and add the following codes below:
1
|
|
And then go to ther browser and type the uri http://localhost:8000/hello
you will get the output “Hello Laravel!”.
So far so good, That’s it!!! See ya!!! :)