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

Route 機制

Shengyou Fan
December 13, 2014

Route 機制

Shengyou Fan

December 13, 2014
Tweet

More Decks by Shengyou Fan

Other Decks in Programming

Transcript

  1. Request ӡߦिظ Route Controller Model View index.php Server Request Response

    Client app init ˒ ჩߟ㘸໌ɿhttp://laravel-recipes.com/recipes/52/understanding-the-request-lifecycle
  2. Laravel తӡ࡞ྲྀఔ 1.੥ٻਐೖ public/index.php ཫ 2.bootstrap/start.php ૑ݐጯ༻ఔࣜ࿨ఁଌӡߦ؀ڥ 3.ᐽՍ㚎෦ framework/start.php ഑ஔઃఆ࿨ࡌೖ

    service providers 4.ጯ༻ఔࣜ app/start ࡌೖ 5.ጯ༻ఔࣜ app/routes.php ࡌೖ 6.੥ٻၚೖጯ༻ఔࣜதਐߦ႔ཧ 7.Response ෺݅ճၚࢸ client ˒ ჩߟ㘸໌ɿhttp://laravel.tw/docs/4.2/lifecycle
  3. ॄኄੋ Routeʁ • Route बੋ App ҰᑍҰᑍత໳ېنଇɼن ଇ㚎ࢦఆ㑌Ұݸ࿏༝ՄҎ઀डతಈ࡞ (GETɺPOSTɺPATCHɺDELETE) ٴधཁ

    ၚೖతჩᏐ • Route نଇ။༝্ࣕԼஞҰൺሣɻූ߹ब ။ࣥߦɺएፙෆ౸ब။ճၚ 404 (Not Found)
  4. ҝॄኄཁ༻ Routeʁ • Document Root ࢦఆࡏ publicɼಁաᐽՍ ਐೖᴍ (index.php) ౷Ұ؅ཧॴ༗࿏༝ɼ⃧

    Ճఔࣜᛰ҆શੑ • ಁա Route ઃܭɼॴ༗ App Մ࢖༻త࿏༝ बҰ໨ྃવɻআࡨ࣌ɼՄҎಁա౷Ұత࿏ ኸஞҰഉআࡨޡ
  5. ዎኄઃఆ Routeʁ • ઀Ꮕ GET (ᩇऔ) • ઀Ꮕ POST (ሜೖ)

    • ઀Ꮕ PATCH (ߋ৽) • ઀Ꮕ DELETE (႟আ) Route::get(‘{url}’,  function()  {  //  Closure  }); Route::post(‘{url}’,  function()  {  //  Closure  }); Route::patch(‘{url}’,  function()  {  //  Closure  }); Route::delete(‘{url}’,  function()  {  //  Closure  });
  6. Route ઀ᏅჩᏐ • ઀ᏅඞཁჩᏐ • ઀ᏅબᎩੑჩᏐ Route::get(‘posts/{id}’,  function($id) {  

         return  ‘Post:  ‘.$id; }); Route::get(‘users/{name?}’,  function($name  =  ‘John’) {        return  ‘My  name  is  ‘.$name; });
  7. ݶ੍ჩᏐ֨ࣜ • ࢖༻ where ݶ੍ • ࢖༻ pattern ݶ੍ Route::get(‘posts/{id}’,

     function($id) {        return  ‘Post:  ‘.$id; }) -­‐>where(‘id’,  ‘[0-­‐9]+’); Route::pattern('id',  '[0-­‐9]+'); Route::get(‘posts/{id}’,  function($id) {        return  ‘Post:  ‘.$id; });
  8. ༗䬟ࠣ routesʁ • Q: ᙛ App ṺሜṺେɺRoute ṺሜṺଟ࣌ɼ ໬ଖ૰߹࢖༻኷ଟ Route

    ಛҟޭೳ࣌ɼ೗ Կ஌ಓ໨લత App ༗䬟ࠣ routes 䏆ʁ • A: ՄҎ੥ artisan ڠॿሡॴ༗ routes نଇ ੔ཧ੒ද֨
  9. artisan routes • ྻग़ routes ද֨ - ݺڣṜݸࢦྩɼartisan ။೺ App

    ໨લॴ ༗త route نଇ੔ཧ੒ද֨ - --name={...} ґ໊ࣈ፺ਘ route - --path={...} ґ࿏ኸ፺ਘ route • ൣྫɿ $  php  artisan  routes
  10. ਐ֊ Route ઃఆ • Laravel ؐՄ౥഑ҎԼઃఆ၏ग़ߋଟኧੑత ઃܭ - group -

    prefix - namespace - sub-domain ˒ ჩߟ㘸໌ɿhttp://laravel.tw/docs/4.2/routing
  11. ท໘෼഑ /categories/{id} ෼ྨท / टท(័᧸) /about ᯩଶท /random ᬋػท /login

    ొೖท /posts จষท /posts/{id} /posts/create /posts/{id}/edit /categories/create /categories/{id}/edit /categories
  12. ֊ஈᒾ查ද • ֬ೝࡏ routes.php 㚎༗ - 12 ݸ GET نଇ

    - 1 ݸ୞ೳ઀डᏐࣈత Route Pattern
  13. ᄸݩখ݁ • ࡏṜݸᄸݩཫɼզ၇㘸໌೗Կઃఆ Route نଇɼฒઃܭ/ఆٛྃ Blog త Route ٴ Route

    Pattern • ԼҰݸᄸݩɼզ၇ሡላश Laravel త View ٴᒬ൘Ҿ№ Blade