Slide 1

Slide 1 text

Modern Website Building Laravel Framework

Slide 2

Slide 2 text

Jace Ju 大澤木小鐵 http://plurk.com/jaceju http://twitter.com/jaceju http://www.jaceju.net

Slide 3

Slide 3 text

Why Laravel ? 0

Slide 4

Slide 4 text

What Is Web Framework 把麻煩的事簡單化 把重複的事自動化

Slide 5

Slide 5 text

The Basic Features • 有效率地處理使⽤用者的請求。 • ⽤用更抽象化的⽅方式處理資料內容。 • 有效管理與整合前端⽂文件。 • 快速回應各種不同格式的內容。 • 提供基本的安全防護。 • 完整的⽂文件。 • 易於擴充與佈署。

Slide 6

Slide 6 text

How to choose ?

Slide 7

Slide 7 text

About Laravel Framework • Taylor Otwell 於 2011 年 4 月發表 • MIT License • 基於 PHP 5.3 的 MVC Framework • 豐富⽽而完整的⽂文件 • 易懂的 API ⽤用法 • 易於擴充新功能 http://laravel.com/

Slide 8

Slide 8 text

Classy & Fluent Interface return Redirect::to('login') ->with_input(); $comments = Post::find(1)->comments; Asset::container('footer') ->add('example', 'js/example.js'); $url = URL::to_secure('some/uri');

Slide 9

Slide 9 text

Environment 1

Slide 10

Slide 10 text

Environment • 開發 / 本機 • 測試 • 正式

Slide 11

Slide 11 text

Configurations • 資料庫主機 IP • 網址 • 除錯策略 • ...

Slide 12

Slide 12 text

paths.php $environments = array( 'production' => array( 'http://www.example.com*'), 'testing' => array( 'http://test.example.com*'), 'local' => array( 'http://localhost*', '*.dev'), );

Slide 13

Slide 13 text

application/config application/config/production/*.php application/config/testing/*.php application/config/local/*.php application/config/*.php http://laravel.com/docs/install#environments

Slide 14

Slide 14 text

Command Line 2

Slide 15

Slide 15 text

artisan http://laravel.com/docs/artisan/tasks

Slide 16

Slide 16 text

Task php artisan migration php artisan key:generate php artisan test

Slide 17

Slide 17 text

Custom Task // application/tasks/notify.php class Notify_Task { // php artisan notify taylor public function run($arguments) { // Do awesome notifying... } // php artisan notify:urgent public function urgent($arguments) { // This is urgent! } }

Slide 18

Slide 18 text

Migration 3

Slide 19

Slide 19 text

• 快速建⽴立正式與測試資料庫 • 管理 SQL 檔案 • 還原成前⼀一版的 Schema • 切換 DBMS Problems

Slide 20

Slide 20 text

Migration php artisan migrate:install php artisan migrate:make create_users_table

Slide 21

Slide 21 text

Migration class Create_Users_Table { public function up() { Schema::table('users', function($table) { $table->create(); $table->increments('id'); $table->string('username'); $table->string('email')->unique(); $table->timestamps(); }); } public function down() { Schema::drop('users'); } }

Slide 22

Slide 22 text

Migration php artisan migrate // 執⾏行 migration php artisan migrate:rollback // 撤消上⼀一步 http://laravel.com/docs/database/migrations

Slide 23

Slide 23 text

Autoloading 4

Slide 24

Slide 24 text

• require • require_once • autoload (PHP 5) Get Classes

Slide 25

Slide 25 text

Autoloader::directories Autoloader::directories(array( path('app') . 'entities', path('app') . 'repositories', )); http://laravel.com/docs/loading

Slide 26

Slide 26 text

Autoloader::map Autoloader::map(array( 'User' => path('app').'models/user.php', 'Contact' => path('app').'models/contact.php', ));

Slide 27

Slide 27 text

Autoloader::namespaces Autoloader::namespaces(array( 'Doctrine' => path('libraries') . 'Doctrine', 'Zend' => path('libraries') . 'Zend', )); https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

Slide 28

Slide 28 text

Autoloader::underscored Autoloader::underscored(array( 'Swift' => path('libraries') . 'SwiftMailer', ));

Slide 29

Slide 29 text

Alias // application/config/application.php array( 'aliases' => array( 'Asset' => 'Laravel\\Asset', 'Autoloader' => 'Laravel\\Autoloader', // ... ), );

Slide 30

Slide 30 text

Routing 5

Slide 31

Slide 31 text

User-Friendly URLs http://www.example.com/2012/08/first-article http://www.example.com/my/articles

Slide 32

Slide 32 text

RESTful URLs http://www.example.com/category/2 http://www.example.com/category/item/5

Slide 33

Slide 33 text

Event-Driven Routing Route::get('/', function() { return "Hello World!"; });

Slide 34

Slide 34 text

RESTful Routing Route::get('user/(:num)', function($id) { // Get user information }); Route::put('user/(:num)', function($id) { // Update user information });

Slide 35

Slide 35 text

Controller Routing // application/routes.php Route::controller(Controller::detect());

Slide 36

Slide 36 text

Input 6

Slide 37

Slide 37 text

• GET (Query String) • POST (Form) • COOKIE • FILES HTTP Request

Slide 38

Slide 38 text

Input & Cookie Input::get($key); // All inputs Input::query($key); // Query String Input::file($key); // Files Input::json(); $name = Cookie::get('name'); Cookie::put('name', 'Jace', 60);

Slide 39

Slide 39 text

Redirecting With Old Input return Redirect::to('login') ->with_input(); // Get old input Input::flash(); $name = Input::old('name'); http://laravel.com/docs/input#redirecting-with-old-input

Slide 40

Slide 40 text

Validation 7

Slide 41

Slide 41 text

Do Not Trust User Input Directly

Slide 42

Slide 42 text

Do Not Trust User Input Directly 在 FB 上拿到的正妹照 千萬不要急著塞到資料褲 庫

Slide 43

Slide 43 text

Validator $input = Input::all(); $rules = array( 'name' => 'required|max:50', 'email' => 'required|email', ); $validation = Validator::make($input, $rules); if ($validation->fails()) { return $validation->errors; }

Slide 44

Slide 44 text

Data Abstraction 8

Slide 45

Slide 45 text

ORM

Slide 46

Slide 46 text

ORM Orz

Slide 47

Slide 47 text

Eloquent ORM

Slide 48

Slide 48 text

Has One class User extends Eloquent { public function phone() { return $this->has_one('Phone'); } } $phone = User::find(1)->phone;

Slide 49

Slide 49 text

Belongs To class Phone extends Eloquent { public function user() { return $this->belongs_to('User'); } } echo Phone::find(1)->user->email;

Slide 50

Slide 50 text

Has Many class Post extends Eloquent { public function comments() { return $this ->has_many('Comment'); } } $comments = Post::find(1)->comments; http://laravel.com/docs/database/eloquent

Slide 51

Slide 51 text

Output 9

Slide 52

Slide 52 text

View Route::get('/', function() { return View::make('home.index'); }); application/views/home/index.blade.php http://laravel.com/docs/views

Slide 53

Slide 53 text

View In Controller public function action_index() { return View::make('home.index') ->with('template_var', 'value'); }

Slide 54

Slide 54 text

JSON Response::json(array('name' => 'Batman')); Response::eloquent(User::find(1));

Slide 55

Slide 55 text

Assets 10

Slide 56

Slide 56 text

Assets Dependency jQuery jQuery UI jQuery Form

Slide 57

Slide 57 text

Global Registration Asset::add('jquery', 'js/jquery.js'); Asset::add('jquery-ui', 'js/jquery-ui.js', 'jquery');

Slide 58

Slide 58 text

Namespace Registration Asset::container('footer') ->add('example', 'js/example.js'); scripts(); ?> http://laravel.com/docs/views/assets

Slide 59

Slide 59 text

Bundles 11

Slide 60

Slide 60 text

Bundles In Laravel http://bundles.laravel.com/

Slide 61

Slide 61 text

Bundle Installation php artisan bundle:install bundle_name // application/bundles.php return array( 'docs' => array('handles' => 'docs'), 'bundle_name' // 註冊 bundle );

Slide 62

Slide 62 text

Bundle Tasks class Admin_Generate_Task { public function run($arguments) { // Generate the admin! } } php artisan admin::generate http://laravel.com/docs/bundles

Slide 63

Slide 63 text

Summary 12

Slide 64

Slide 64 text

• Form • Errors & Logging • Localization • Authentication • IoC Container • Unit Testing Other Features http://laravel.com/docs

Slide 65

Slide 65 text

• Is Good MVC ? • Secure Request • Fast Response • Simply Data Handling • Extendable The Points

Slide 66

Slide 66 text

Framework 是⽤用來解決問題 ⽽而不是⽤用來製造問題的

Slide 67

Slide 67 text

Thank you