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

連結 Controller

連結 Controller

Shengyou Fan

December 13, 2014
Tweet

More Decks by Shengyou Fan

Other Decks in Programming

Transcript

  1. ॄኄੋ Controllerʁ • ґর MVC ݪཧɼRoute ႔ཧ Request ฒґ ጯ༻ఔࣜᬓा෼഑څ

    Controller႔ཧɼ༝ Controller ૢ࡞ Models ٴճၚ Views • ሡጯ༻ఔࣜతཫሡෳᯑతఔࣜૢ࡞ᬓाɼ ᘐཱ੒ Class Ҏརҡޢɼኺ Classɺ Function త໋໊बՄҎେகᘃग़ଖޭೳ
  2. artisan controller:make • 㗞ੜ Controller 䈕Ҋ - ݺڣṜݸࢦྩɼartisan ။ґఏڙత໊᜝ 㗞ੜ

    Controller 䈕Ҋฒ์ஔ౸ app/ controllers ఈԼ - ໋໊׳ྫ {‘M’odelෳᏐ}Controller • ൣྫɿ $  php  artisan  controller:make  PostsController
  3. Controller ݁ߏ • 㗞ੜग़ိత Controller ґ RESTful ֓೦Մ ე෼ग़ҎԼಈ࡞ ಈࢺ

    ࿏ኸ ಈ࡞ ໊᜝ 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
  4. ઃఆ Route • ሡಈ࡞᫚څ Controller Route::get(‘posts’,  ‘PostsController@index’); Route::get(‘posts/create’,  ‘PostsController@create’); Route::post(‘posts’,

     ‘PostsController@store’); Route::get(‘posts/{id}’,  ‘PostsController@show’); Route::get(‘posts/{id}/edit’,  ‘PostsController@edit’); Route::patch(‘posts/{id}’,  ‘PostsController@update’); Route::delete(‘posts/{id}’,  ‘PostsController@destroy’);
  5. 㢨 Route ໋໊ • ՄҎҝ㑌Ұݸ Route औҰݸ໊ࣈɼํศࡏ view ཫ࢖༻ route()

    helper ઃఆ࿈݁ɼ Laravel ໵။ᒾ查 route ੋ൱ଘࡏʁ • ໋໊׳ྫɿresources.verb //  routes.php Route::get(‘posts/{id}’,  [‘as’  =>  ‘posts.show’,  ‘uses’  =>   ‘PostsController@show’]); //  app/views/home/index.blade.php <a  href=”{{  route(‘posts.show’,  $id)  }}”>Read  more</a>
  6. ೺ᬓाൖ཭ Route • ೺ݪຊሜࡏ routes.php ཫతఔࣜᛰൖ౸ሣ ጯత Controller ཫతሣጯಈ࡞ •

    मվ Views ཫݪຊత url() ሜ๏ɼվ੒ route() తሜ๏ //  app/controllers/PostsController.php public  function  show($id) {        return  View::make(‘posts.show’); }
  7. ੔߹ Model ૹࢿྉ • ࡏ Controller ཫ೺֘ทཁᰖࣔతࢿྉ༻ Model 查ᨑग़ိɼฒૹ౸ Views

    ཫ //  app/controllers/PostsController.php public  function  show($id) {        $post  =  Post::find($id);        $data  =  compact(‘post’);                return  View::make(‘posts.show’,  $data); }
  8. ࡏ Views ཫᰖࣔࢿྉ • ᙛ Controller ೺ࢿྉ४උ޷ૹ౸ Views ޙɼࡏ Views

    ཫबՄҎ௚઀औ༻֘෺݅ိ ᰖࣔࢿྉ //  app/views/home/index.blade.php @foreach($posts  as  $post)        <h1>{{{  $post-­‐>title  }}}</h1>        <p>{{{  $post-­‐>content  }}}</p> @endforeach
  9. ᄸݩখ݁ • ࡏṜݸᄸݩཫɼզ၇ላश೗Կ೺ጯ༻ఔࣜ త߇੍ᬓाኺ Route ൖࢸ Controllerɼฒ ࡏ Controller ཫ查ᨑࢿྉޙɼૹࢸ

    View ཫ ᰖࣔ • ԼҰݸᄸݩɼզ၇ሡ૰߹ጯ༻ॴላɼ׬੒ ጯ༻ఔࣜతॴ༗ CRUD ಈ࡞