Use [] to find tag! Example: [flutter, javascript]

Laravel Exploration Carefully
Laravel Exploration Carefully

Chapter 6

Controller

remove_red_eye 3278 Times
spellcheck 644 Words, 3164 Characters
*Note: this book is still ongoing until it has a finished label.

Laravel is a web framework that uses the concept of MVC (Model View Controller), where we can use model to process data, view  to manage the display, controller to manage the business process.


Location of Controllers

Location of the controller is in app/Http/Controllers or you can see the following image.



Creation of Controllers

In laravel we are provided with very cool tools. We can create controller with command line or other tools. Here I will give several ways to create a controller.

Create with the Command Line

1. You must enter your project directory or you can open your project with visual studio code and press ctrl+`

2. Then you can write the command line as below

php artisan make:controller <controllername>

example: create a controller Login.php

php artisan make:controller Login

example: create a controller Product inside master folder

php artisan make:controller master/product

Create with VS Code

a. Install the Laravel Artisan extension by:


1. Go to the plugins menu

2. search for laravel artisan

3. press the install button


b. After the install process is complete, you can create a laravel controller by:

1. press ctrl+shift+p then type artisan make controller or follow image .gif below.



It's easy isn't it :)


Import Controller on Route

After we learn how to make controllers. Now let's try using our controller on our router.

1. Create a Home controller

2. To import controller Home we can use the code below on our router (web.php)

use App\Http\Controllers\Home;

3. Modify our controller code. like the code below

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class Home extends Controller
{
     employee() function
     {
         $employee = [
             array('id' => 1, 'name' => 'Sukidi', 'age' => 30),
             array('id' => 2, 'name' => 'Parjo', 'age' => 29),
         ];
         return view('home', ['employee' => $employee]); // throws employee data to view
     }
}
4. Then in our home routes, we can create a router like this to use our controller instead of the function.

Route::get('/', [Home::class, 'employee']);

5. then in our home.blade.php view, which was $users we change to $employee so the code is like this.

@extends('layout.main')
@section('content')
<table>
     <thead>
         <tr>
             <th>ID</th>
             <th>Name</th>
             <th>Age</th>
         </tr>
     </thead>
     <tbody>
         @foreach ($employee as $item) {{-- example loop --}}
             <tr>
                 <td>{{ $item['id'] }}</td>
                 <td>{{ $item['name'] }}</td>
                 <td>{{ $item['age'] }} Year</td>
             </tr>
         @endforeach
     </tbody>
</table>
@endsection


Source code is here

Thank you for reading this article, I hope this is useful

Content Navigation