Laravel 11ãããŸããïŒãºã¡ã±ãŒãšã©ã©ãã©ãŒæåïŒæ°æ©èœã玹ä»ããŸã
Laravel 11ã®ã€ã³ã¹ããŒã«
Laravel 11ã¯PHP8.2以äžãå¿ èŠã§ã
composer create-project laravel/laravel:^11.0 example-app
cd example-app
åçåãããã¢ããªã±ãŒã·ã§ã³æ§é
Laravel 11 ã§ã¯ãæ¢åã®ã¢ããªã±ãŒã·ã§ã³ã«å€æŽãå ããããšãªããæ°ããLaravel ã¢ããªã±ãŒã·ã§ã³åãã«åçåãããã¢ããªã±ãŒã·ã§ã³æ§é ãå°å ¥ãããŠããŸããæ°ããã¢ããªã±ãŒã·ã§ã³æ§é ã¯ãLaravel éçºè ããã§ã«ããç¥ã£ãŠããæŠå¿µã®å€ããä¿æããªãããããç¡é§ããªããããçŸä»£çãªãšã¯ã¹ããªãšã³ã¹ãæäŸããããšãç®çãšããŠããŸãã以äžã§ã¯ãLaravel ã®æ°ããã¢ããªã±ãŒã·ã§ã³æ§é ã®ãã€ã©ã€ãã«ã€ããŠèª¬æããŸãã
ã¢ããªã±ãŒã·ã§ã³ã®ããŒãã¹ãã©ãã ãã¡ã€ã«
ãã®bootstrap/app.php
ãã¡ã€ã«ã¯ãã³ãŒããã¡ãŒã¹ãã®ã¢ããªã±ãŒã·ã§ã³æ§æãã¡ã€ã«ãšããŠåŸ©æŽ»ããŸããããã®ãã¡ã€ã«ãããã¢ããªã±ãŒã·ã§ã³ã®ã«ãŒãã£ã³ã°ãããã«ãŠã§ã¢ããµãŒãã¹ ãããã€ããŒãäŸå€åŠçãªã©ãã«ã¹ã¿ãã€ãºã§ããŸãããã®ãã¡ã€ã«ã¯ã以åã¯ã¢ããªã±ãŒã·ã§ã³ã®ãã¡ã€ã«æ§é å
šäœã«æ£åšããŠããããŸããŸãªé«ã¬ãã«ã®ã¢ããªã±ãŒã·ã§ã³åäœèšå®ãçµ±åããŸãã
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
//
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
ãµãŒãã¹ãããã€ããŒ
ããã©ã«ãã® Laravel ã¢ããªã±ãŒã·ã§ã³æ§é ã«ã¯ 5 ã€ã®ãµãŒãã¹ãããã€ããŒãå«ãŸããŠããŸãããLaravel 11 ã«ã¯ 1 ã€ã®ãµãŒãã¹ãããã€ããŒã®ã¿ãå«ãŸããŠããŸãAppServiceProviderã以åã®ãµãŒãã¹ ãããã€ããŒã®æ©èœã¯ ã«çµã¿èŸŒãŸããŠããbootstrap/app.phpããã¬ãŒã ã¯ãŒã¯ã«ãã£ãŠèªåçã«åŠçãããããã¢ããªã±ãŒã·ã§ã³ã® ã«é 眮ãããå ŽåããããŸãAppServiceProviderã
ããšãã°ãã€ãã³ãæ€åºãããã©ã«ãã§æå¹ã«ãªããã€ãã³ããšãã®ãªã¹ããŒãæåã§ç»é²ããå¿ èŠãã»ãšãã©ãªããªããŸããããã ããã€ãã³ããæåã§ç»é²ããå¿ èŠãããå Žåã¯ã ã§ç°¡åã«ç»é²ã§ããŸãAppServiceProviderãåæ§ã«ã以åã« ã«ç»é²ããã«ãŒã ã¢ãã« ãã€ã³ãã£ã³ã°ãŸãã¯èªèšŒã²ãŒããã ã«AuthServiceProviderç»é²ãããå¯èœæ§ããããŸãAppServiceProviderã
èšå®ãã¡ã€ã«
ç°å¢å€æ°ã®äœ¿çšãæ¡åŒµããã.env.exampleæ°ãã Laravel ã¢ããªã±ãŒã·ã§ã³ã«å«ãŸãããã¡ã€ã«ã«ãã¬ãŒã¹ãã«ããŒãè¿œå ãããŸããããã®ãããã»ãŒãã¹ãŠã®ã³ã¢ ãã¬ãŒã ã¯ãŒã¯æ©èœã¯.envãåå¥ã®æ§æãã¡ã€ã«ã§ã¯ãªãã¢ããªã±ãŒã·ã§ã³ã®ãã¡ã€ã«ãä»ããŠæ§æã§ããŸãããããã£ãŠãconfigããã©ã«ãã§ã¯ãã£ã¬ã¯ããªã«ã¯æ§æãã¡ã€ã«ãå«ãŸããªããªããŸããã
代ããã«ãæ°ãã Artisan ã³ãã³ãã䜿çšããŠæ§æãã¡ã€ã«ãå ¬éã§ããŸãconfig:publishãããã«ãããã«ã¹ã¿ãã€ãºãããæ§æãã¡ã€ã«ã®ã¿ãå ¬éã§ããŸãã
php artisan config:publish
ãã¡ããããã¬ãŒã ã¯ãŒã¯ã®ãã¹ãŠã®æ§æãã¡ã€ã«ãç°¡åã«å ¬éããããšãã§ããŸãã
php artisan config:publish --all
ãªããã€ã³ API ãšãããŒããã£ã¹ã ã«ãŒãã£ã³ã°
ããã³Route ãã¡ã€ã«ã¯api.phpãchannels.phpå€ãã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ãããã®ãã¡ã€ã«ãå¿ èŠãšããªããããããã©ã«ãã§ã¯ååšããªããªããŸããã代ããã«ãåçŽãª Artisan ã³ãã³ãã䜿çšããŠäœæããããšãã§ããŸãã
php artisan install:api
php artisan install:broadcasting
ããã«ãŠã§ã¢
以åã¯ãæ°ãã Laravel ã¢ããªã±ãŒã·ã§ã³ã«ã¯ 9 ã€ã®ããã«ãŠã§ã¢ãå«ãŸããŠããŸããããããã®ããã«ãŠã§ã¢ã¯ããªã¯ãšã¹ãã®èªèšŒãå ¥åæååã®ããªãã³ã°ãCSRF ããŒã¯ã³ã®æ€èšŒãªã©ã®ããŸããŸãªã¿ã¹ã¯ãå®è¡ããŸããã
Laravel 11 ã§ã¯ããããã®ããã«ãŠã§ã¢ã¯ãã¬ãŒã ã¯ãŒã¯èªäœã«ç§»åãããŠãããããã¢ããªã±ãŒã·ã§ã³ã®æ§é ãããã°ãããšã¯ãããŸããããããã®ããã«ãŠã§ã¢ã®åäœãã«ã¹ã¿ãã€ãºããæ°ããã¡ãœããããã¬ãŒã ã¯ãŒã¯ã«è¿œå ãããŠãããã¢ããªã±ãŒã·ã§ã³ã®bootstrap/app.phpãã¡ã€ã«ããåŒã³åºãããšãã§ããŸãã
->withMiddleware(function (Middleware $middleware) {
$middleware->validateCsrfTokens(
except: ['stripe/*']
);
$middleware->web(append: [
EnsureUserIsSubscribed::class,
])
})
ãã¹ãŠã®ããã«ãŠã§ã¢ã¯ã¢ããªã±ãŒã·ã§ã³ã® ãä»ããŠç°¡åã«ã«ã¹ã¿ãã€ãºã§ããããbootstrap/app.phpãå¥åã® HTTP ãã«ãŒãã«ãã¯ã©ã¹ã®å¿ èŠæ§ããªããªããŸããã
ã¹ã±ãžã¥ãŒã«èšå®
æ°ããScheduleãã¡ãµãŒãã䜿çšãããšãã¹ã±ãžã¥ãŒã«ãããã¿ã¹ã¯ãã¢ããªã±ãŒã·ã§ã³ã®routes/console.phpãã¡ã€ã«ã§çŽæ¥å®çŸ©ã§ããããã«ãªããå¥åã®ã³ã³ãœãŒã«ãã«ãŒãã«ãã¯ã©ã¹ãäžèŠã«ãªããŸãã
use Illuminate\Support\Facades\Schedule;
Schedule::command('emails:send')->daily();
äŸå€åŠç
bootstrap/app.phpã«ãŒãã£ã³ã°ãããã«ãŠã§ã¢ãšåæ§ã«ãäŸå€åŠçãåå¥ã®äŸå€ãã³ãã©ãŒ ã¯ã©ã¹ã§ã¯ãªãã¢ããªã±ãŒã·ã§ã³ã®ãã¡ã€ã«ããã«ã¹ã¿ãã€ãºã§ããããã«ãªããæ°ãã Laravel ã¢ããªã±ãŒã·ã§ã³ã«å«ãŸãããã¡ã€ã«å šäœã®æ°ãåæžãããŸãã
->withExceptions(function (Exceptions $exceptions) {
$exceptions->dontReport(MissedFlightException::class);
$exceptions->reportable(function (InvalidOrderException $e) {
// ...
});
})
ã¢ããªã±ãŒã·ã§ã³ã®ããã©ã«ã
databaseããã©ã«ãã§ã¯ãæ°ãã Laravel ã¢ããªã±ãŒã·ã§ã³ã¯ãLaravel ã®ã»ãã·ã§ã³ããã£ãã·ã¥ããã¥ãŒã®ãã©ã€ããŒã ãã§ãªããããŒã¿ããŒã¹ ã¹ãã¬ãŒãžã«ã SQLite ã䜿çšããŸããããã«ãããè¿œå ã®ãœãããŠã§ã¢ãã€ã³ã¹ããŒã«ããããè¿œå ã®ããŒã¿ããŒã¹ç§»è¡ãäœæãããããããšãªããæ°ãã Laravel ã¢ããªã±ãŒã·ã§ã³ãäœæããåŸããã«ã¢ããªã±ãŒã·ã§ã³ã®æ§ç¯ãéå§ã§ããŸãã
ããã«ãæéã®çµéãšãšãã«ãdatabaseãããã® Laravel ãµãŒãã¹ã®ãã©ã€ããŒã¯ãå€ãã®ã¢ããªã±ãŒã·ã§ã³ ã³ã³ããã¹ãã§æ¬çªç°å¢ã§äœ¿çšã§ããã»ã©ååã«å ç¢ã«ãªããŸããããããã£ãŠãããŒã«ã« ã¢ããªã±ãŒã·ã§ã³ãšéçšã¢ããªã±ãŒã·ã§ã³ã®äž¡æ¹ã«è³¢æã§çµ±äžãããéžæè¢ãæäŸããŸãã
ã©ã©ãã«ãªããŒã
Laravel Reverb ã¯ãè¶ é«éã§ã¹ã±ãŒã©ãã«ãªãªã¢ã«ã¿ã€ã WebSocket éä¿¡ã Laravel ã¢ããªã±ãŒã·ã§ã³ã«çŽæ¥ãããããLaravel Echo ãªã©ã® Laravel ã®æ¢åã®ã€ãã³ã ãããŒããã£ã¹ã ããŒã« ã¹ã€ãŒããšã®ã·ãŒã ã¬ã¹ãªçµ±åãæäŸããŸãã
php artisan reverb:start
ããã«ãReverb 㯠Redis ã®ãããªãã·ã¥/ãµãã¹ã¯ã©ã€ãæ©èœãä»ããæ°Žå¹³ã¹ã±ãŒãªã³ã°ããµããŒãããŠãããåäžã®é«éèŠã¢ããªã±ãŒã·ã§ã³ããµããŒãããè€æ°ã®ããã¯ãšã³ã Reverb ãµãŒããŒå šäœã« WebSocket ãã©ãã£ãã¯ãåæ£ã§ããŸãã
Laravel Reverb ã®è©³çŽ°ã«ã€ããŠã¯ãReverb ã®å®å šãªããã¥ã¡ã³ããåç §ããŠãã ããã
1 ç§ãããã®ã¬ãŒãå¶é
Laravel ã¯ãHTTP ãªã¯ãšã¹ãããã¥ãŒã«å ¥ãããããžã§ãã®ã¬ãŒã ãªããã¿ãŒãå«ãããã¹ãŠã®ã¬ãŒã ãªããã¿ãŒã«å¯ŸããŠã1 ç§ããããã®ã¬ãŒãå¶éããµããŒãããããã«ãªããŸããã以åã¯ãLaravel ã®ã¬ãŒã ãªããã¿ãŒã¯ã1 åããããã®ç²åºŠã«å¶éãããŠããŸããã
RateLimiter::for('invoices', function (Request $request) {
return Limit::perSecond(1);
});
Laravel ã§ã®ã¬ãŒãå¶éã®è©³çŽ°ã«ã€ããŠã¯ãã¬ãŒãå¶éã®ããã¥ã¡ã³ããåç §ããŠãã ããã
ãã«ã¹ã«ãŒãã£ã³ã°
æ°ããLaravel 11ã¢ããªã±ãŒã·ã§ã³ã«ã¯healthã«ãŒãã£ã³ã°ãã£ã¬ã¯ãã£ããå«ãŸããŠããããµãŒãããŒãã£ã®ã¢ããªã±ãŒã·ã§ã³ãã«ã¹ã¢ãã¿ãªã³ã°ãµãŒãã¹ãKubernetesãªã©ã®ãªãŒã±ã¹ãã¬ãŒã·ã§ã³ã·ã¹ãã ã«ãã£ãŠåŒã³åºãããåçŽãªãã«ã¹ãã§ãã¯ãšã³ããã€ã³ããå®çŸ©ããããLaravelã«æ瀺ããŸããããã©ã«ãã§ã¯ããã®ã«ãŒãã¯æ¬¡ã®å Žæã§æäŸãããŸã/upã
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
ãã®ã«ãŒãã«å¯Ÿã㊠HTTP ãªã¯ãšã¹ããè¡ããããšãLaravel ã¯DiagnosingHealthã€ãã³ããéåºããŠãã¢ããªã±ãŒã·ã§ã³ã«é¢é£ããè¿œå ã®ãã«ã¹ãã§ãã¯ãå®è¡ã§ããããã«ããŸãã
æ£åžžãªæå·åããŒã®ããŒããŒã·ã§ã³
Laravel ã¯ã¢ããªã±ãŒã·ã§ã³ã®ã»ãã·ã§ã³ Cookie ãå«ããã¹ãŠã® Cookie ãæå·åãããããåºæ¬çã« Laravel ã¢ããªã±ãŒã·ã§ã³ãžã®ãã¹ãŠã®ãªã¯ãšã¹ãã¯æå·åã«äŸåããŸãããã ãããã®ãããã¢ããªã±ãŒã·ã§ã³ã®æå·åããŒãããŒããŒã·ã§ã³ãããšããã¹ãŠã®ãŠãŒã¶ãŒãã¢ããªã±ãŒã·ã§ã³ãããã°ã¢ãŠããããŸãããŸãã以åã®æå·éµã§æå·åãããããŒã¿ã¯åŸ©å·ã§ããªããªããŸãã
Laravel 11 ã§ã¯ãç°å¢å€æ°ã䜿çšããŠã¢ããªã±ãŒã·ã§ã³ã®ä»¥åã®æå·åããŒãã«ã³ãåºåãã®ãªã¹ããšããŠå®çŸ©ã§ããŸãAPP_PREVIOUS_KEYSã
å€ãæå·åãããšããLaravel ã¯åžžã«ç°å¢å€æ°å ã«ãããçŸåšã®ãæå·åããŒã䜿çšããŸãAPP_KEYãå€ã埩å·åãããšããLaravel ã¯æåã«çŸåšã®ããŒãè©Šè¡ããŸããçŸåšã®ããŒã䜿çšãã埩å·åã倱æããå ŽåãLaravel ã¯ããŒã® 1 ã€ã§å€ã埩å·åã§ãããŸã§ã以åã®ãã¹ãŠã®ããŒãè©ŠããŸãã
ãã®æ£åžžãªåŸ©å·åã®ã¢ãããŒãã«ãããæå·åããŒãããŒããŒã·ã§ã³ãããå Žåã§ãããŠãŒã¶ãŒã¯ã¢ããªã±ãŒã·ã§ã³ãäžæããããšãªã䜿çšãç¶ããããšãã§ããŸãã
Laravel ã§ã®æå·åã®è©³çŽ°ã«ã€ããŠã¯ãæå·åã«é¢ããããã¥ã¡ã³ããåç §ããŠãã ããã
å³ææ€èšŒ
Laravel Prompts ã¯ããã¬ãŒã¹ãã«ã㌠ããã¹ããæ€èšŒãªã©ã®ãã©ãŠã¶ãŒã®ãããªæ©èœãåãããçŸãããŠãŒã¶ãŒãã¬ã³ããªãŒãªãã©ãŒã ãã³ãã³ãã©ã€ã³ ã¢ããªã±ãŒã·ã§ã³ã«è¿œå ããããã® PHP ããã±ãŒãžã§ãã
Laravel ããã³ããã¯ãã¯ããŒãžã£ãŒãä»ããå ¥åæ€èšŒããµããŒãããŠããŸãã
$name = text(
label: 'What is your name?',
validate: fn (string $value) => match (true) {
strlen($value) < 3 => 'The name must be at least 3 characters.',
strlen($value) > 255 => 'The name must not exceed 255 characters.',
default => null
}
);
ãã ããå€ãã®å ¥åãè€éãªæ€èšŒã·ããªãªãæ±ãå Žåãããã¯é¢åã«ãªãå¯èœæ§ããããŸãããããã£ãŠãLaravel 11 ã§ã¯ãããã³ããå ¥åãæ€èšŒãããšãã« Laravel ã®ããªããŒã¿ãŒã®æ©èœãæ倧éã«æŽ»çšã§ããŸãã
$name = text('What is your name?', validate: [
'name' => 'required|min:3|max:255',
]);
ãã¥ãŒçžäºäœçšã®ãã¹ã
以åã¯ããã¥ãŒã«å ¥ãããããžã§ãã解æŸãåé€ãããããæåã§å€±æãããããã¹ãããããšããã®ã¯é¢åã§ãã«ã¹ã¿ã ãã¥ãŒã®ãã§ã€ã¯ãšã¹ã¿ããå®çŸ©ããå¿ èŠããããŸããããã ããLaravel 11 ã§ã¯ã次ã®ã¡ãœããã䜿çšããŠãããã®ãã¥ãŒã®å¯Ÿè©±ãç°¡åã«ãã¹ãã§ããŸãwithFakeQueueInteractionsã
use App\Jobs\ProcessPodcast;
$job = (new ProcessPodcast)->withFakeQueueInteractions();
$job->handle();
$job->assertReleased(delay: 30);
ãã¥ãŒã«å ¥ãããããžã§ãã®ãã¹ãã®è©³çŽ°ã«ã€ããŠã¯ããã¥ãŒã®ããã¥ã¡ã³ããåç §ããŠãã ããã
æ°ããã¢ãŒãã£ã¶ã³ã³ãã³ã
æ°ããã¢ãŒãã£ã¶ã³ ã³ãã³ããè¿œå ãããã¯ã©ã¹ãã€ã³ã¿ãŒãã§ã€ã¹ãç¹æ§ãè¿ éã«äœæã§ããããã«ãªããŸããã
php artisan make:class
$ php artisan make:class Hoge
INFO Class [app/Hoge.php] created successfully.
<?php
namespace App;
class Hoge
{
/**
* Create a new class instance.
*/
public function __construct()
{
//
}
}
php artisan make:enum
$ php artisan make:enum Hoge
INFO Enum [app/Hoge.php] created successfully.
<?php
namespace App;
enum Hoge
{
//
}
php artisan make:interface
$ php artisan make:interface Hoge
INFO Interface [app/Hoge.php] created successfully.
<?php
namespace App;
interface Hoge
{
//
}
php artisan make:trait
$ php artisan make:trait Hoge
INFO Trait [app/Hoge.php] created successfully.
<?php
namespace App;
trait Hoge
{
//
}
ãŸãšã
ãããã§ããã?(ð€
3/7~3/9 ãºã¡ã±ãŒã®äŒè°ããããŸãããããããã°ãã²åå ããŠã»ããã®ã
ãªãŒã