5 - PHP Framework Part 1-MTD
5 - PHP Framework Part 1-MTD
5 - PHP Framework Part 1-MTD
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)
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