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

CRUD 綜合應用

Shengyou Fan
December 14, 2014

CRUD 綜合應用

Shengyou Fan

December 14, 2014
Tweet

More Decks by Shengyou Fan

Other Decks in Programming

Transcript

  1. ॄኄੋ CRUDʁ • CRUD = Ұݸ resource తඪ४࢛ಈ࡞ - Create

    ݐཱࢿྉ - Read ᩇऔࢿྉ - Update ߋ৽ࢿྉ - Delete ႟আࢿྉ
  2. Route త CRUD • Route ᢛ Controller ೭ؒ CRUD ሣጯ౸త

    ࿏ኸٴ Controller తവ໊ࣜ᜝ ಈࢺ ࿏ኸ ಈ࡞ ໊᜝ GET /resource index resource.index GET /resource/create create resource.create POST /resource store resource.store GET /resource/{id} show resource.show GET /resource/{id}/edit edit resource.edit PUT/PATCH /resource/{id} update resource.update DELETE /resource/{id} destroy resource.destroy ˒ ჩߟɿhttp://laravel.tw/docs/4.2/controllers#restful-resource-controllers
  3. ݱ֊ஈ੒Ռ • ໨લቮៃ׬੒త༗ɿ • index ท໘ • create ท໘ •

    show ท໘ • edit ท໘ (P.S  ౎᪑ᰖࣔ༗᮫ɼؐᔒ༗ಈ࡞)
  4. දᄸಈ࡞ઃఆ • ሡදᄸ㚎తࢿྉૹ౸ሣጯత Controller ಈ ࡞ཫ //  app/views/posts/create.blade.php Form::open([‘route’  =>

     ‘posts.store’,  ‘method’  =>  ‘POST’]) //  app/views/posts/edit.blade.php Form::model($post,  [‘route’  =>  [‘posts.update’,  $post-­‐>id],   ‘method’  =>  ‘POST’]) ˒ ׭ํจ݅ɿhttp://laravel.tw/docs/4.2/html#form-model-binding
  5. ઀Ꮕࢿྉฒ႔ཧ • ࡏ Controller ཫɼઌ઀Ꮕදᄸૹաိతࢿ ྉɼ࠶ૹࢸ Model ႔ཧ //  app/controllers/PostsController.php

    public  function  store() {        Post::create(Input::all());        return  Redirect::route(‘posts.index’)                                      -­‐>with(‘success’,  ‘੒ޭ৽⃧จষ’); } ˒ ׭ํจ݅ɿhttp://laravel.tw/docs/4.2/requests#basic-input
  6. ท໘ᰖࣔ㘤ଉ • ݐཱҰݸ৽త notification త partial viewɼ ՄҎᩋզ၇ᰖࣔ㘤ଉ (࢖༻ bootstrap)

    //  app/views/partials/notifications.blade.php @if  ($message  =  Session::get('success')) <div  class="alert  alert-­‐dismissable  alert-­‐success  backend-­‐ hud">        <button  type="button"  class="close"  data-­‐ dismiss="alert">×</button>        <strong>੒ޭʂ</strong>  {{  $message  }} </div> @endif ˒ ׭ํจ݅ɿhttp://laravel.tw/docs/4.2/validation#working-with-error-messages
  7. ࢿྉᱛᨽ • ኺදᄸૹաိతࢿྉಘઌᒾ查ੋෆੋਖ਼֬ ແ᠍࿙తʁਖ਼֬࠽ೳሜೖࢿྉݿ //  app/controllers/PostsController.php $inputs  =  Input::all(); $validation

     =  Validator::make($inputs,  Post::$rules); if  ($validation-­‐>fails()) {   return  Redirect::back()                                      -­‐>withErrors($validation)-­‐>withInput(); } Post::create($inputs); ˒ ׭ํจ݅ɿhttp://laravel.tw/docs/4.2/validation#basic-usage
  8. ࡨޡ႔ཧ • ፙෆ౸֘චࢿྉ࣌ɼՄҎዎኄ႔ཧʁ /*  ઌᒾ查ࢿྉଘෆଘࡏʁෆଘࡏबճၚ  404  */ //  app/controllers/PostsController.php public

     function  edit($id) {   $post  =  Post::find($id);     if  (is_null($post))   {     App::abort(404);   }        /*  ҎԼུ...  */
  9. ࡨޡ႔ཧ • ፙෆ౸֘චࢿྉ࣌ɼՄҎዎኄ႔ཧʁ /*  Eloquent  ༗Ұݸํ๏᜝ҝ  findOrFail  ፙෆ౸࣌။ࣗಈ  404  */

    //  app/controllers/PostsController.php public  function  update($id) {   $post  =  Post::findOrFail($id);        /*  ҎԼུ...  */
  10. ࡨޡ႔ཧ • ፙෆ౸֘චࢿྉ࣌ɼՄҎዎኄ႔ཧʁ /*  ҃ੋզ၇ՄҎ኷༏խత᪑࢖༻ऀ㘸ፙෆ౸  */ //  app/controllers/PostsController.php public  function

     show($id) {   $post  =  Post::find($id);     if  (is_null($post))   {     return  Redirect::route('home.index')                                              -­‐>with('error',  'ፙෆ౸֘จষ');   }        /*  ҎԼུ...  */
  11. ҆શ๷ڔ • ௚઀઀ड৽⃧ɺߋ৽ࢿྉతಈ࡞༗ةᯃɼ ⃧Ճᱛᨽ csrf_token ఏߴ҆શੑ //  app/controllers/PostsController.php public  function

     __construct() {   $this-­‐>beforeFilter('csrf',  ['on'  =>  'post']); } //  app/filters.php Route::filter('csrf',  function() {   if  (Session::token()  !==  Input::get('_token'))   {     throw  new  Illuminate\Session\TokenMismatchException;   } }); ˒ ׭ํจ݅ɿhttp://laravel.tw/docs/4.2/html#csrf-protection
  12. ෼ท • Laravel ༬ઃबࢧԉ෼ทઃఆɼ୞ཁࡏऔग़ ࢿྉ࣌ɼ೺ get() ׵੒ paginate() ଈՄ •

    ࡏ View ্࢖༻ links()ɼLaravel ࣗಈ㗞ੜ bootstrap ෼ท࿈݁ɼURL ࣗಈ綁ఆ ˒ ׭ํจ݅ɿhttp://laravel.tw/docs/4.2/pagination //  app/controllers/HomeController.php $posts  =  Post::orderBy('created_at',  'desc')-­‐>paginate(5); //  app/views/home/index.blade.php <div  class="text-­‐center">   {{  $posts-­‐>links()  }} </div>