æŽæ°ãããå ¬åŒãŠã§ããµã€ãã®ãã¶ã€ã³
LaravelãåããŠç¥ã£ããšãããªã³ã¯ããªãã«ããããšãèŠããŠããŸãã ãµã€ãã®ååã¯åœŒã®ç±æãå ±æããŸããããæ°ååŸã圌ã¯ã«ããŽãªããã³ã¯ãå€ãããŸãããèããŸããã ä»åŸããã®ãããªçºèšã¯è¡ãããã¹ãã§ã¯ãããŸããã æ°ãããã¶ã€ã³ã§ã¯ãã¡ã€ã³ããŒãžã®äžéšã«ãã倧ããªãã³ã¯ã®åžœåãåãé€ãããããŒãœãã©ã€ãºãããç¹å®ã®ãœãªã¥ãŒã·ã§ã³ã®äœ¿çšãèŠæ±ããïŒãã¬ãŒã ã¯ãŒã¯ã®ã³ã³ããã¹ãã§ïŒæåœçãªèŠåºãããããã«æ®ã£ãŠããŸãã
ããã¥ã¡ã³ãããŒãžã¯ä»¥åã®å€èŠ³ãä¿æããŠããŸãããããŒãžã§ã³ã®åé¢ã«é¢ããŠã¯ããè«ççã«ãªããŸããã 以åã¯ãéçºããŒãžã§ã³ã¯devãšããŠããŒã¯ãããŠããŸããããçŸåšã¯masterãšåŒã°ããããã¯githubã®ãã©ã³ãã®ååã«å¯Ÿå¿ããŠããŸãã
ãã¡ã€ã«æ§é
app / modelsãã©ã«ããŒãåé€ãããŸããã ããã§ãã¢ããªã±ãŒã·ã§ã³ã«é¢ãããã¹ãŠã®ã³ãŒãã¯ã Appåå空éïŒhello psr-4ïŒïŒã®appãã©ã«ããŒã«ä¿åãããŸãã æšæºã®åå空éã¯ã appïŒnameã³ãã³ãã䜿çšããŠArtisanã«ãã£ãŠå€æŽã§ããŸãã
ã³ã³ãããŒã©ãŒãããã«ãŠã§ã¢ãããã³ãªã¯ãšã¹ãïŒLaravel 5.0ã®æ°ããã¯ã©ã¹ã¿ã€ãïŒã¯ã次ã®ããã«app / Httpãã©ã«ããŒã«ã°ã«ãŒãåãããŸãã ãããã®ã¯ã©ã¹ã¯ãã¢ããªã±ãŒã·ã§ã³HTTPã¬ã€ã€ãŒã«å±ããŸãã ãã£ã«ã¿ãŒãæã€åäžã®ãã¡ã€ã«ã®ä»£ããã«ãåããã«ãŠã§ã¢ïŒãã£ã«ã¿ãŒã®ä»£æ¿ãèŠæ±ãšã³ã³ãããŒã©ãŒãžã®ç§»è¡ã®éã«ãããã®ïŒãç¬èªã®ã¯ã©ã¹ãååŸããŸããã
app / Providersãã©ã«ããŒå ã®ã¯ã©ã¹ã¯ã app / startãã©ã«ããŒå ã®ãã¡ã€ã«ã眮ãæããããã«ãªããŸããã ãããã®ãµãŒãã¹ãããã€ããŒã¯ããšã©ãŒåŠçããã®ã³ã°ãã«ãŒãã®ããŒããªã©ãã¢ããªã±ãŒã·ã§ã³ãžã®ããŒãã¯ã©ã¹ãæäŸããŸããåãå Žæã§ãç¬èªã®ãµãŒãã¹ãããã€ããŒãäœæã§ããŸãã
å€èšèªãã¡ã€ã«ãšãã¬ãŒã³ããŒã·ã§ã³ãã¡ã€ã«ã¯ãªãœãŒã¹ãã©ã«ããŒã«ç§»åãããŸããã
å¥çŽ
ãã¹ãŠã®äž»èŠãªLaravelã³ã³ããŒãã³ãã¯ã ç §æ/å¥çŽãªããžããªã«ããã€ã³ã¿ãŒãã§ãŒã¹ãå®è£ ããŸãã ãã®ãªããžããªã«ã¯å€éšäŸåé¢ä¿ã¯ãããŸããã 䟿å©ã§äžå åãããã€ã³ã¿ãŒãã§ã€ã¹ã®ã»ããã䜿çšããŠããããããã«ãããªã³ã°ã«äœ¿çšã§ããŸããäŸåé¢ä¿ã®å®è£ ã¯ããã¡ãµãŒãã®åªãã代æ¿æ段ãšããŠæ©èœããŸãã
ããã¥ã¡ã³ããŒã·ã§ã³
ã«ãŒããã£ãã·ã¥
ã¢ããªã±ãŒã·ã§ã³ãã«ãŒãã³ã³ãããŒã©ãŒã®ã¿ã§æ§æãããŠããå Žåã¯ãæ°ããArtisan routeïŒcacheã³ãã³ãã䜿çšããŠã ã«ãŒããããã®èªã¿èŸŒã¿ãå€§å¹ ã«é«éåã§ããŸãã 100以äžã®ã«ãŒããæã€ã¢ããªã±ãŒã·ã§ã³ã§ãã®æ©èœã䜿çšããŠãã¢ããªã±ãŒã·ã§ã³ã®ãã®éšåã®ããŠã³ããŒãé床ãäžããããšã¯çã«ããªã£ãŠããŸãã
ã«ãŒãããã«ãŠã§ã¢
Laravel 4ãã£ã«ã¿ãŒã¯Laravel 5 HTTPããã«ãŠã§ã¢ã«çœ®ãæããããããªãã¿ã®æšæºèªèšŒãã£ã«ã¿ãŒãšCSRFä¿è·ã¯æ°ããæ¹æ³ã§æžãçŽãããŸããã ããã«ãŠã§ã¢ã¯ãããããçš®é¡ã®ãã£ã«ã¿ãŒã亀æããããã®åäžã®äžè²«ããã€ã³ã¿ãŒãã§ãŒã¹ãæäŸããåŠçã®ããã«ã³ã³ãããŒã©ãŒã«éä¿¡ãããåã«ããªã¯ãšã¹ããç°¡åã«æ€æ»ããã³æåŠã§ããŸãã
ããã¥ã¡ã³ããŒã·ã§ã³
ã³ã³ãããŒã©ãŒã¡ãœããã€ã³ãžã§ã¯ã·ã§ã³
æ¢åã®ã³ã³ã¹ãã©ã¯ã¿ãŒæ³šå ¥ã«å ããŠãã³ã³ãããŒã©ãŒã¡ãœããã§äŸåé¢ä¿ïŒorigãïŒtype-hintäŸåé¢ä¿ïŒãæå®ã§ããããã«ãªããŸããã ã«ãŒãã«ä»ã®ãã©ã¡ãŒã¿ãŒãå«ãŸããŠããå Žåã§ããIoCã³ã³ãããŒã¯äŸåé¢ä¿ãèªåçã«æ¿å ¥ããŸãã
public function createPost(Request $request, PostRepository $posts) { // }
èªèšŒã¹ãã£ãã©ãŒã«ãã£ã³ã°
ç»é²ããŠãŒã¶ãŒèªèšŒãããã³ãã¹ã¯ãŒãã®å埩ããç®±ããåºããŠãããã«å©çšã§ããããã«ãªãã察å¿ãããã¥ãŒã¯resources / views / authã«ãããŸãã ããã«å ããŠãusersããŒãã«ã®ç§»è¡ã«ã¯ãã¬ãŒã ã¯ãŒã¯ãä»å±ããŠããŸãã ãããã®åçŽãªãªãœãŒã¹ãå«ããããšã§ãèªèšŒã®æŽçã«æéãè²»ããããšãªãããããã¿ã€ããè¿ éã«éçºã§ããŸãã èš±å¯ã€ã³ã¿ãŒãã§ãŒã¹ã¯ã auth / loginããã³auth / registerã«ãŒãã§äœ¿çšã§ããŸãã App \ Services \ Auth \ RegistrarãµãŒãã¹ã¯ããŠãŒã¶ãŒã®æ€èšŒãšäœæãæ åœããŸãã
ã€ãã³ããªããžã§ã¯ã
ã€ãã³ãïŒorigãïŒEventsïŒã¯ãæååã§ã¯ãªãã¯ã©ã¹ãšããŠå®çŸ©ã§ããããã«ãªããŸãããäŸïŒ
class PodcastWasPurchased { public $podcast; public function __construct(Podcast $podcast) { $this->podcast = $podcast; } }
åãšåãããã«ãã€ãã³ããéä¿¡ã§ããŸãïŒå ïŒãã£ã¹ãããæžã¿ïŒã
Event::fire(new PodcastWasPurchased($podcast));
ã€ãã³ããã³ãã©ã¯ãããŒã¿ãªã¹ãã®ä»£ããã«ã€ãã³ããªããžã§ã¯ããåãåããŸãã
class ReportPodcastPurchase { public function handle(PodcastWasPurchased $event) { // } }
ããã¥ã¡ã³ããŒã·ã§ã³
ã³ãã³ã/ãã¥ãŒã€ã³ã°
Laravel 4ã§ãµããŒããããŠãããã¥ãŒïŒãªãªãžãã«ïŒãã¥ãŒãžã§ã圢åŒïŒã«å ããŠãLaravel 5ã§ã¯ãã¥ãŒãåçŽãªã³ãã³ããªããžã§ã¯ããšããŠæ瀺ã§ããŸãã ãããã®ã³ãã³ãã¯ã app / Commandsãã©ã«ããŒã«ãããŸãã äŸïŒ
class PurchasePodcast extends Command implements SelfHandling, ShouldBeQueued { use SerializesModels; protected $user, $podcast; /** * Create a new command instance. * * @return void */ public function __construct(User $user, Podcast $podcast) { $this->user = $user; $this->podcast = $podcast; } /** * Execute the command. * * @return void */ public function handle() { // Handle the logic to purchase the podcast... event(new PodcastWasPurchased($this->user, $this->podcast)); } }
LaravelããŒã¹ã³ã³ãããŒã©ãŒã¯ãæ°ããDispatchesCommandsãã©ã°ïŒorigãïŒtraitïŒã䜿çšããŸããããã«ãããå®è¡ããã³ãã³ããéåžžã«ç°¡åã«éä¿¡ã§ããŸãã
$this->dispatch(new PurchasePodcastCommand($user, $podcast));
ã³ãã³ãã¯ãåæããŠå®è¡ãããã¿ã¹ã¯ã«ã䜿çšã§ããŸãïŒæŽåããªãã§ãã ããïŒã å®éãã³ãã³ãã䜿çšããããšã¯ãã¢ããªã±ãŒã·ã§ã³ã解決ããè€éãªã¿ã¹ã¯ãã«ãã»ã«åããåªããæ¹æ³ã§ãã
ããã¥ã¡ã³ããŒã·ã§ã³
ããŒã¿ããŒã¹ãã¥ãŒ
ããŒã¿ããŒã¹ãã¥ãŒãã©ã€ããŒã¯ãããã«äœ¿çšã§ããããã«ãªããŸããã ããŒã¿ããŒã¹å€éšã®è¿œå ã®äŸåé¢ä¿ãå¿ èŠãšããªãã·ã³ãã«ãªããŒã«ã«ãã©ã€ããŒãæäŸããŸãã
Laravelã¹ã±ãžã¥ãŒã©ãŒ
以åã¯ãã³ã³ãœãŒã«ã³ãã³ãã®å®è¡ãèšç»ããããã«ãéçºè ã¯åã³ãã³ããcronã«ããã³ã°ããããå¿ èŠããããå€ãã®é çã®çš®ã«ãªããŸããã ãã®ããã»ã¹ã¯ã¢ããªã±ãŒã·ã§ã³ãšã³ã·ã¹ãã ã§ã¯çºçããªããããSSHçµç±ïŒãŸãã¯Webã€ã³ã¿ãŒãã§ã€ã¹çµç±ïŒã§ãµãŒããŒã«æ¥ç¶ããããã»ã¹ãæåã§å¶åŸ¡ããå¿ èŠããããŸããã ä»ããã¹ãŠãã¯ããã«ç°¡åã«ãªããŸããã Laravelã®ã¿ã¹ã¯ã¹ã±ãžã¥ãŒã©ã䜿çšãããšãã¢ããªã±ãŒã·ã§ã³èªäœã«ã¹ã±ãžã¥ãŒã«ãããã¿ã¹ã¯ããã°ããç°¡åã«è¿œå ã§ããå¿ èŠãªcronã¿ã¹ã¯ã¯1ã€ã ãã«ãªããŸããã
次ã®ããã«ãªããŸãã
$schedule->command('artisan:command')->dailyAt('15:00');
ããã¥ã¡ã³ããŒã·ã§ã³
ãã£ã³ã«ãŒ/ psysh
PHPã¢ãŒãã£ã¶ã³ãã£ã³ã«ãŒããŒã ã¯ããžã£ã¹ãã£ã³ãã«ãã³ã®PsyshïŒPHPã®ããå ç¢ãªREPLïŒã䜿çšããããã«ãªããŸããã Laravel 4ã§Borisã䜿çšããã®ã奜ããªãããã£ãšPsyshã奜ãã«ãªãã§ãããã Windowsã§ãåäœããŸãïŒ éå§ããã«ã¯ãã¿ãŒããã«ã«æžã蟌ã¿ãŸãã
php artisan tinker
ããã³ã
Laravel 5ã¯ãããŸããŸãªãµããã©ã«ããŒãç°å¢èšå®ãšæ··åãã代ããã«ãVance Lucasã®DotEnvã䜿çšããŸãã ãã®ã©ã€ãã©ãªã¯ãç¹å®ã®ã©ã³ã¿ã€ã ç°å¢ã®èšå®ã管çããéåžžã«ç°¡åãªæ¹æ³ãæäŸãããã®å Žã§ç°å¢ãå®çŸ©ã§ããŸãã
ããã¥ã¡ã³ããŒã·ã§ã³
ã©ã©ãŽã§ã«ãšãªãã·ã«
Jeffrey Wayã®Laravel Elixirã¯ããªãœãŒã¹ïŒå ïŒã¢ã»ããïŒãçµã¿ç«ãŠãããã®ã·ã³ãã«ã§è¡šçŸåè±ããªã€ã³ã¿ãŒãã§ãŒã¹ãæäŸããŸãã GruntãŸãã¯Gulpã®åŠç¿ãéå§ããããšãæããŠããå Žåãç°¡åã«ãªããŸãã Elixirã¯ãGulpã®äžéå±€ã§ãããLessãSassãCoffeeScriptãæ§ç¯ããããŒãžã§ã³ã«åºã¥ããŠãã¡ã€ã«ãé£çµãããã£ãã·ã¥ããŸãã 圌ã¯ãã¹ããå®è¡ããããšããã§ããŸãïŒ
ããã¥ã¡ã³ããŒã·ã§ã³
Laravel socialite
Laravel Socialiteã¯Laravel 5.0+ãšäºææ§ã®ãããªãã·ã§ã³ããã±ãŒãžã§ãããOAuthãããã³ã«ã䜿çšããŠèªèšŒãå®å šã«å®è£ ã§ããŸãã çŸåšãFacebookãTwitterãGoogleãããã³GitHubããµããŒãããŠããŸãã ããã¯ã©ã®ããã«èŠããŸããïŒ
public function redirectForAuth() { return Socialize::with('twitter')->redirect(); } public function getUserFromProvider() { $user = Socialize::with('twitter')->user(); }
ããã¥ã¡ã³ããŒã·ã§ã³
ãã©ã€ã·ã¹ãã çµ±å
Laravelã«ã¯ãæã匷åãªFlysystemãã¡ã€ã«ã·ã¹ãã ãããŒã«ã«FSãAmazon S3ãRackspaceã¯ã©ãŠãã¹ãã¬ãŒãžãšã®ã·ãŒã ã¬ã¹ãªçµ±åãæäŸããæœè±¡åã¬ã€ã€ãŒãä»å±ããŠããŸãããããã¯ãã¹ãŠ1ã€ã®ãšã¬ã¬ã³ããªAPIã§ãã Amazon S3ã§ã®ãã¡ã€ã«ã®ä¿åãéåžžã«ç°¡åã«ãªããŸããã
Storage::put('file.txt', 'contents');
ããã¥ã¡ã³ããŒã·ã§ã³
ãã©ãŒã ã®èŠä»¶
Laravel 5.0ã¯ãããã©ãŒã ãªã¯ãšã¹ããïŒå ã®ãã©ãŒã ãªã¯ãšã¹ãïŒãå°å ¥ããŸããããã¯ã Illuminate \ Foundation \ Http \ FormRequestã¯ã©ã¹ã«ãã£ãŠæ¡åŒµãããŸãã ãããã®èŠæ±ãªããžã§ã¯ããã³ã³ãããŒã©ãŒã¡ãœããã«åã蟌ã¿ããŠãŒã¶ãŒå ¥åãæ€èšŒã§ããŸãã ç°¡åãªFormRequestã®äŸïŒ
<?php namespace App\Http\Requests; class RegisterRequest extends FormRequest { public function rules() { return [ 'email' => 'required|email|unique:users', 'password' => 'required|confirmed|min:8', ]; } public function authorize() { return true; } }
ã¯ã©ã¹ãå®çŸ©ãããåŸãã³ã³ãããŒã©ãŒã¡ãœããã§ããã«ã€ããŠããã³ããïŒorigãïŒtype-hintïŒãå®è¡ã§ããŸãã
public function register(RegisterRequest $request) { var_dump($request->input()); }
Laravel IoCã³ã³ãããFormRequestã¯ã©ã¹ã®å®è£ æžã¿ã€ã³ã¹ã¿ã³ã¹ã決å®ãããšããªã¯ãšã¹ãã¯èªåçã«æ€èšŒãããŸã ïŒãªãªãžãã«ïŒæ€èšŒæžã¿ïŒã ããã¯ãã³ã³ãããŒã©ãŒã®ïŒå ã®ïŒã¢ã¯ã·ã§ã³ïŒã¡ãœãããåŒã³åºãããå Žåã FormRequestã§å®çŸ©ãããã«ãŒã«ã«åŸã£ãŠãŠãŒã¶ãŒå ¥åãæ£åžžã«æ€èšŒãããããšã確èªã§ããããšãæå³ããŸãã ããã«ããªã¯ãšã¹ããç¡å¹ãªå Žåããªãã€ã¬ã¯ãïŒèšå®å¯èœïŒã¯ãã¯ã€ãã¯ã¡ãã»ãŒãžïŒãªãªãžãã«ïŒãã©ãã·ã¥ã¡ãã»ãŒãžïŒãã»ãã·ã§ã³ã«éä¿¡ãããå ŽæããŸãã¯JSONæååã§èªåçã«æ©èœããŸãã ãã©ãŒã ã®æ€èšŒããã€ãŠãªãã»ã©å®¹æã«ãªããŸããïŒ
ããã¥ã¡ã³ããŒã·ã§ã³
ã·ã³ãã«ãªã³ã³ãããŒã©ãŒãªã¯ãšã¹ãã®æ€èšŒ
Laravel 5ã®ããŒã¹ã³ã³ãããŒã©ãŒã¯ã ValidatesRequestså±æ§ïŒorigãïŒtraitïŒã䜿çšã§ããããã«ãªããŸããã ãã®æ©èœã¯ãçä¿¡ãªã¯ãšã¹ããæ€èšŒããç°¡åãªæ¹æ³ãæäŸããŸãã FormRequestsãã¢ããªã±ãŒã·ã§ã³ã«ãšã£ãŠé¢åãããå Žåã¯ã以äžãã芧ãã ããã
public function createPost(Request $request) { $this->validate($request, [ 'title' => 'required|max:255', 'body' => 'required', ]); }
æ€èšŒã«å€±æãããšãäŸå€ãã¹ããŒãããé©åãªHTTPããããŒããã©ãŠã¶ãŒã«éãè¿ãããŸãã ãŸããæ€èšŒãšã©ãŒã¯ã»ãã·ã§ã³ã«ããã¬ã¢ã¢ãããïŒå ïŒç¹æ» ïŒããŸããã ãªã¯ãšã¹ããAJAXã«ãã£ãŠéä¿¡ãããå ŽåãLaravelã¯ãããã®ãšã©ãŒãèªåçã«JSONæååã«å€æããŸãã
ããã¥ã¡ã³ããŒã·ã§ã³
æ°ãããžã§ãã¬ãŒã¿ãŒ
ãã¬ãŒã ã¯ãŒã¯æ§é ãæŽæ°ããéã«ãæ°ããArtisanãžã§ãã¬ãŒã¿ãŒã³ãã³ããè¿œå ãããŸããã 詳现ã«ã€ããŠã¯ãã¿ãŒããã«ã«æžã蟌ã¿ãŸã
php artisan list
æ§æãã£ãã·ã¥
次ã®ã³ãã³ãã䜿çšããŠããã¹ãŠã®æ§æãã¡ã€ã«ã1ã€ã®ãã¡ã€ã«ã«çµåããŠãã£ãã·ã¥ã§ããŸãã
php artisan config:cache
symfony vardumper
ãããã°æ å ±ãããã³ãããã人æ°ã®ddãã«ããŒãæŽæ°ãããçŸåšã§ã¯ä¿¡ããããªãã»ã©ã®Symfony VarDumperã䜿çšããŠããŸãããã®Symfony VarDumperã¯ãã«ã©ãŒåºåãæäŸããé åã®æãããã¿/å±éãããè¡ããŸãã ãããè©ŠããŠãã ããïŒ
dd([1, 2, 3]);
5çªç®ã®ããŒãžã§ã³ã®ãªãªãŒã¹ã«éãã Jeffrey Wayã¯ãããªãã¥ãŒããªã¢ã«ãžã®ç¡æã¢ã¯ã»ã¹ãéå§ããŸããããã®ãã¥ãŒããªã¢ã«ã§ã¯ãæ°æ©èœãæ確ã«èª¬æããŠããŸãã ã©ã®ç¹ãŸã§ã¯äžæã§ãã ãããªã¯ãªã³ã©ã€ã³ã§èŠãããšãã§ããŸãããããŠã³ããŒããã¿ã³ã¯å人ã¢ã«ãŠã³ãã«ã€ãªããã®ã§ãåŸã§èŠããå Žåã¯ãçŽæ¥ãªã³ã¯ãæ¢ããŠãããªãã¬ãŒã€ãŒã調ã¹ãŠãã ããã
èªã¿ãããã°ã Matt Staufferã«ããäžé£ã®èšäºããããŸãã
Laravelã¯çŽ æŽããããæ°é®®ãªãã¬ãŒã ã¯ãŒã¯ã§ãã OOPã®ãã¹ããã©ã¯ãã£ã¹ãšæ¢åã®ã³ã³ããŒãã³ããããèªäœã«èç©ããç°¡åã§æ¥œããèšèšãè¡ããã¶ã€ããŒã§ãã
UPD 1ïŒ dr1v3ã®ãã©ãã«ã«æè¬
UPD 2ïŒ ajaxtelamonid ã¯ã第5ããŒãžã§ã³ã®ããã¥ã¡ã³ãã®ç¿»èš³ãžã®ãªã³ã¯ãå ±æããŸãã