CSE 3100 : Web Programming
Lab 6 (Laravel Architecture and Blade Templates)
Farhan Sadaf Kazi Saeed Alam
Lecturer, Assistant Professor,
Dept of CSE, KUET Dept of CSE, KUET
Email: farhansadaf@[Link] Email: [Link]@[Link]
What is a Framework?
• In terms of s/w development:
• Framework is the collection of methods, classes, or files that the
programmer uses, and they can also extend its functionality by using
their code.
• Something that can be reused and extended.
• Platform for developing software applications.
13-Jan-24 2
Framework
Classes
Packages Framework Functions
Database
Config.
13-Jan-24 3
Web-Frameworks
• PHP web framework: Laravel
• C# based framework: [Link] Core
• Python web frameworks -> Django, Flask
• Java: Spring Boot
• Ruby: Ruby on Rails (Rails)
• TypeScript based: Angular by Google
• JavaScript based: React, [Link], [Link]
and So on…….
13-Jan-24 4
Laravel
• PHP based open source web-framework.
• Follows the model-view-controller (MVC) architectural pattern.
• Taylor Otwell developed Laravel in July 2011.
• Currently Laravel 10.x in operation.
• Most popular web framework.
• Works with the help of Artisan CLI (Command line interface).
13-Jan-24 5
MVC Pattern
13-Jan-24 6
Why Laravel?
• MVC Support and Object-Oriented Approach • Eloquent ORM
• Built in Authentication and Authorization • Templating engine (Blade)
• Packaging System • Task Scheduling
• Multiple File System • Events and Broadcasting
• Artisan Console • Testing
13-Jan-24 7
Steps to follow
• Installation of PHP Local Server (i.e XAMPP)
• Installation of Composer (Package manager)
• Installation Laravel Globally
• Creating a new project
• Installation of a code editor (VS Code)
13-Jan-24 8
Laravel Application Structure
• Once the project is
created in Laravel,
the application
structure generated
as shown in the
screenshot:
13-Jan-24 9
Laravel’s App Directory
• Most of the code is
written in the app
folder.
• The App folder contains
the following sub-
folders:
Console
Exceptions
Http
Models
Providers
13-Jan-24 10
Laravel’s App Directory
• Console: This directory contains all the custom Artisan commands created using
make:command.
• Exceptions: This directory contains the application’s exception handler and is a
good place to add custom exception classes to handle different exceptions thrown
by your application
• Http: This directory contains all your controllers, middleware, views and requests.
• Models: This is a new directory added since Laravel 8 to hold Model files.
• Providers: This directory contains all your service providers for the application.
13-Jan-24 11
Other Directories
• Bootstrap: This directory contains framework bootstrap as well as
configuration files. It also contains Cache directory which contains
framework generated cache files. It also contains the file [Link] that
initializes the scripts required for bootstrap.
• Config: This directory contains all your application’s configuration files.
• Database: This directory contains all database migrations and seeds.
Factories: The factories folder is used to generate a huge number of data records.
Migrations: The migrations folder is used to migrate the database in web application.
Seeds: The seeds folder contains the classes used to perform unit testing database.
13-Jan-24 12
Other Directories
• Storage: This directory contains blade templates, session files, cache
files and other.
• Tests: This directory contains all the test files.
• Vendor: This directory contains all composer dependencies.
13-Jan-24 13
Other Directories
• Public: This directory contains assets like images, js files and CSS. It
contains the front controllers used for initializing the Laravel web
application. ([Link])
• Resources: This directory contains all assets, view files and CSS or LESS
or SASS files. It also contains lang directory to store language files.
• Routes: This directory contains all routes definitions for the application.
php is the file which receives all the requests to your application and here
you can redirect the requests to their respective controller methods.
13-Jan-24 14
Routing in Laravel
• Basic routing is
meant to route
your request to an
appropriate
controller.
• app/Http/[Link]
13-Jan-24 15
Routing in Laravel
• To see the Laravel
homepage with the
help of routing.
• app/Http/[Link]
13-Jan-24 16
Routing in Laravel
• You can also send and capture parameters passed with the URL.
Route::get('name/{name}', Route::get('/{name?}', function ($name = null) {
function ($name) { $data = compact('name');
echo 'Name: '.$name; return view('home')->with($data);
}); });
• Whatever argument that passed after the root URL
([Link] it will be stored in $id and can be used
for further processing
• It can be passed onto view or controller for further processing.
13-Jan-24 17
Routing in Laravel
• You can also send optional parameters passed with the URL.
• The presence of these parameters is not necessary in the URL.
• These parameters are indicated by “?” sign after the name of the
parameters.
Route::get ('/user/{name?}’, function ($name = ‘Saeed’){
echo "Name: ".$name;
});
13-Jan-24 18
Blade Templates
• Blade is a templating engine in a Laravel framework.
• Blade templating engine provides its own structure such as
conditional statements and loops.
• To create a view file, save it with a .[Link] extension instead of
.php extension.
13-Jan-24 19
Displaying Data
Core PHP Syntax Blade Template Syntax
<?php {{ $name }}
echo $name;
?> {!! $name !!}
• Adding ‘!!’ with ‘{{‘
executes/decodes html tags.
13-Jan-24 20
Blade Conditional Directives
• @if, @elseif, @else, and @endif.
• @unless, @endunless.
• @isset, @endisset
@if($name == "")
{{"Name is empty"}}
@elseif($name == "Saeed")
{{"Welcome Saeed"}}
@else
{{"Welcome Guest"}}
@endif
13-Jan-24 21
Blade Looping Directives
• @for and @endfor. @for ($i=1;$i<10;$i++)
• @while and @endwhile. <h2>{{$i}}</h2>
@endfor
• @foreach and @endforeach.
• @break and @continue. @php
$i = 1;
@endphp
@while ($i<=10)
<h2>{{$i}}</h2>
@php $i++; @endphp
@endwhile
13-Jan-24 22
Other Directives
• For Comments: {{-- Comments goes here --}}
• For including: @include
• For raw PHP: @php and @endphp
13-Jan-24 23
Layout Blade Directives
• @yield directive is used to display the contents of a given section.
• @section directive defines a section of a content.
• @extends directive is to specify which layout the child view should
“inherit”.
13-Jan-24 24
Layout Blade Directives
• To render the complete stack contents, pass the name of the stack
to the @stack directive
• @push and @endpush is used to push data into the stack.
13-Jan-24 25
Laravel Controller
• Controller are class based php files.
• Controllers can group related request handling logic into a single
class.
• Brings OOP functionalities.
• Types of Controller:
1. Basic Controllers
2. Single Action Controllers
3. Resource Controllers (CRUD)
13-Jan-24 26
Controller Commands
• Basic Controller:
php artisan make:controller ControllerName
[Link]
<?php [Link]
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use App\Http\Controllers\DemoController;
class DemoController extends Controller
{ Route::get('/', [DemoController::class, 'index']);
public function index(){
return view('home');
}
}
13-Jan-24 27
Controller Commands
• Single Action Controller:
php artisan make:controller ControllerName –invokable
• Resource Controller:
php artisan make:controller ControllerName –resource
13-Jan-24 28
References
• [Link]
• [Link]
• [Link]
13-Jan-24 29
Thank You!!!
13-Jan-24 30