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

Laravel 九淺一深

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for JohnsonLu JohnsonLu
July 18, 2014
240

Laravel 九淺一深

Laravel Introduction

Avatar for JohnsonLu

JohnsonLu

July 18, 2014
Tweet

Transcript

  1. 九淺 ⼀一深 • Installation • Install • Route • Blade(View)

    • Controller • Artisan • Autoload • Session • Input • Eloquent ORM 2
  2. Laravel Framework • Trademark of Taylor Otwell • Use Symfony

    Component • Like Ruby on Rails 、 ASP.NET MVC • Version 4.2 4
  3. ⺫⽬目錄 • app/config/app.php • 紀錄debug、url、timezone、key • app/storage • 暫存檔⺫⽬目錄 •

    app/routes.php • 控制 Route 的檔案 • app/filters.php • 控制 Route Filter 檔案 9
  4. ⺫⽬目錄 • app/controllers • Controller ⺫⽬目錄 • app/view • View

    ⺫⽬目錄 • bootstrap/paths.php • 設定基本⺫⽬目錄路徑的檔案(app,public…etc) ! 10
  5. Default Route • app/routes.php ! ! // Demo 1 Route::get('demo1',

    function() { return 'Hello World'; }); 13
  6. Match HTTP Verbs • app/routes.php ! ! // Demo 3

    Route::match(array('GET', 'POST'), 'demo3', function() { return 'Hello World'; }); ! 15
  7. Use https • app/routes.php ! ! // Demo 4 Route::get('demo4',

    array('https', function() { return 'Must be over HTTPS'; })); ! 16
  8. Route Filters • app/routes.php or app/filters.php ! // Demo 6

    Route::filter('old', function() { if (Input::get('age') < 30) { return 'Go back to my home'; } }); ! Route::get('demo6', array('before' => 'old', function() { return 'Too old'; })); ! 18
  9. Blade • Blade 就是 Laravel 提供的 Template Engine • 在

    Laravel 中,使⽤用 Blade 時必須在 View 的檔 名加上 blade.php 20
  10. Blade • app/views • ├── hello.php • ├── includes •

    │ └── header.blade.php • ├── layouts • │ └── default.blade.php • └── pages • ├── demo7.blade.php • └── home.blade.php 21
  11. Blade 參數 • app/routes.php ! // Demo 7 Route::get('demo7', function()

    { return View::make('pages.demo7', array('name' => 'Johnson')); }); ! 22
  12. Blade 參數 • app/view/pages/demo8.blade.php <!DOCTYPE HTML> <html> <head> <!-- incudle

    --> @include('includes.header') </head> <body> <!-- Demo 7 --> ! <!-- Default Value --> {{{ $name or 'Default' }}} ! <!-- not execute --> @{{ not execute }} ! <!-- no escape --> Hello, {{ $name }} </body> </html> ! 23
  13. Blade 連續技 • app/view/includes/header.blade.php <!-- Demo 8 --> <meta charset="utf-8">

    <meta name="description" content="This is Test Layout"> <meta name="author" content="Johnson"> ! <title>Test Layout</title> 25
  14. Blade 連續技 • app/view/layouts/default.blade.php <!DOCTYPE HTML> <!-- Demo 8 -->

    <html> <head> <!-- incudle --> @include('includes.header') </head> <body> <div class="content"> <!-- render content section --> @yield('content') </div> </body> </html> ! 26
  15. Blade 連續技 • app/view/pages/home.blade.php {{-- Demo 8 --}} {{-- Use

    Default Layout --}} @extends('layouts.default') ! {{-- Render content --}} @section('content') i am the home page @stop ! ! 27
  16. Basic Controller • app/controllers/Demo9Controller.php class Demo9Controller extends BaseController { //

    Demo 9 public function sayHello() { return View::make('hello'); } } ! ! 30
  17. RESTful Controllers • app/controllers/Demo10Controller.php class Demo10Controller extends BaseController { //

    Demo 10 public function getIndex() { return "Hello"; } ! public function postProfile() { return 'Profile'; } ! // Any Http Verbs public function anyLogin() { return 'Login'; } ! public function getAdminProfile() { return 'demo10/admin-profile'; } } ! 32
  18. Controller Filter ! class UsersController extends BaseController { public function

    __construct() { // Filter use auth except getLogin $this->beforeFilter('auth', array('except' => 'getLogin')); ! // Filter use csrf on post $this->beforeFilter('csrf', array('on' => 'post')); ! // Filter only foo,bar action $this->afterFilter('log', array('only' => array('fooAction', 'barAction'))); ! // After Filter $this->afterFilter(function() { echo "After"; }); } } ! 33
  19. Controller Filter • app/controllers/Demo11Controller.php ! class Demo11Controller extends BaseController {

    // Demo 11 public function __construct() { $this->beforeFilter('@filterRequests'); } ! public function index(){} ! public function filterRequests($route, $request) { echo 'Filter'; } } ! 34
  20. Artisan • # 查看可以使⽤用的指令清單 • php artisan list ! •

    # 指令參數查詢 (指令前⾯面加上help) • php artisan help migrate ! • # 查詢 Laravel 版本 • php artisan --version 37
  21. Artisan • # 建⽴立 Resource Controller • php artisan controller:make

    PhotoController ! • # 條件判斷,只建⽴立index, show • php artisan controller:make PhotoController --only=index,show ! • # 條件判斷,除了index以外全部建⽴立 • php artisan controller:make PhotoController --except=index 38
  22. Autoload • composer.json "autoload": { "classmap": [ "app/commands", "app/controllers", "app/models",

    "app/database/migrations", "app/database/seeds", "app/tests/TestCase.php" ], "psr-0": { "KK": "app/lib/kklibs" } } 40
  23. Session ! // put session(key, value) Session::put('name', 'Johnson'); ! //

    get session $value = Session::get('name'); ! // getAll session $data = Session::all(); ! // default value $value = Session::get('name', 'default'); 43
  24. Session ! // push array Session::push('user.teams', 'developers'); $value = Session::get('user');

    echo $value['teams'][0]; ! // isset if (Session::has('name')) { echo 'Have'; } ! // release session Session::forget('key'); ! // release all session Session::flush(); ! 44
  25. Input ! // get name $name = Input::get('name'); ! //

    default value $name = Input::get('name', 'Johnson'); ! // isset if (Input::has('name')) {} ! // getAll $input = Input::all(); ! // get name, password $input = Input::only('name', 'password'); ! // except name $input = Input::except('name'); 46
  26. Eloquent ORM • app/models/Member.php ! class Member extends Eloquent {

    protected $table = 'member'; protected $primaryKey = 'account'; } ! 49
  27. Eloquent ORM • app/models/Member.php ! class Member extends Eloquent {

    protected $table = 'member'; protected $primaryKey = 'account'; public $timestamps = false; } 51