際際滷

際際滷Share a Scribd company logo
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 1
LARAVEL 5 FRAMEWORK
TABLE OF CONTENTS
1. Gi畛i thi畛u Laravel 5..........................................................................................................................2
2. Download v ci 畉t ........................................................................................................................2
2.1. Chu畉n b畛 m担i tr動畛ng................................................................................................................2
2.2. Ti畉n hnh ci 畉t .....................................................................................................................3
2.2.1. S畛 d畛ng install composer vo th動 m畛c ch畛a project ...........................................................3
2.2.2. S畛 d畛ng l畛nh t畉o project v畛i command line.........................................................................6
3. C叩c thnh ph畉n c董 b畉n c畛a Laravel .................................................................................................9
3.1. Routing....................................................................................................................................9
3.2. T狸m hi畛u c畉u tr炭c MVC trong Laravel..................................................................................... 12
4. C叩c Command line s畛 d畛ng trong Laravel 5 ..................................................................................20
5. p d畛ng project th畛c t畉 - web b叩n hng........................................................................................ 21
5.1. Ph但n t鱈ch ch畛c nng v c董 s畛 d畛 li畛u..................................................................................... 21
5.1.1. Ch畛c nng c畛a website .................................................................................................. 21
5.1.2. C董 s畛 d畛 li畛u.................................................................................................................. 21
5.2. Kh畛i t畉o project v ch畉y th畛.................................................................................................. 21
5.3. T畉o database s畛 d畛ng Migrate ..............................................................................................25
5.4. C畉u h狸nh cho model theo c叩c b畉ng 達 t畉o trong database 畛 thi畉t l畉p quan h畛 gi畛a c叩c b畉ng
27
5.5. Thi畉t k畉 giao di畛n 畛 folder views ...........................................................................................28
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 2
1. GI畛I THI畛U LARAVEL 5
Laravel l framework opensource s畛 d畛ng ng担n ng畛 PHP d湛ng 畛 ph叩t tri畛n c叩c h畛 th担ng websites
l畛n d畛 dng cho vi畛c ph叩t tri畛n n棚n 動畛c c担ng 畛ng l畉p tr狸nh vi棚n s畛 d畛ng r畛ng r達i.
2. DOWNLOAD V CI 畉T
2.1. Chu畉n b畛 m担i tr動畛ng
- Server web (XAMPP, WAMSERVER, )
- Larabel 5
Download: https://github.com/laravel/laravel
- Composer
Download: https://getcomposer.org/
- Th棚m bi畉n m担i tr動畛ng php
Th棚m vo bi畉n path th動 m畛c php trong web server.
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 3
2.2. Ti畉n hnh ci 畉t
2.2.1. S畛 d畛ng install composer vo th動 m畛c ch畛a project
- D湛ng command line download thnh ph畉n c畛a Laravel
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 4
Download thnh c担ng
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 5
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 6
2.2.2. S畛 d畛ng l畛nh t畉o project v畛i command line
- Ci 畉t composer ci 畉t Composer-Setup.exe
- Ki畛m tra l畛nh composer, php, laravel tr棚n command line
- Ki畛m tra c叩c l畛nh c畉n d湛ng, m畛 command line v g探 l畛nh:
Laravel
Composer
Php
- L畛nh t畉o project: cd t畛i th動 m畛c c畉n l動u project
M畛 command line v ch畉y l畛nh:
composer create-project laravel/laravel laravel5demo --prefer-dist
Trong 坦: laravel5demo: l t棚n project
- Ci 畉t thnh c担ng
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 7
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 8
- Ch畉y web demo: vo 動畛ng d畉n trong webserver v th動 m畛c public trong th動 m畛c 達
t畉o trong laravel
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 9
3. CC THNH PH畉N C B畉N C畛A LARAVEL
3.1. Routing
Ch畛c nng: i畛u h動畛ng c叩c request c畛a ng動畛i d湛ng.
- GET
LO畉I 1: i畛u h動畛ng theo request tr棚n url c畛a ng動畛i d湛ng.
M畛 file routers.php 畛 vi畉t i畛u h動畛ng
Code:
Route::get('hello-world', function () {
echo "Routes hello-world";
});
URL: http://localhost:6969/fwlaravel/laravel5/public/hello-world
Ch畉y i畛u h動畛ng tr棚n url c畛a tr狸nh duy畛t web:
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 10
LO畉I 2: Truy c畉p vo thnh ph畉n view:
Code:
T畉o giao di畛n 畛 view:
<form action="<?php echo Asset('dang-nhap') ?>" method="post">
<input type=s"text" name="username"></input>
<input type="password" name="password"></input>
<input type="submit"></input>
</form>
M畛 file routers.php 畛 vi畉t i畛u h動畛ng:
Code:
Route::get('/dangnhap', function () {
return view('trangdangnhap');
});
URL: http://localhost:6969/fwlaravel/laravel5/public/dangnhap
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 11
Ch畉y truy c畉p tr棚n url
POST
PARAMETERS
FILTUERS
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 12
3.2. T狸m hi畛u c畉u tr炭c MVC trong Laravel
T畛ng quan
C叩c x畛 l箪 request t畛 ng動畛i d湛ng t畉t c畉 th担ng Routing.
V D畛 1: L畉y th担ng tin
// appHttproutes.php
Route::get('thongtin',
'HomeController@showi
nfo');
// appHttpControllers/HomeController.php
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpRequests;
class HomeController extends Controller {
public function showinfo() {
echo "th担ng tin c叩 nh但n";
}
}
http://localhost:69
69/fwlaravel/larav
el5/public/thongti
n
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 13
K畉t qu畉 khi ng動畛i d湛ng th畛c hi畛n request:
V D畛 2: Xu畉t th担ng tin t畛 Controller ra view
- X但y d畛ng view ( Truy畛n d畛 li畛u th担ng qua bi畉n $dulieu
<form action="<?php echo Asset('dang-nhap') ?>" method="post">
<input type="text" name="username"></input>
<input type="password" name="password"></input>
<input type="submit"></input>
<h1><?php echo $dulieu; ?></h1>
</form>
- X但y d畛ng Controller
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpRequests;
class HomeController extends Controller
{
public function showinfo() {
echo "th担ng tin c叩 nh但n";
}
public function showinfoview() {
return view('dang-nhap', ['dulieu' => 'D畛 li畛u ng nh畉p']);
}
}
- G畛i i畛u h動畛ng trong Routes
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 14
Route::get('thongtinview', 'HomeController@showinfoview');
- K畉t qu畉 khi ng動畛i d湛ng th畛c hi畛n request
C叩ch t畉o 1 Controller:
M畛 command line t畉i th動 m畛c project v g探 l畛nh: ( V畛i HomController l t棚n c畛a controller
c畉n t畉o)
php artisan make:controller HomeController
K畉t qu畉 t畉o thnh c担ng:
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 15
3.3. Thnh ph畉n View v Controller
- View
Ch炭c nng: Thnh ph畉n th畛 hi畛n giao di畛n.
C叩ch truy xu畉t vo view: C畉u tr炭c th動 m畛c c畛a view
Truy xu畉t view trong Router
Route::get('goi-view', function(){
return view('layout.demo.view');
});
K畉t qu畉 khi th畛c hi畛n request
C叩ch truy畛n d畛 li畛u ra view.
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 16
o T畉o view
<!DOCTYPE html>
<html>
<head>
<title><?php echo $tenphim;?></title>
</head>
<body>
<p>B畉n ang xem phim <?php echo $tenphim;?></p>
<h1>Th畛i gian: <?php echo $thoigian; ?></h1>
</body>
</html>
Truy畛n d畛 li畛u ra view th担ng qua Routes:
Route::get('phim', function() {
$data = array('tenphim' => "zzzzzzzzzzz" ,
'thoigian' => 222);
// return View::make('phim')->with('tenphim', 'Steve zzzzzzzzz');
return View::make('phim', $data);
});
K畉t qu畉 trang
Chia s畉 d畛 li畛u gia c叩c view
T畉o bi畉n chia s畉 d畛 li畛u cho view trong Routes
View::share('title', 'Ti棚u 畛 trang web');
T畉o 2 request cho 2 view 畛 Rotes
Route::get('goi-view', function(){
return view('layout.demo.view');
});
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 17
Route::get('goi-view2', function(){
return view('layout.demo.view2');
});
T畉o 2 view c坦 n畛i dung
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1>View 2</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1>View </h1>
</body>
</html>
K畉t qu畉 khi th畛c hi畛n 2 request t畛i 2 view
View v view 2 c坦 chung ti棚u 畛
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 18
i畛u ph畛i c叩c bi畉n 動畛c chia s畉 trong c叩c view
T畉o View composer: Ch畛 cho view2 s畛 d畛ng bi畉n dulieu_baomat
View::composer('layout.demo.view2', function($view){
$view->with('dulieu_baomat', "D畛 li畛u c畉n b畉o m畉t!");
});
Th畛c hi畛n g畛i request view2 畛 xem d畛 li畛u
Khi view g畛i s畛 d畛ng bi畉n dulieu_baomat s畉 l畛i
Cho view 3 s畛 d畛ng bi畉n dulieu_baomat c畉n th棚m vo composer
View::composer('layout.demo.view2',
'layout.demo.view3', function($view){
$view->with('dulieu_baomat', "D畛 li畛u c畉n b畉o m畉t!");
});
C叩c ph動董ng th畛c th動畛ng s畛 d畛ng c畛a view
Route::get('check-view', function(){
if (view()->exists('layout.demo.view3')) {
return "T畛n t畉i view 3";
}
else {
return "Kh担ng 畛n t畉i view 3";
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 19
}
});
3.4. Blade
T畉o master trong view
<!DOCTYPE html>
<html>
<head>
<title>@yield('title')</title>
</head>
<body>
<h2>@yield('content')</h2>
</body>
</html>
Trang ch畛 k畉 th畛a t畛 master
@extends('layout.master')
@section('title', "Ti棚u 畛")
@section('content')
Trang ch畛
@stop
T畉o request i畛u h動畛ng trong Routes
Route::get('trangchu', function(){
return view('layout.trangchu');
});
K畉t qu畉 ng動畛i d湛ng request
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 20
4. CC COMMAND LINE S畛 D畛NG TRONG LARAVEL 5
L畛nh Ch畛c nng Ghi ch炭
composer create-project laravel/laravel <T棚n
project> --prefer-dist
Kh畛i t畉o project web
v畛i Laravel 5.0
C畉n cd t畛i th動
m畛c ch動a project
tr動畛c khi ch畉y
l畛nh t畉o project.
php artisan make:model <T棚n B畉ng> --migration T畉o model v migrate
php artisan migrate C畉p nh畉t migrate 畛
th棚m b畉ng vo
database sau khi t畉o
migrate v th棚m c担t
trong file
create_teenbang_table
php artisan make:controller <T棚n Controller> T畉o Controller 畛 x畛 l箪
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 21
5. P D畛NG PROJECT TH畛C T畉 - WEB BN HNG
5.1. Ph但n t鱈ch ch畛c nng v c董 s畛 d畛 li畛u
5.1.1. Ch畛c nng c畛a website
A
5.1.2. C董 s畛 d畛 li畛u
B畉ng
STT T棚n B畉ng
1
Cate
5.2. Kh畛i t畉o project v ch畉y th畛
Th担ng tin project:
- T棚n project: ProjectLaravel
T畉o project b畉ng c叩ch s畛 d畛ng command line
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 22
composer create-project laravel/laravel projectlaravel --prefer-dist
T畉o project thnh c担ng ( c坦 sinh ra Application key)
Ki畛m tra l畉i b畉ng c叩ch ch畉y th畛 tr棚n tr狸nh duy畛t
Ch畛nh l畉i c畉u h狸nh kh担ng c畉n vo th動 m畛c public 畛 xem websites.
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 23
Di chuy畛n 2 file ( index.php v .htaccess) t畛 th動 m畛c public ra ngoi th動 m畛c project
M畛 file index.php v ch畛nh l畉i 動畛ng d畉n
C滴:
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once
__DIR__.'/../bootstrap/app.php';
Ch畛nh thnh:
require __DIR__.'/bootstrap/autoload.php';
$app = require_once
__DIR__.'/bootstrap/app.php';
Ch畉y l畉i 動畛ng d畉n m畛i kh担ng c畉n vo th動 m畛c public
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 24
B畉t ch畉 畛 debug 畛 hi畛n th畛 l畛i khi l畉p tr狸nh: M畛 file app.php trong folder
config v 畛i gi叩 tr畛 APP_DEBUG false -> true
debug' => env('APP_DEBUG', true),
C畉u h狸nh database: M畛 file .env 畛 folder g畛c: thay 畛i gi叩 tr畛 DB_HOST,
DB_PORT, DB_DATABASE, ..
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=project_laravel
DB_USERNAME=root
DB_PASSWORD=
Vo phpmyamin 畛 t畉o database c坦 t棚n project_laravel
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 25
5.3. T畉o database s畛 d畛ng Migrate
S畛 d畛ng l畛nh cmd 畛 t畉o migrate v model
T畉o b畉ng Cate
php artisan make:model Cate --migration
M畛 file create_cates_table 達 動畛c t畉o trong folder database/migrations 畛 th棚m c畛t cho
b畉ng
(Vi畉t code trong function up)
public function up()
{
Schema::create('cates', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('alias');
$table->integer('order');
$table->integer('parent_id');
$table->string('keywords');
$table->string('description');
$table->timestamps();
});
}
T動董ng t畛 m畛 file create_users_table 畛 th棚m c畛t cho b畉ng User
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username')->unique();
$table->string('password', 60);
$table->string('email');
$table->tinyInteger('level');
$table->rememberToken();
$table->timestamps();
php artisan make:model <T棚n B畉ng> --migration
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 26
});
}
M畛 command line 畛 folder g畛c v ch畉y l棚n migrate 畛 th棚m b畉ng trong c董 s畛 d畛 li畛u
php artisan migrate
Vo phpmyadmin 畛 ki畛m tra c叩c b畉ng 達 動畛c t畉o trong database
T畉o t動董ng t畛 c叩c b畉ng Product
php artisan make:model Product --migration
Th棚m c畛t cho b畉ng Products, Trong b畉ng Product c坦 t畉o kh坦a ngo畉i v畛i 2 b畉ng Cates v
Users
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('alias');
$table->integer('price');
$table->text('intro');
$table->longText('content');
$table->string('image');
$table->string('keywords');
$table->string('description');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')-
>onDelete('cascade');
$table->integer('cate_id')->unsigned();
$table->foreign('cate_id')->references('id')->on('cates')-
>onDelete('cascade');
$table->timestamps();
});
}
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 27
T畉o t動董ng t畛 c叩c b畉ng Product
php artisan make:model ProductImage --migration
Th棚m c畛t cho b畉ng Products, Trong b畉ng Product c坦 t畉o kh坦a ngo畉i v畛i 2 b畉ng Cates v
Product_Images
public function up()
{
Schema::create('product_images', function (Blueprint $table) {
$table->increments('id');
$table->string('image');
$table->integer('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products')-
>onDelete('cascade');
$table->timestamps();
});
}
Xem design m畛i quan h畛 畛 phpmyadmin
5.4. C畉u h狸nh cho model theo c叩c b畉ng 達 t畉o trong database 畛 thi畉t l畉p quan h畛 gi畛a c叩c b畉ng
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 28
T畉o bi畉n PHP c畉u h狸nh c叩c th担ng s畛 sau:
- Th担ng tin c畛a b畉ng Cate
protected $table = 'cates';
protected $fillable = ['name',
'alias',
'order',
'parent_id',
'keywords',
'description'];
public $timestamp = false;
- M担 t畉 quan h畛 d畛 li畛u c畛a c叩c b畉ng v畛i b畉ng Product
/*
M畛t Cate ch畛a nhi畛u Cate
*/
public function product () {
return $this->hasMany('AppProduct');
}
5.5. Thi畉t k畉 giao di畛n 畛 folder views
C叩c giao di畛n 動畛c l動u trong folder resource/views. T畉o trang master.blade.php
畛 trang master.blade.php s畛 d畛ng l畛nh @yield 畛 th畛 hi畛n ph畉n hi畛n th畛 chung c畛a c叩c
trang
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 29
@yield('content')
Khi 1 trang mu畛n s畛 d畛ng trang master th狸 k畉 th畛a t畛 master v ch畛n section v畛i t畛 kh坦a
t動董ng 畛ng
@extends('admin.master')
@section('content')
// code
@endsection('content')
C叩c code x畛 l箪 javascript v css 動畛c l動u 畛 folder public
Ch畛nh c叩c link c畛a css v js 炭ng 動畛ng d畉n
<link
href=/slideshow/laravel-5-framework/60869735/"{{ url('public/admin/bower_components/bootstrap/dist/css/bootstrap.min.css') }}"
rel="stylesheet">
<script
src=/slideshow/laravel-5-framework/60869735/"{{ url('public/admin/bower_components/jquery/dist/jquery.min.js') }}"></script>
5.6. T畉o controller 畛 x畛 l箪
M畛 command line 畛 folder g畛c 畛 ch畉y l畛nh t畉o controller
php artisan make:controller CateController
Vi畉t hm GET POST cho controller
TRAINING LARAVEL 5 FRAMEWORK 04/13/2016
HIEU TRAN TRUNG 30
class CateController extends Controller
{
public function getAdd () {
return view('admin.cate.add');
}
public function postAdd () {
}
}

More Related Content

Laravel 5 framework

  • 1. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 1 LARAVEL 5 FRAMEWORK TABLE OF CONTENTS 1. Gi畛i thi畛u Laravel 5..........................................................................................................................2 2. Download v ci 畉t ........................................................................................................................2 2.1. Chu畉n b畛 m担i tr動畛ng................................................................................................................2 2.2. Ti畉n hnh ci 畉t .....................................................................................................................3 2.2.1. S畛 d畛ng install composer vo th動 m畛c ch畛a project ...........................................................3 2.2.2. S畛 d畛ng l畛nh t畉o project v畛i command line.........................................................................6 3. C叩c thnh ph畉n c董 b畉n c畛a Laravel .................................................................................................9 3.1. Routing....................................................................................................................................9 3.2. T狸m hi畛u c畉u tr炭c MVC trong Laravel..................................................................................... 12 4. C叩c Command line s畛 d畛ng trong Laravel 5 ..................................................................................20 5. p d畛ng project th畛c t畉 - web b叩n hng........................................................................................ 21 5.1. Ph但n t鱈ch ch畛c nng v c董 s畛 d畛 li畛u..................................................................................... 21 5.1.1. Ch畛c nng c畛a website .................................................................................................. 21 5.1.2. C董 s畛 d畛 li畛u.................................................................................................................. 21 5.2. Kh畛i t畉o project v ch畉y th畛.................................................................................................. 21 5.3. T畉o database s畛 d畛ng Migrate ..............................................................................................25 5.4. C畉u h狸nh cho model theo c叩c b畉ng 達 t畉o trong database 畛 thi畉t l畉p quan h畛 gi畛a c叩c b畉ng 27 5.5. Thi畉t k畉 giao di畛n 畛 folder views ...........................................................................................28
  • 2. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 2 1. GI畛I THI畛U LARAVEL 5 Laravel l framework opensource s畛 d畛ng ng担n ng畛 PHP d湛ng 畛 ph叩t tri畛n c叩c h畛 th担ng websites l畛n d畛 dng cho vi畛c ph叩t tri畛n n棚n 動畛c c担ng 畛ng l畉p tr狸nh vi棚n s畛 d畛ng r畛ng r達i. 2. DOWNLOAD V CI 畉T 2.1. Chu畉n b畛 m担i tr動畛ng - Server web (XAMPP, WAMSERVER, ) - Larabel 5 Download: https://github.com/laravel/laravel - Composer Download: https://getcomposer.org/ - Th棚m bi畉n m担i tr動畛ng php Th棚m vo bi畉n path th動 m畛c php trong web server.
  • 3. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 3 2.2. Ti畉n hnh ci 畉t 2.2.1. S畛 d畛ng install composer vo th動 m畛c ch畛a project - D湛ng command line download thnh ph畉n c畛a Laravel
  • 4. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 4 Download thnh c担ng
  • 5. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 5
  • 6. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 6 2.2.2. S畛 d畛ng l畛nh t畉o project v畛i command line - Ci 畉t composer ci 畉t Composer-Setup.exe - Ki畛m tra l畛nh composer, php, laravel tr棚n command line - Ki畛m tra c叩c l畛nh c畉n d湛ng, m畛 command line v g探 l畛nh: Laravel Composer Php - L畛nh t畉o project: cd t畛i th動 m畛c c畉n l動u project M畛 command line v ch畉y l畛nh: composer create-project laravel/laravel laravel5demo --prefer-dist Trong 坦: laravel5demo: l t棚n project - Ci 畉t thnh c担ng
  • 7. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 7
  • 8. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 8 - Ch畉y web demo: vo 動畛ng d畉n trong webserver v th動 m畛c public trong th動 m畛c 達 t畉o trong laravel
  • 9. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 9 3. CC THNH PH畉N C B畉N C畛A LARAVEL 3.1. Routing Ch畛c nng: i畛u h動畛ng c叩c request c畛a ng動畛i d湛ng. - GET LO畉I 1: i畛u h動畛ng theo request tr棚n url c畛a ng動畛i d湛ng. M畛 file routers.php 畛 vi畉t i畛u h動畛ng Code: Route::get('hello-world', function () { echo "Routes hello-world"; }); URL: http://localhost:6969/fwlaravel/laravel5/public/hello-world Ch畉y i畛u h動畛ng tr棚n url c畛a tr狸nh duy畛t web:
  • 10. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 10 LO畉I 2: Truy c畉p vo thnh ph畉n view: Code: T畉o giao di畛n 畛 view: <form action="<?php echo Asset('dang-nhap') ?>" method="post"> <input type=s"text" name="username"></input> <input type="password" name="password"></input> <input type="submit"></input> </form> M畛 file routers.php 畛 vi畉t i畛u h動畛ng: Code: Route::get('/dangnhap', function () { return view('trangdangnhap'); }); URL: http://localhost:6969/fwlaravel/laravel5/public/dangnhap
  • 11. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 11 Ch畉y truy c畉p tr棚n url POST PARAMETERS FILTUERS
  • 12. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 12 3.2. T狸m hi畛u c畉u tr炭c MVC trong Laravel T畛ng quan C叩c x畛 l箪 request t畛 ng動畛i d湛ng t畉t c畉 th担ng Routing. V D畛 1: L畉y th担ng tin // appHttproutes.php Route::get('thongtin', 'HomeController@showi nfo'); // appHttpControllers/HomeController.php <?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppHttpRequests; class HomeController extends Controller { public function showinfo() { echo "th担ng tin c叩 nh但n"; } } http://localhost:69 69/fwlaravel/larav el5/public/thongti n
  • 13. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 13 K畉t qu畉 khi ng動畛i d湛ng th畛c hi畛n request: V D畛 2: Xu畉t th担ng tin t畛 Controller ra view - X但y d畛ng view ( Truy畛n d畛 li畛u th担ng qua bi畉n $dulieu <form action="<?php echo Asset('dang-nhap') ?>" method="post"> <input type="text" name="username"></input> <input type="password" name="password"></input> <input type="submit"></input> <h1><?php echo $dulieu; ?></h1> </form> - X但y d畛ng Controller <?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppHttpRequests; class HomeController extends Controller { public function showinfo() { echo "th担ng tin c叩 nh但n"; } public function showinfoview() { return view('dang-nhap', ['dulieu' => 'D畛 li畛u ng nh畉p']); } } - G畛i i畛u h動畛ng trong Routes
  • 14. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 14 Route::get('thongtinview', 'HomeController@showinfoview'); - K畉t qu畉 khi ng動畛i d湛ng th畛c hi畛n request C叩ch t畉o 1 Controller: M畛 command line t畉i th動 m畛c project v g探 l畛nh: ( V畛i HomController l t棚n c畛a controller c畉n t畉o) php artisan make:controller HomeController K畉t qu畉 t畉o thnh c担ng:
  • 15. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 15 3.3. Thnh ph畉n View v Controller - View Ch炭c nng: Thnh ph畉n th畛 hi畛n giao di畛n. C叩ch truy xu畉t vo view: C畉u tr炭c th動 m畛c c畛a view Truy xu畉t view trong Router Route::get('goi-view', function(){ return view('layout.demo.view'); }); K畉t qu畉 khi th畛c hi畛n request C叩ch truy畛n d畛 li畛u ra view.
  • 16. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 16 o T畉o view <!DOCTYPE html> <html> <head> <title><?php echo $tenphim;?></title> </head> <body> <p>B畉n ang xem phim <?php echo $tenphim;?></p> <h1>Th畛i gian: <?php echo $thoigian; ?></h1> </body> </html> Truy畛n d畛 li畛u ra view th担ng qua Routes: Route::get('phim', function() { $data = array('tenphim' => "zzzzzzzzzzz" , 'thoigian' => 222); // return View::make('phim')->with('tenphim', 'Steve zzzzzzzzz'); return View::make('phim', $data); }); K畉t qu畉 trang Chia s畉 d畛 li畛u gia c叩c view T畉o bi畉n chia s畉 d畛 li畛u cho view trong Routes View::share('title', 'Ti棚u 畛 trang web'); T畉o 2 request cho 2 view 畛 Rotes Route::get('goi-view', function(){ return view('layout.demo.view'); });
  • 17. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 17 Route::get('goi-view2', function(){ return view('layout.demo.view2'); }); T畉o 2 view c坦 n畛i dung <!DOCTYPE html> <html> <head> <title><?php echo $title ?></title> </head> <body> <h1>View 2</h1> </body> </html> <!DOCTYPE html> <html> <head> <title><?php echo $title ?></title> </head> <body> <h1>View </h1> </body> </html> K畉t qu畉 khi th畛c hi畛n 2 request t畛i 2 view View v view 2 c坦 chung ti棚u 畛
  • 18. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 18 i畛u ph畛i c叩c bi畉n 動畛c chia s畉 trong c叩c view T畉o View composer: Ch畛 cho view2 s畛 d畛ng bi畉n dulieu_baomat View::composer('layout.demo.view2', function($view){ $view->with('dulieu_baomat', "D畛 li畛u c畉n b畉o m畉t!"); }); Th畛c hi畛n g畛i request view2 畛 xem d畛 li畛u Khi view g畛i s畛 d畛ng bi畉n dulieu_baomat s畉 l畛i Cho view 3 s畛 d畛ng bi畉n dulieu_baomat c畉n th棚m vo composer View::composer('layout.demo.view2', 'layout.demo.view3', function($view){ $view->with('dulieu_baomat', "D畛 li畛u c畉n b畉o m畉t!"); }); C叩c ph動董ng th畛c th動畛ng s畛 d畛ng c畛a view Route::get('check-view', function(){ if (view()->exists('layout.demo.view3')) { return "T畛n t畉i view 3"; } else { return "Kh担ng 畛n t畉i view 3";
  • 19. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 19 } }); 3.4. Blade T畉o master trong view <!DOCTYPE html> <html> <head> <title>@yield('title')</title> </head> <body> <h2>@yield('content')</h2> </body> </html> Trang ch畛 k畉 th畛a t畛 master @extends('layout.master') @section('title', "Ti棚u 畛") @section('content') Trang ch畛 @stop T畉o request i畛u h動畛ng trong Routes Route::get('trangchu', function(){ return view('layout.trangchu'); }); K畉t qu畉 ng動畛i d湛ng request
  • 20. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 20 4. CC COMMAND LINE S畛 D畛NG TRONG LARAVEL 5 L畛nh Ch畛c nng Ghi ch炭 composer create-project laravel/laravel <T棚n project> --prefer-dist Kh畛i t畉o project web v畛i Laravel 5.0 C畉n cd t畛i th動 m畛c ch動a project tr動畛c khi ch畉y l畛nh t畉o project. php artisan make:model <T棚n B畉ng> --migration T畉o model v migrate php artisan migrate C畉p nh畉t migrate 畛 th棚m b畉ng vo database sau khi t畉o migrate v th棚m c担t trong file create_teenbang_table php artisan make:controller <T棚n Controller> T畉o Controller 畛 x畛 l箪
  • 21. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 21 5. P D畛NG PROJECT TH畛C T畉 - WEB BN HNG 5.1. Ph但n t鱈ch ch畛c nng v c董 s畛 d畛 li畛u 5.1.1. Ch畛c nng c畛a website A 5.1.2. C董 s畛 d畛 li畛u B畉ng STT T棚n B畉ng 1 Cate 5.2. Kh畛i t畉o project v ch畉y th畛 Th担ng tin project: - T棚n project: ProjectLaravel T畉o project b畉ng c叩ch s畛 d畛ng command line
  • 22. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 22 composer create-project laravel/laravel projectlaravel --prefer-dist T畉o project thnh c担ng ( c坦 sinh ra Application key) Ki畛m tra l畉i b畉ng c叩ch ch畉y th畛 tr棚n tr狸nh duy畛t Ch畛nh l畉i c畉u h狸nh kh担ng c畉n vo th動 m畛c public 畛 xem websites.
  • 23. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 23 Di chuy畛n 2 file ( index.php v .htaccess) t畛 th動 m畛c public ra ngoi th動 m畛c project M畛 file index.php v ch畛nh l畉i 動畛ng d畉n C滴: require __DIR__.'/../bootstrap/autoload.php'; $app = require_once __DIR__.'/../bootstrap/app.php'; Ch畛nh thnh: require __DIR__.'/bootstrap/autoload.php'; $app = require_once __DIR__.'/bootstrap/app.php'; Ch畉y l畉i 動畛ng d畉n m畛i kh担ng c畉n vo th動 m畛c public
  • 24. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 24 B畉t ch畉 畛 debug 畛 hi畛n th畛 l畛i khi l畉p tr狸nh: M畛 file app.php trong folder config v 畛i gi叩 tr畛 APP_DEBUG false -> true debug' => env('APP_DEBUG', true), C畉u h狸nh database: M畛 file .env 畛 folder g畛c: thay 畛i gi叩 tr畛 DB_HOST, DB_PORT, DB_DATABASE, .. DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=project_laravel DB_USERNAME=root DB_PASSWORD= Vo phpmyamin 畛 t畉o database c坦 t棚n project_laravel
  • 25. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 25 5.3. T畉o database s畛 d畛ng Migrate S畛 d畛ng l畛nh cmd 畛 t畉o migrate v model T畉o b畉ng Cate php artisan make:model Cate --migration M畛 file create_cates_table 達 動畛c t畉o trong folder database/migrations 畛 th棚m c畛t cho b畉ng (Vi畉t code trong function up) public function up() { Schema::create('cates', function (Blueprint $table) { $table->increments('id'); $table->string('name')->unique(); $table->string('alias'); $table->integer('order'); $table->integer('parent_id'); $table->string('keywords'); $table->string('description'); $table->timestamps(); }); } T動董ng t畛 m畛 file create_users_table 畛 th棚m c畛t cho b畉ng User public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('username')->unique(); $table->string('password', 60); $table->string('email'); $table->tinyInteger('level'); $table->rememberToken(); $table->timestamps(); php artisan make:model <T棚n B畉ng> --migration
  • 26. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 26 }); } M畛 command line 畛 folder g畛c v ch畉y l棚n migrate 畛 th棚m b畉ng trong c董 s畛 d畛 li畛u php artisan migrate Vo phpmyadmin 畛 ki畛m tra c叩c b畉ng 達 動畛c t畉o trong database T畉o t動董ng t畛 c叩c b畉ng Product php artisan make:model Product --migration Th棚m c畛t cho b畉ng Products, Trong b畉ng Product c坦 t畉o kh坦a ngo畉i v畛i 2 b畉ng Cates v Users public function up() { Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->string('name')->unique(); $table->string('alias'); $table->integer('price'); $table->text('intro'); $table->longText('content'); $table->string('image'); $table->string('keywords'); $table->string('description'); $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users')- >onDelete('cascade'); $table->integer('cate_id')->unsigned(); $table->foreign('cate_id')->references('id')->on('cates')- >onDelete('cascade'); $table->timestamps(); }); }
  • 27. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 27 T畉o t動董ng t畛 c叩c b畉ng Product php artisan make:model ProductImage --migration Th棚m c畛t cho b畉ng Products, Trong b畉ng Product c坦 t畉o kh坦a ngo畉i v畛i 2 b畉ng Cates v Product_Images public function up() { Schema::create('product_images', function (Blueprint $table) { $table->increments('id'); $table->string('image'); $table->integer('product_id')->unsigned(); $table->foreign('product_id')->references('id')->on('products')- >onDelete('cascade'); $table->timestamps(); }); } Xem design m畛i quan h畛 畛 phpmyadmin 5.4. C畉u h狸nh cho model theo c叩c b畉ng 達 t畉o trong database 畛 thi畉t l畉p quan h畛 gi畛a c叩c b畉ng
  • 28. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 28 T畉o bi畉n PHP c畉u h狸nh c叩c th担ng s畛 sau: - Th担ng tin c畛a b畉ng Cate protected $table = 'cates'; protected $fillable = ['name', 'alias', 'order', 'parent_id', 'keywords', 'description']; public $timestamp = false; - M担 t畉 quan h畛 d畛 li畛u c畛a c叩c b畉ng v畛i b畉ng Product /* M畛t Cate ch畛a nhi畛u Cate */ public function product () { return $this->hasMany('AppProduct'); } 5.5. Thi畉t k畉 giao di畛n 畛 folder views C叩c giao di畛n 動畛c l動u trong folder resource/views. T畉o trang master.blade.php 畛 trang master.blade.php s畛 d畛ng l畛nh @yield 畛 th畛 hi畛n ph畉n hi畛n th畛 chung c畛a c叩c trang
  • 29. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 29 @yield('content') Khi 1 trang mu畛n s畛 d畛ng trang master th狸 k畉 th畛a t畛 master v ch畛n section v畛i t畛 kh坦a t動董ng 畛ng @extends('admin.master') @section('content') // code @endsection('content') C叩c code x畛 l箪 javascript v css 動畛c l動u 畛 folder public Ch畛nh c叩c link c畛a css v js 炭ng 動畛ng d畉n <link href=/slideshow/laravel-5-framework/60869735/"{{ url('public/admin/bower_components/bootstrap/dist/css/bootstrap.min.css') }}" rel="stylesheet"> <script src=/slideshow/laravel-5-framework/60869735/"{{ url('public/admin/bower_components/jquery/dist/jquery.min.js') }}"></script> 5.6. T畉o controller 畛 x畛 l箪 M畛 command line 畛 folder g畛c 畛 ch畉y l畛nh t畉o controller php artisan make:controller CateController Vi畉t hm GET POST cho controller
  • 30. TRAINING LARAVEL 5 FRAMEWORK 04/13/2016 HIEU TRAN TRUNG 30 class CateController extends Controller { public function getAdd () { return view('admin.cate.add'); } public function postAdd () { } }