5 - PHP Framework Part 1-MTD

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

CII3H4

Aplikasi Berbasis Platform

PHP Framework (1)


Framework
A partial application that consists of many class-
based components that specially intended to
application development

To use the libraries or features that provided by


framework, developer must learn the rules of the
framework, because every framework has their
own rules
Framework Characteristics
Support extensions (feature adding / improving)

Made as simple as possible, but not simpler than without one

Must have documentation and how-to

Scalable dan reusable

Skeletal or assembly structured


Framework Trade-off
Advantages:
– Reusability: speed up development time
– Extensibility: flexible, reduced development complexity
– Same coding style

Disadvantages:
– Learning curve (developer must learn the framework rules)
– Possible to unknown bugs caused by library or environment
dependency
– System flow control isn’t known by developer, so you don’t know
what is happening inside
Model-View-Controller (MVC)
MVC is an architecture pattern for software that
separates the application into three roles:
– Model (business logic)
– View (user interface)
– Controller (process logic / user input)

This separation of roles allows software


development, testing, and maintenance for each
role to be more flexible and independent
Model
Manage the data behavior of application scope

Responds request from Controller to do data


management process

Example: class that represents database table,


active record
View
The UI or forms to interact with user

One Controller can use many Views for different


needs

Example: login page, registration form, dashboard


page
Controller
Accepts and process the user inputs from View

Responds with Model or View calling

Give instructions to Model / View to do some action

Example: Class that isn’t a Model or a View, usually contains


logic and business process
MVC
MVC Flow
User interacts with View (UI) (example: fill the
input field or click a button)
Controller handles the inputs from View, runs the
logic / calls and instructs the Model
Model runs the instructions
Model give a return message / data to Controller
Controller return the message / data from View
and/or return another View
Famous PHP Frameworks
• Laravel
• CodeIgniter
• Yii (Yes It Is)
• CakePHP
• Zend
• Symfony
• etc
Best Framework in the world (by TechnoStacks, 2021)

1. Laravel
2. CodeIgniter
3. Symfony
4. Yii 2
5. Phalcon
6. CakePHP
7. FuelPHP
8. Slim
Laravel
First rank for most used framework in US, and probably in
the world
But for Indonesia, the award goes to CodeIgniter
Why Laravel ?
Open Source (Free)
MVC-based
Many extending libraries
Complete documentation
Large developer community
Not-so-hard to understand and learn
MVC in Laravel

When a URL is opened, the index.php file will be read. In this file, Laravel checks
if the URL is registered in the routing that has been created. If there is a mapping
and the Security is fulfilled (if required, authentication for example) then Laravel
will call the Controller that mapped by routing. Inside the Controller, all the back-
end logic is executed, then it can return the View to the browser. This view is
stored in Caching for faster processing afterwards.
Laravel Framework
Step by step basic
Installing the Laravel
1. Install via Composer (see the Practicum Book)
2. Setting the PHP environment (see the Practicum Book)
3. Open command prompt / console / terminal, go to the project ro
app folder, run:
php artisan serve
Open the browser
Basic Routing
When using Laravel, we can’t access the URL with page that
have .php extension, but we must access it via a route (segmented
URL). Formats:
http://localhost:8000/[segment1]/[segment2]/...
Routing settings are in the routes/web.php file. Here we can set the
URL mapping with the action we want to perform. The method can
also be set if it only accepts certain methods (post, get, or others).
Routing can be set with parameters, groups, prefixes, or
middlewares
Basic Routing
Basic View
Laravel uses Blade template-engine
View file is stored inside resources/views folder
View can be set directly inside Router or called from Controller
function:
view('[file_name]',$data);
[file_name] is the file name without .php.
$data is optional. $data is set if there is data that you want to pass
to the view, so you can use that data inside the view
Basic Controller
Controller file is stored inside app/Http/Controllers folder
You can create the Controller manually or generate it with script:
php artisan make:controller SiteController
If Controller will be used for data management with database, then
the above command can be added –resource, so the default
functions that will be used for data management are generated
automatically by Laravel
php artisan make:controller ProductController --resource
For this Resource Controller, add this in Routing for all data
management URL routes:
Route::resource('product', ProductController::class);
Accessing data passed by Controller to View
Data is an array
How to access it inside the view by access the associative (key)
index:
//controller
$data['rebus'] = 'mie';
return view('welcome', $data);
//view welcome.blade.php
{{ $rebus }} <!-- output: mie -->
Using Asset Files
Asset files (CSS, JS, or image) is stored in public folder so you can
use it globally inside view by a function with a relative path URL:
{{ asset([file path file in public folder]) }}
Common practice is you create a folder named ‘assets’ or so inside
public folder and place the assets inside it
So if you have a CSS file named ‘style.css’ inside assets folder:
<link rel="stylesheet" href="{{ asset('style.css') }}"
/>
Or an image named ‘apa.jpg’ inside assets/img folder:
<img src="{{ asset('assets/img/apa.jpg') }}" />
Any question?
Exercise
Create controller Lat1Controller and create index function

....
public function index(){
$data["nama"]="Agus";
$data["asal"]="Bandung";
return view('v_latihan1', $data);
}
....
Exercise [contd.]
Create view v_latihan1.blade.php

<!DOCTYPE html>
<html>
<head><title>Latihan 1</title></head>
<body>
Nama : {{$nama}} <br/>
Asal : {{$asal}} <br/>
</body>
</html>
Exercise [contd.]
Add the route in routes/web.php

....
Route::get('/lat1’, 'App\Http\Controllers\
Lat1Controller@index');
....
Exercise [contd.]
Test it:
http://localhost:8000/lat1
Exercise [contd.]
• Create a function in controller Lat1:

...
public function method2(){
$data['title'] = "Daftar Mahasiswa";
$data['daf_mhs'] = array(
array("nama" => "Agus", "asal" =>
"Bandung"),
array("nama" => "Budi", "asal" =>
"Jakarta"),
array("nama" => "Roni", "asal" =>
"Surabaya")
);
return view('v_latihan2', $data);
}
...
Exercise [contd.]
Create view v_latihan2.blade.php
<!DOCTYPE html>
<html>
<head>
<title>{{$title}}</title>
</head>
<body>
<h3>{{$title}}</h3>
<table border="1">
<tr><th>No</th><th>Nama</th><th>Asal</th></tr>
@php $no = 1; @endphp
@foreach($daf_mhs as $mhs)
<tr>
<td>{{$no++}}</td>
<td>{{$mhs['nama']}}</td>
<td>{{$mhs['asal']}}</td>
</tr>
@endforeach
</table>
</body>
</html>
Exercise [contd.]
Add the route in routes/web.php

....
Route::get('/lat1/m2', 'App\Http\Controllers\
Lat1Controller@method2');
....
Exercise [contd.]
Test it:
http://localhost:8000/lat1/m2
References
https://laravel.com/docs/master
THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy