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

使用 Laravel 的前與後

Shengyou Fan
November 27, 2014

使用 Laravel 的前與後

於 2014/11/27 受邀於 彰化師範大學 資訊工程學系系學會 分享 Laravel Framework,主題分為三段:

1. 選一個框架當好朋友,讓您成為開心攻城獅 (https://speakerdeck.com/shengyou/xuan-ge-kuang-jia-dang-hao-peng-you-rang-nin-cheng-wei-kai-xin-gong-cheng-shi)
2. 使用 Laravel 的前與後 (本投影片)
3. 成為全能 PHP 攻城獅 (https://speakerdeck.com/shengyou/cheng-wei-quan-neng-php-gong-cheng-shi)

Shengyou Fan

November 27, 2014
Tweet

More Decks by Shengyou Fan

Other Decks in Programming

Transcript

  1. ؆ใओ୊ • ಁա࢖༻ Laravel Framework ׬੒ಉᒬతሢ ୊㚎༰ɼൺֱ࢖༻ᐽՍᢛ൱తࠩҟɿ - ࢖༻ Homestead

    - 啟࢝ Laravel - ؀ڥᏓᏐઃఆ - Artisan ࢦྩ - ݐཱ Migration/Seeding - ઃఆ Route - ࿈݁ Controller - ࢖༻ᒬ൘Ҿ№ - ࢖༻ ORM/Model - ࢖༻ Package
  2. ॄኄੋ Homesteadʁ • ᬋஶ։ᚙ޻۩Ṻ਼ෳᯑɼҝ߱௿ݐஔ։ᚙ ؀ڥత೉౓ɺݮগݐஔ࣌ؒɺฒఏڙ׬੔ ޻۩᭵ɼLaravel ׭ํਪग़໊ҝ Homestead త㐘ٖػثํҊ •

    Homestead 2.0 ߋ੔߹ྃ Composerɼᩋ homestead త࢖༻ɺૢ࡞ߋՃ؆ศ ˒ ჩߟɿhttp://laravel.tw/docs/4.2/homestead
  3. ҝॄኄཁ Homesteadʁ • ಁա࢖༻㐘ٖػثɼՄ௚઀ࡏ։ᚙػ্໛ ্ٖઢओػ؀ڥɼ֬อఔࣜՄ䆋ॺ • ಁա Virtual Box +

    Vagrant త૊߹ɼᩋෳᯑ త㐘ٖػث҆᧋ɺෳ੡ɺࢄ䆋ɺߋ৽Ꮣಘ ߋՃ༰қɼࣕ Laravel ׭ํಁաṜݸํࣜ ௚઀ఏڙ։ᚙऀҰ੔ݸ։ᚙ޻۩แ
  4. ༗䬟ࠣ޷ྉʁ • Ubuntu 14.04 • PHP 5.6 (with mcrypt) •

    HHVM • Nginx • MySQL • Postgres • Node (With Bower, Grunt, and Gulp) • Redis • Memcached • Beanstalkd • Laravel Envoy • HipChat + Slack
  5. ዎኄ࢖༻ʁ • ҆᧋ Virtual Box • ҆᧋ Vagrant • ҆᧋

    composer ౟݅ • ಁաࢦྩ 啟ಈ/࢑ఀ homestead ˒ ჩߟɿhttp://mattstauffer.co/blog/introducing-laravel-homestead-2.0
  6. Before & After • ᅶୂ੒һ֤ग़ح টɺࣗݐ։ᚙ؀ڥ • 䆋ॺ࣌ՄೳҼҝ؀ ڥࠩҟࣕ଄੒ఔࣜ ᛰແ๏ӡ࡞

    • ౷Ұᅶୂ੒һత։ᚙ ؀ڥతࠩҟ • ։ᚙ؀ڥᢛ্ઢओػ ಉ㑊ɼ։ᚙ࣌ଈՄ༬ ଌఔࣜ䆋ॺ݁Ռ
  7. Before & After • खಈ؅ཧॴ༗ݪ࢝ ᛰ䈕ҊɼएࡏҠ ಈɺෳ੡࣌ग़ࡨब GG • Ҿೖ౟݅ɺঋڃവ

    ࣜݿඅ࣌ਏۤ • ࢖༻ Composer ౷Ұ ؅ཧॴ༗ݩ݅ɼ҆ ᧋ɺߋ৽౎ํศ • ᐽՍ໨㑚݁ߏਗ਼ુ׌ ґর׳ྫɼଈศ৽੒ һ໵ೳແ઀๓࢖༻
  8. ֤։ᚙ֊ஈྲྀఔ [ development ] ։ᚙ༻ɺଌࢼ༻ [ local ] ຊػ୺։ᚙ [

    staging ] ֊ஈੑᚙ䆋 [ production ] ਖ਼্ࣜઢ 㑌ݸ։ᚙᆿ൐ຊػ୺ ᘐཱ։ᚙɼ։ᚙ׬੒ ޙૹճ൛ຊ߇੍ܥ౷ [version control] ݪ࢝ᛰ؅ཧ ఆظኺ൛ຊ߇੍ܥ౷ औग़ޙɼਐߦ੔߹ɺ ᄸݩ౳ଌࢼ ሢҊ֊ஈ׬੒ޙɼਐ ߦ֊ஈੑᚙ䆋ɼڅ٬ 㖽ଌࢼɺ֬ೝ ॴ༗֊ஈ׬੒ޙ䆋ॺ ࢸਖ਼ࣜओػɼሣ֎ެ ։࢖༻
  9. ೗Կઃఆ؀ڥᏓᏐʁ • app/config/{environment}/*.php - Laravel ။ࣗಈࠜᎦ໨લత؀ڥɼࡌೖሣ ጯతઃఆ䈕ɼ෴ሜ༬ઃઃఆ • .env.{environment}.php -

    Laravel 4.1 ։࢝੔߹ phpenv ౟݅ɼ௚઀ ሡ؀ڥᏓᏐࡌೖޙɼ෴ሜ༬ઃઃఆ ˒ ჩߟɿhttp://laravel.tw/docs/configuration#protecting-sensitive-configuration ˒ ჩߟɿhttp://laravel.tw/docs/4.2/configuration#environment-configuration
  10. Before & After • ఔࣜᛰ䆋ॺ౸ෆಉ ओػ࣌ɼ౎ཁखಈ मվ૬᮫ઃఆ值 • ௚઀ሡػහࢿ㘤ሜ ࡏఔࣜᛰ์౸൛ຊ

    ߇੍ཫɼ༗മ࿐෩ ᯃ • ಁա؀ڥઃఆɼఔࣜ ᛰՄҎࣗಈదጯॴࡏ ओػత૬᮫ઃఆ • ಁա phpenv ಈଶࡌ ೖɼػහࢿ㘤ෆ။മ ࿐ࡏ൛ຊ߇੍తݪ࢝ ᛰ㚎
  11. ೗Կ࢖༻ artisanʁ • $ php artisan {command} • ৗ༻తࢦྩ༗ɿ -

    envɺ dump-autoloadɺoptimize - controller:makeɺmigrate:make - migrateɺdb:seed
  12. ॄኄੋ Migrationʁ • ॴ༗ሣ DB ૢ࡞తಈ࡞ɼ౎Ḥሜሣጯతఔ ࣜᛰိ׬੒ (Laravel Schema builder)

    • ෼੒ up / down (લਐ/ޙୀ) • 㑌ݸਓ፤౸ߋ৽൛ຊޙɼಁաࣥߦ migrate बՄҎ༗૬ಉత DB ݁ߏɼए༗໰ ୊໵ՄҎ rollback ճ೭લత㐫ଶ ˒ ჩߟɿhttp://laravel.tw/docs/migrations
  13. ॄኄੋ Seedingʁ • ࡏ։ᚙ࣌ɼԟԟ။धཁࢿྉိ၏ᰖࣔɺ႟ আိଌࢼఔࣜɼୠࢿྉత㚎༰ෆඞੋਅ తɼୠࣗݾݐଠྦྷྃ… • ಁա Laravel త

    table seederɼՄҎշ଎త ሡ၊ࢿྉ౗ਐࢿྉݿɼํศ։ᚙ CRUD Ṝ छҎࢿྉૢ࡞ҝओతޭೳ ˒ ჩߟɿhttp://laravel.tw/docs/migrations#database-seeding
  14. ҝॄኄཁ༻ M & Sʁ • ଟਓ։ᚙ࣌ɼҼҝ৽ޭೳࣕधཁमվ DB ݁ߏɼएᔒ༗ Migration ଇଖଞਓबແ๏

    ஌ಓ DB తࠩҟʀ䆋ॺ࣌ɼDB తमվᔒ ༗ل㑚ɺए༗㐫گ໵ແኺճ෮ • ༻ Seeding ޙɼՄҎշ଎తᩋ DB 㚎༗ଌ ࢼࢿྉՄҎ࢖༻ɼሜ DB ૢ࡞࣌Ꮘ཰େ⃧
  15. Before & After • ॴ༗త DB ૢ࡞ᔒ ༗ل㑚ɼଟਓ։ᚙ ࣌ৗৗ။Ҽҝ DB

    ෆಉ㑊ᖵᎃ • ։ᚙ࣌ɼޫ㗞ੜଌ ࢼࢿྉबՖᎃ኷ଟ ࣌ؒ • ॴ༗ DB मվ౎༗ل 㑚ɼಉ㑊ํศ׌Մ෮ ݪ • Մշ଎㗞ੜ၊ࢿྉɼ ໵Մշ଎ਗ਼ۭࢿྉ ݿɼํศ։ᚙሜࢿྉ ૢ࡞ (CRUD)ޭೳ
  16. ॄኄੋ Routeʁ • Route बੋ App ҰᑍҰᑍత໳ېنଇɼن ଇ㚎ࢦఆ㑌Ұݸ࿏༝ՄҎ઀डతಈ࡞ (GETɺPOST) ٴधཁၚೖతჩᏐ

    • ኺ্ࣕԼஞҰൺሣɻූ߹ब။ࣥߦɺएፙ ෆ౸ब။ճၚ 404 (Not Found) • Մ౥഑ groupɺprefixɺnamespaceɺsub- domain ၏ኧੑઃܭ ˒ ჩߟɿhttp://laravel.tw/docs/routing
  17. ҝॄኄཁ༻ Routeʁ • Document Root ࢦఆࡏ publicɼಁաᐽՍ ਐೖᴍ (index.php) ౷Ұ؅ཧॴ༗࿏༝ɼ⃧

    Ճఔࣜᛰ҆શੑ • ಁա Route ઃܭɼॴ༗ App Մ࢖༻త࿏༝ बҰ໨ྃવɼएग़ࡨ࣌໵ํศআࡨ
  18. Before & After • ॴ༗తท໘౎ཁख ಈ৽⃧Ұݸ *.php 䈕Ҋ • 㑌Ұݸ

    *.php 䈕Ҋ ؐཁ਑ሣՄ઀डత http method ၏൑Ꮧ • ౷Ұ༝ Route ؅ཧ App 㚎ॴ༗త࿏༝ɼ ⃧Ճ։ᚙతᏈ཰ᢛጯ ༻ఔࣜ҆શੑ
  19. ॄኄੋ Controllerʁ • ґর MVC ݪཧɼ႔ཧ Route ૹաိతࢿ ྉɼฒґጯ༻ఔࣜᬓाૢ࡞ Model

    վᏓṶ ଘతࢿྉ • ॴ༗ጯ༻ఔࣜతᬓा༝ Controller ౷Ұ႔ ཧɼController ຊ਎ґޭೳ໋໊ ˒ ჩߟɿhttp://laravel.tw/docs/controllers
  20. Before & After • ఔࣜతૢ࡞ᬓा័ ੋሜࡏ *.php త։ ಄ɼఔࣜᬓाɺDB ૢ࡞ɺࢿྉᰖࣔશ

    ࠞࡏҰىతʮٛେ རṵࣜʯሜ๏ • ఔࣜૢ࡞ᬓा෼໳ผ ྨ༝ෆಉత Class ႔ ཧɼClass 㚎֤ Function ໵୞ሢ஫႔ ཧҰݸᬓाɼࡏ෼޻ ᢛҡޢ্౎ߋਗ਼ુ
  21. ॄኄੋ/ҝॄኄཁᒬ൘ʁ • Ұݸ MVC ໢࿏ጯ༻ఔࣜ௚઀᪑࢖༻ऀޓ ಈత෦㟨बੋ View • ࡏመ࡞ท໘࣌ɼ኷ଟท໘త HTML

    DOM ଖመੋॏෳతɼҝݮগҡޢଟ㟨ॏෳతݪ ࢝ᛰɼฒᩋ։ᚙߋ؆ศɼզ၇धཁಁաᒬ ൘ሡท໘૬ಉతეմᘐཱग़ိ
  22. Laravel తᒬ൘Ҿ№ • Laravel ਑ሣሜ໢࿏ጯ༻ఔࣜҰఆधཁతᒬ ൘ޭೳఏڙྃ Blade Ṝݸᒬ൘Ҿ№ • ಁա

    Blade ޠ๏ɼՄҎᩋท໘༗਌ሱ᮫ ܎ɼᩋᒬ൘៺ঝ (template inheritance)Ꮣಘ ؆ᄸɼ׌Մሡท໘ॏෳత෦㟨፥෼੒ეմ (section)ɼ࠶Ҿೖࢸท໘ሣጯతҐஔଈՄ ˒ ჩߟɿhttp://laravel.tw/docs/4.2/templates
  23. ዎኄ༻ Bladeʁ • ॴ༗త view ౎౷Ұ์ࡏ views ࢿྉᇄఈԼ • ґর׳ྫɼओᒬ൘။์ࡏ

    layouts ࢿྉᇄ 㚎ʀࢠᒬ൘။์ࡏ֘ resource ࢿྉᇄ㚎 • blade ᒬ൘త෭䈕໊໋໊ҝ *.blade.php
  24. Before & After • ෆಉท໘ཫ૬ಉత ეմᷮೳ༻ include తํࣜࡏෆಉท໘ Ҿೖɺኧੑֱ௿ •

    ಁա Blade ኷༰қత ሡ໢ท፥੒ओ൘ɺࢠ ൘ٴეմ • ෆಉეմៃ፥ղޙɼ ༰қॏෳ࢖༻ɼ։ᚙ ࣌ᷮधሢ஫ࡏეմ㚎 ଈՄ
  25. Laravel త Eloquent • Eloquent ੋ Laravel ఏڙత ORM ޻۩ɼґ

    রҎԼ׳ྫɼᩋ DB ૢ࡞Ꮣಘ༰қɿ - Ұݸ resource ሣጯ౸ DB ཫతҰݸ table - Ұݸ model ሣጯ౸ table ཫతҰݸ row - model ੋᄸᏐɺtable ੋෳᏐ - ኺ Eloquent औग़ိత౦੢ੋ Collection
  26. Before & After • ॴ༗᪑ࢿྉݿૢ࡞ ૬᮫తಈ࡞౎ཁࣗ ݾԼ SQL ޠ๏ •

    ࡏ૊߹ SQL ޠ๏ ࣌ɼཁখ৺೚Կ༗ ҆શੑྀٙతࣈ۲ • ಁա Eloquentɼૢ ࡞ DB ब૾ሜᏌड़ ۟Ұᒬɼࢿྉදؒ త᮫࿆໵኷௚᧷ • ຊ਎बቮ਑ሣ҆શ ੑ၏๷ڔɼݮগॳ ላऀՄೳతࡨޡ
  27. ॄኄੋ Packageʁ • Package ؆ᄸိ㘸बੋ Laravel తᎷॆఔ ࣜɼಁա҆᧋ Package बՄҎ௚઀࢖༻ผ

    ਓሜ޷తޭೳɼՃշ։ᚙ଎౓ • ࡏ 3.0 ࣌ڣ Bundleɼ4.0 Ҏޙվ᜝ Package • ֓೦ಉ Symfony త BundleʀRails త Gems
  28. Կ႔ਘፙ Packageʁ • ፺ਘҾ№ɿ - Packagist - Packalyst • ᡒ

    େਆ/ಉࣄ/๎༑ తਪન • ؃ผਓత Open Source ཫ༻䬟ࠣʁ
  29. ೗ԿબᎩ Packageʁ • ჩߟ Package ፺ਘҾ№্త Downloadsɺ TrustɺPopularity ౳Ꮠ值၏ჩߟࢦඪ •

    ؃ Package త࡞ऀੋ୭ʁ • ᡒผਓతਪનɺ෦མ֨తհ঺ • ଟࢼزݸಉྨܕత Packageɼፙग़࠷ॱࣗ ݾखతိ༻
  30. ೗Կ҆᧋ Packageʁ • ࢔ࡉ㕕ᩇ README (֬ೝ൛ຊɺن֨) • ௐ੔ composer.json /

    composer update • ઃఆ ServiceProviderɺAlias (ࢹधٻ) • Publish ઃఆ (ࢹधٻ) • Migrate DB (ࢹधٻ) ˒ Ҏ Sentry 2 ҝྫɿhttps://cartalyst.com/manual/sentry#installation
  31. Before & After • ૝ཁ࢖༻ผਓሜ޷ తവࣜݿɼཁखಈ Լࡌɺ҆᧋ɺ੔߹ ਐࣗݾతఔࣜᛰ • ಁա

    Laravel Package తઃఆɼผਓቮៃሜ ޷తവࣜݿՄҎਝ౓ త੔߹౸ࣗݾతሢҊ ཫɼ᜾ࡏڊਓతݞ᡺ ্