30
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à cài đặt ........................................................................................................................ 2 2.1. Chuẩn bị môi trường................................................................................................................ 2 2.2. Tiến hành cài đặt ..................................................................................................................... 3 2.2.1. Sử dụng install composer vào 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 thành 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 hàng........................................................................................ 21 5.1. Phân tích chức năng và cơ sở dữ liệu ..................................................................................... 21 5.1.1. Chức năng 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

Laravel 5 framework

Embed Size (px)

Citation preview

Page 1: Laravel 5 framework

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à cài đặt ........................................................................................................................ 2

2.1. Chuẩn bị môi trường................................................................................................................ 2

2.2. Tiến hành cài đặt ..................................................................................................................... 3

2.2.1. Sử dụng install composer vào 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 thành 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 hàng ........................................................................................ 21

5.1. Phân tích chức năng và cơ sở dữ liệu ..................................................................................... 21

5.1.1. Chức năng 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

Page 2: Laravel 5 framework

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ễ dàng 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À CÀI ĐẶ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 vào biến path thư mục php trong web server.

Page 3: Laravel 5 framework

TRAINING LARAVEL 5 FRAMEWORK 04/13/2016

HIEU TRAN TRUNG 3

2.2. Tiến hành cài đặt

2.2.1. Sử dụng install composer vào thư mục chứa project

- Dùng command line download thành phần của Laravel

Page 4: Laravel 5 framework

TRAINING LARAVEL 5 FRAMEWORK 04/13/2016

HIEU TRAN TRUNG 4

Download thành công

Page 5: Laravel 5 framework

TRAINING LARAVEL 5 FRAMEWORK 04/13/2016

HIEU TRAN TRUNG 5

Page 6: Laravel 5 framework

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 - Cài đặt composer cài đặ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

- Cài đặt thành công

Page 7: Laravel 5 framework

TRAINING LARAVEL 5 FRAMEWORK 04/13/2016

HIEU TRAN TRUNG 7

Page 8: Laravel 5 framework

TRAINING LARAVEL 5 FRAMEWORK 04/13/2016

HIEU TRAN TRUNG 8

- Chạy web demo: vào đường dẫn trong webserver và thư mục public trong thư mục đã

tạo trong laravel

Page 9: Laravel 5 framework

TRAINING LARAVEL 5 FRAMEWORK 04/13/2016

HIEU TRAN TRUNG 9

3. CÁC THÀNH PHẦN CƠ BẢN CỦA LARAVEL

3.1. Routing Chức năng: đ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:

Page 10: Laravel 5 framework

TRAINING LARAVEL 5 FRAMEWORK 04/13/2016

HIEU TRAN TRUNG 10

LOẠI 2: Truy cập vào thành 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

Page 11: Laravel 5 framework

TRAINING LARAVEL 5 FRAMEWORK 04/13/2016

HIEU TRAN TRUNG 11

Chạy truy cập trên url

POST

PARAMETERS

FILTUERS

Page 12: Laravel 5 framework

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

// app\Http\routes.php

Route::get('thongtin',

'HomeController@showi

nfo');

// \app\Http\Controllers/HomeController.php <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class HomeController extends Controller {

public function showinfo() {

echo "thông tin cá nhân";

}

}

http://localhost:69

69/fwlaravel/larav

el5/public/thongti

n

Page 13: Laravel 5 framework

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 App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; 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

Page 14: Laravel 5 framework

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 thành công:

Page 15: Laravel 5 framework

TRAINING LARAVEL 5 FRAMEWORK 04/13/2016

HIEU TRAN TRUNG 15

3.3. Thành phần View và Controller

- View

Chúc năng: Thành phần thể hiện giao diện.

Cách truy xuất vào 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.

Page 16: Laravel 5 framework

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'); });

Page 17: Laravel 5 framework

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 đề

Page 18: Laravel 5 framework

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 vào 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";

Page 19: Laravel 5 framework

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

Page 20: Laravel 5 framework

TRAINING LARAVEL 5 FRAMEWORK 04/13/2016

HIEU TRAN TRUNG 20

4. CÁC COMMAND LINE SỬ DỤNG TRONG LARAVEL 5

Lệnh Chức năng 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 vào 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ý

Page 21: Laravel 5 framework

TRAINING LARAVEL 5 FRAMEWORK 04/13/2016

HIEU TRAN TRUNG 21

5. ÁP DỤNG PROJECT THỰC TẾ - WEB BÁN HÀNG

5.1. Phân tích chức năng và cơ sở dữ liệu

5.1.1. Chức năng 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

Page 22: Laravel 5 framework

TRAINING LARAVEL 5 FRAMEWORK 04/13/2016

HIEU TRAN TRUNG 22

composer create-project laravel/laravel projectlaravel --prefer-dist

Tạo project thành 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 vào thư mục public để xem websites.

Page 23: Laravel 5 framework

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 ngoài 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 thành:

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 vào thư mục public

Page 24: Laravel 5 framework

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=

Vào phpmyamin để tạo database có tên project_laravel

Page 25: Laravel 5 framework

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

Page 26: Laravel 5 framework

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

Vào 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(); });

}

Page 27: Laravel 5 framework

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

Page 28: Laravel 5 framework

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('App\Product');

}

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

Page 29: Laravel 5 framework

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="{{ url('public/admin/bower_components/bootstrap/dist/css/bootstrap.min.css') }}" rel="stylesheet">

<script src="{{ 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 hàm GET POST cho controller

Page 30: Laravel 5 framework

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 () { }

}