Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Laravel Framework Modern Website Building

Laravel Framework Modern Website Building

大澤木小鐵

November 03, 2012
Tweet

More Decks by 大澤木小鐵

Other Decks in Programming

Transcript

  1. Modern Website Building
    Laravel Framework

    View Slide

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

    View Slide

  3. Why Laravel ?
    0

    View Slide

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

    View Slide

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

    View Slide

  6. How to choose ?

    View Slide

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

    View Slide

  8. 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');

    View Slide

  9. Environment
    1

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  14. Command
    Line
    2

    View Slide

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

    View Slide

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

    View Slide

  17. 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!
    }
    }

    View Slide

  18. Migration
    3

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  23. Autoloading
    4

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  27. 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

    View Slide

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

    View Slide

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

    View Slide

  30. Routing
    5

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  36. Input
    6

    View Slide

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

    View Slide

  38. 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);

    View Slide

  39. 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

    View Slide

  40. Validation
    7

    View Slide

  41. Do Not Trust User Input Directly

    View Slide

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

    View Slide

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

    View Slide

  44. Data
    Abstraction
    8

    View Slide

  45. ORM

    View Slide

  46. ORM
    Orz

    View Slide

  47. Eloquent ORM

    View Slide

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

    View Slide

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

    View Slide

  50. 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

    View Slide

  51. Output
    9

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  55. Assets
    10

    View Slide

  56. Assets Dependency
    jQuery
    jQuery UI
    jQuery Form

    View Slide

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


    View Slide

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

    View Slide

  59. Bundles
    11

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  63. Summary
    12

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  67. Thank you

    View Slide