Laravel: Controller

  • In order to make Controller work in laravel, we have to define controller in route.
  • To Edit route file, open file web.php
Route::get('/','PagesController@home');
Route::get('/about','PagesController@about');
  • After edit route, we have to create controller file using artisan by Open terminal, type:
php artisan make:controller PageController
  • Next step is create method in PageController.php
<?php

namespace App\Http\Controller;

use Illuminate\Http\Request;

class PagesController extends Controller
{
   public function home()
   {
      return view('index');
   }

   public function about()
   {
     return view('about',['variable'=>'testing var']);
   }
}
  • More artisan syntax to create controller is by adding –resource. This action will generate a controller with default method in it.
php artisan make:controller PendudukController --resource

Source : https://laravel.com/docs/7.x/controllers#defining-controllers

Leave a Reply

Your email address will not be published. Required fields are marked *