DEV Community

Cover image for Laravel for Beginners : a Quick Guide - 3
Kartik Bhat
Kartik Bhat

Posted on

Laravel for Beginners : a Quick Guide - 3

I hope you tried to understand my previous post on laravel application structure and how it works in general, Today here I am providing you the code level explanation of it...

You observed the word request raised multiple times while reading about work flow of Laravel application, it goes to routes then to controller then to model and then here and then there.... ok... ok...

I think now you got a doubt regarding how that request actually transits through these hurdles in this cycle ?

Wait, I am coming there itself :)

Request initiates from user interface

User Interface !!!
it is nothing but executed/processed state of your HTML file, now you got it at least :), here in our terminology we call it as "VIEWS"; there is a sub folder present under resources folder (go to file structure and find it). all your so called HTML codes files exists here.

I have one point to add : Laravel, a PHP's framework here even though you are writing HTML code under views folder but still those files should contain .php as their extension. Mind it...

all the files/user interface files created under views folder has an extension .blade.php

What ? .blade !!! What is this again ?

Cool; Let know know about it soon

hitting an URL, Clicking a button may a initiate a request to the application. here a route , say a key word is used to raise a request

Now again come to the main point, hit the URL provided by the command prompt.

I think you got this window,

Alt Text

Right ? now observe your URL bar : 127.0.0.1:8000 (in my case)

here at the end of this address along with a '/' symbol we can call any route. (here there is no specific keyword mentioned so it goes to '/' route)

did't got ? :(

Ok, if you want to call/use any route, first you need to register/define it in the application.

Then How to do it ?

Open routes folder under application folder the open web.php
it looks like,

RouteFile

here you can observe '/' route got defined !!!

do not worry about get() method, I will tell about its importance later

here '/' route is returning a view called welcome

returning a view means it calls a .blade.php file located under resource's subfolder views

route can either return a view or it can call a specific method/function defined under specific Controller i.e control/request then shifts to the Controller's method

Route::get('/', function () {
    return view('welcome');
});
Enter fullscreen mode Exit fullscreen mode

here you can see route '/' is returning a view welcome (no need to append .blade.php extension to it, Laravel manages it automatically)

now let see, how request got transited to controller;
As I already said that route will again call specific method of specific controller,

What is Controller then ?
in general, Controller is authentic PHP class, a .php file located under App -> Http -> Controllers folder (go and check existence of this folder in your application)

Why we need to controller then ?
request received from user interface got processed in the Controller, processed in the sense it either generates data or fetches data from the database with an intervention of Model

How to create controller ?
open command prompt in our Laravel application folder, then enter this command

php artisan make:controller DataController
Enter fullscreen mode Exit fullscreen mode

you can observe a success message

ControllerCreated

this will create a file called DataController under App-> Http-> Controllers Folder

now you need to pass controller from route to this controller's method, Ok before that write a function under DataController File (say a PHP class) , which returns a view welcome

after adding a method/function our DataController looks like this

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DataController extends Controller
{
    public function welcomeFunction() {
        return view('welcome');
    }
}
Enter fullscreen mode Exit fullscreen mode

Write a new route under web.php like

Route::get('welcome', 'DataController::welcomeFunction');
Enter fullscreen mode Exit fullscreen mode

now our web.php file looks like this

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DataController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('welcome', [DataController::class,'welcomeFunction']);
Enter fullscreen mode Exit fullscreen mode

observe DataController file got defined before its usage (look at the top section of web.php , DataController file called with key word use , every controller should be call/define here first then we can use them futher... keep it in mind...)

Ok, Now are ready to hit new route created just now...

open your browser (I hope you already running our Laravel application :) ) then hit

** http://127.0.0.1:8000/welcome **

Bingo,

newRoute

Your new route is perfectly working...

Your are seeing same window/page with both '/' and with '/welcome' routes... check them again... :)

Fine, Hope you got creation of new route and new controller and its function from this article, I will be back with Model interaction with Controller

Thank You

Bye :)

Top comments (0)