{"id":1047,"date":"2024-01-27T23:59:02","date_gmt":"2024-01-27T14:59:02","guid":{"rendered":"https:\/\/shinke1987.net\/?p=1047"},"modified":"2024-01-27T23:59:03","modified_gmt":"2024-01-27T14:59:03","slug":"laravel%e3%81%a7%e3%81%ae%e3%82%b5%e3%83%bc%e3%83%93%e3%82%b9%e3%82%b3%e3%83%b3%e3%83%86%e3%83%8a%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"https:\/\/shinke1987.net\/?p=1047","title":{"rendered":"Laravel\u3067\u306e\u30b5\u30fc\u30d3\u30b9\u30b3\u30f3\u30c6\u30ca\u306b\u3064\u3044\u3066"},"content":{"rendered":"\n<h2 id=\"toc0\" class=\"wp-block-heading\">\u30b5\u30fc\u30d3\u30b9\u30b3\u30f3\u30c6\u30ca\u306b\u3064\u3044\u3066<\/h2>\n\n\n\n<p>\u5f79\u5272\u306fLaravel\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u5185\u3067\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u7ba1\u7406\u3002<\/p>\n\n\n\n<p>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u751f\u6210\u65b9\u6cd5\u3092\u767b\u9332\u3059\u308b\u51e6\u7406\u3092\u30d0\u30a4\u30f3\u30c9\u3068\u547c\u3076\u3002<\/p>\n\n\n\n<p>\u6307\u5b9a\u3055\u308c\u305f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u3057\u3066\u8fd4\u3059\u3053\u3068\u3092\u89e3\u6c7a\u3059\u308b\u3068\u547c\u3076\u3002<\/p>\n\n\n\n<h2 id=\"toc1\" class=\"wp-block-heading\">\u30d0\u30a4\u30f3\u30c9\u306e\u5b9a\u7fa9\u5834\u6240<\/h2>\n\n\n\n<p>app\\Providers\u30d5\u30a9\u30eb\u30c0\u306bServiceProvider\u306e\u5b50\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u3066\u5b9a\u7fa9\u3057\u3066\u3082\u826f\u3044\u3057\u3001<br>\u65e2\u306b\u7528\u610f\u3055\u308c\u3066\u3044\u308bAppServiceProvider\u30af\u30e9\u30b9\u306b\u5b9a\u7fa9\u3057\u3066\u3082\u826f\u3044\u3002<\/p>\n\n\n\n<p>register\u30e1\u30bd\u30c3\u30c9\u3068boot\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308a\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u8d77\u52d5\u51e6\u7406\u4e2d\u306b\u5b9f\u884c\u3055\u308c\u308b\u3002<\/p>\n\n\n\n<p>\u901a\u5e38\u3001\u30d0\u30a4\u30f3\u30c9\u51e6\u7406\u306fregister\u30e1\u30bd\u30c3\u30c9\u306b\u5b9a\u7fa9\u3055\u308c\u308b\u304c\u3001<br>\u4ed6\u306e\u30af\u30e9\u30b9\u3092\u5229\u7528\u3059\u308b\u30af\u30e9\u30b9\u3092\u30d0\u30a4\u30f3\u30c9\u3059\u308b\u6642\u306fboot\u30e1\u30bd\u30c3\u30c9\u3067\u5b9a\u7fa9\u3059\u308b\u306e\u304c\u826f\u3044\u3002<br>\u305d\u306e\u7406\u7531\u306fboot\u30e1\u30bd\u30c3\u30c9\u304c\u5b9f\u884c\u3055\u308c\u308b\u6642\u306f\u3001\u4ed6\u306e\u6a5f\u80fd\u306e\u30d0\u30a4\u30f3\u30c9\u51e6\u7406\u3082\u7d42\u4e86\u3057\u3066\u3044\u308b\u305f\u3081\u3002<\/p>\n\n\n\n<h2 id=\"toc2\" class=\"wp-block-heading\">\u30b3\u30f3\u30c6\u30ca\u306e\u30d0\u30a4\u30f3\u30c9\u3068\u89e3\u6c7a\u306e\u4f8b<\/h2>\n\n\n\n<h3 id=\"toc3\" class=\"wp-block-heading\">make\u30e1\u30bd\u30c3\u30c9\u5b9f\u884c\u306e\u5ea6\u306b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u751f\u6210<\/h3>\n\n\n\n<p>\u4e0b\u8a18\u30b3\u30fc\u30c9\u3092Tinker\u3067\u5b9f\u884c\u3059\u308b\u3068\u826f\u3044\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nuse Illuminate\\Foundation\\Application;\n\nclass Number {\n    protected $number;\n\n    public function __construct($number = 0) {\n        $this-&gt;number = $number;\n    }\n\n    public function getNumber() {\n        return $this-&gt;number;\n    }\n}\n\n\/\/ \u7121\u540d\u95a2\u6570\u306e\u5f15\u6570\u306f\u914d\u5217\u3067\u3042\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002\u914d\u5217\u4ee5\u5916\u3060\u3068\u30a8\u30e9\u30fc\u306b\u306a\u308b\u3002\n\/\/ bindIf\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u3068\u3001\u65e2\u306b\u540c\u3058\u540d\u524d\u3067bind\u3055\u308c\u3066\u3044\u305f\u5834\u5408\u306b\u306f\u7121\u52b9\u3068\u306a\u308b\u3002\napp()-&gt;bind(Number::class, function(Application $app, array $args_number) {\n    return new Number($args_number&#x5B;0]);\n});\n\n$number = app()-&gt;make(Number::class, &#x5B;100]);\n$number-&gt;getNumber();\n<\/pre><\/div>\n\n\n<h3 id=\"toc4\" class=\"wp-block-heading\">make\u30e1\u30bd\u30c3\u30c9\u3092\u4f55\u5ea6\u5b9f\u884c\u3057\u3066\u3082\u540c\u4e00\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u8fd4\u3055\u308c\u308b<\/h3>\n\n\n\n<p>\u4e0b\u8a18\u30b3\u30fc\u30c9\u3092Tinker\u3067\u5b9f\u884c\u3059\u308b\u3068\u826f\u3044\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nuse Illuminate\\Foundation\\Application;\n\nclass Number {\n    protected $number;\n\n    public function __construct() {\n        $this-&gt;number = mt_rand(0, 100);\n    }\n\n    public function getNumber() {\n        return $this-&gt;number;\n    }\n}\n\napp()-&gt;singleton(Number::class, function(Application $app) {\n    return new Number();\n});\n\n$number = app()-&gt;make(Number::class);\n$number2 = app(Number::class);\n$number3 = app()-&gt;make(Number::class);\n$number-&gt;getNumber();\n$number2-&gt;getNumber();\n$number3-&gt;getNumber();\n<\/pre><\/div>\n\n\n<h3 id=\"toc5\" class=\"wp-block-heading\">\u65e2\u306b\u751f\u6210\u3057\u305f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u8fd4\u3059<\/h3>\n\n\n\n<p>\u4e0b\u8a18\u30b3\u30fc\u30c9\u3092Tinker\u3067\u5b9f\u884c\u3059\u308b\u3068\u826f\u3044\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nclass Number {\n    protected $number;\n\n    public function __construct() {\n        $this-&gt;number = mt_rand(0, 100);\n    }\n\n    public function getNumber() {\n        return $this-&gt;number;\n    }\n}\n\n$number = new Number();\n$number-&gt;getNumber();\n\napp()-&gt;instance(Number::class, $number);\n\n$number2 = app()-&gt;make(Number::class);\n$number3 = app(Number::class);\n$number2-&gt;getNumber();\n$number3-&gt;getNumber();\n<\/pre><\/div>\n\n\n<h3 id=\"toc6\" class=\"wp-block-heading\">\u30d0\u30a4\u30f3\u30c9\u3055\u308c\u3066\u3044\u306a\u3044\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210<\/h3>\n\n\n\n<p>\u4e0b\u8a18\u30b3\u30fc\u30c9\u3092Tinker\u3067\u5b9f\u884c\u3059\u308b\u3068\u826f\u3044\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nclass Number {\n    protected $number;\n\n    public function __construct() {\n        $this-&gt;number = mt_rand(0, 100);\n    }\n\n    public function getNumber() {\n        return $this-&gt;number;\n    }\n}\n\n$number = app()-&gt;make(Number::class);\n$number-&gt;getNumber();\n<\/pre><\/div>\n\n\n<h2 id=\"toc7\" class=\"wp-block-heading\">\u4f9d\u5b58\u6027\u306e\u6ce8\u5165(Dependency Injection) (\u30d0\u30a4\u30f3\u30c9\u306e\u524a\u9664\u3084\u78ba\u8a8d\u65b9\u6cd5\u3042\u308a)<\/h2>\n\n\n\n<h3 id=\"toc8\" class=\"wp-block-heading\">\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3<\/h3>\n\n\n\n<p>\u4e0b\u8a18\u30b3\u30fc\u30c9\u3092Tinker\u3067\u5b9f\u884c\u3059\u308b\u3068\u826f\u3044\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\ninterface NotifierInterface {\n    public function send(string $to, string $message): void;\n}\n\n\nclass MailSender implements NotifierInterface {\n    public function send(string $to, string $message): void {\n        \/\/ \u30e1\u30fc\u30eb\u9001\u4fe1\u51e6\u7406        \n        echo &#039;\u30e1\u30fc\u30eb\u9001\u4fe1\uff1a&#039; . $to . &#039; ( &#039; . $message . &#039; )&#039;;\n    }\n}\n\n\nclass PushSender implements NotifierInterface {\n    public function send(string $to, string $message): void {\n        \/\/ \u30d7\u30c3\u30b7\u30e5\u901a\u77e5\u51e6\u7406\n        echo &#039;\u30d7\u30c3\u30b7\u30e5\u901a\u77e5\uff1a&#039; . $to . &#039; ( &#039; . $message . &#039; )&#039;;\n    }\n}\n\n\nclass UserService {\n    protected $notifier;\n\n    public function __construct(NotifierInterface $notifier) {\n        $this-&gt;notifier = $notifier;\n    }\n\n    public function sendNotification(string $to, string $message): void {\n        $this-&gt;notifier-&gt;send($to, $message);\n    }\n}\n\n\napp()-&gt;bind(NotifierInterface::class, function() {\n    return new MailSender();\n});\n\n$user = app()-&gt;make(UserService::class);\n$user-&gt;sendNotification(&#039;to&#039;, &#039;message&#039;);        \/\/ \u30e1\u30fc\u30eb\u9001\u4fe1\uff1ato ( message ) \u3068\u8868\u793a\u3055\u308c\u308b\u3002\n\n\napp()-&gt;bind(NotifierInterface::class, function() {\n    return new PushSender();\n});\n\n$user = app()-&gt;make(UserService::class);\n$user-&gt;sendNotification(&#039;to&#039;, &#039;message&#039;);        \/\/ \u30d7\u30c3\u30b7\u30e5\u901a\u77e5\uff1ato ( message ) \u3068\u8868\u793a\u3055\u308c\u308b\u3002\n\n\n\/\/ \u30d0\u30a4\u30f3\u30c9\u3092\u524a\u9664\u3002\nApp::offsetUnset(NotifierInterface::class);     \/\/ \u307e\u305f\u306f app()-&gt;offsetUnset(NotifierInterface::class); \u3067\u3082\u826f\u3044\u3002\n$user = app()-&gt;make(UserService::class);        \/\/ \u6b21\u306e\u30a8\u30e9\u30fc\u304c\u8868\u793a\u3055\u308c\u308b\u3002\n\/\/ Illuminate\\Contracts\\Container\\BindingResolutionException  Target &#x5B;NotifierInterface] is not instantiable while building &#x5B;UserService].\n\n\n\/\/ \u30d0\u30a4\u30f3\u30c9\u306e\u5b58\u5728\u3092\u78ba\u8a8d\napp()&#x5B;NotifierInterface::class];\n\/\/ \u30d0\u30a4\u30f3\u30c9\u306e\u5b58\u5728\u3092\u78ba\u8a8d2\napp()-&gt;getBindings()&#x5B;NotifierInterface::class];\n<\/pre><\/div>\n\n\n<p>\u203b \u62bd\u8c61\u30af\u30e9\u30b9\uff08\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u7b49\uff09\u306b\u4f9d\u5b58\u3055\u305b\u308b\u3068\u67d4\u8edf\u306b\u5bfe\u5fdc\u3067\u304d\u308b\u3053\u3068\u304c\u591a\u3044\u3089\u3057\u3044\u3002<\/p>\n\n\n\n<h3 id=\"toc9\" class=\"wp-block-heading\">\u30e1\u30bd\u30c3\u30c9\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3<\/h3>\n\n\n\n<p>\u4e0b\u8a18\u30b3\u30fc\u30c9\u3092Tinker\u3067\u5b9f\u884c\u3059\u308b\u3068\u826f\u3044\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\ninterface NotifierInterface {\n    public function send(string $to, string $message): void;\n}\n\n\nclass MailSender implements NotifierInterface {\n    public function send(string $to, string $message): void {\n        \/\/ \u30e1\u30fc\u30eb\u9001\u4fe1\u51e6\u7406        \n        echo &#039;\u30e1\u30fc\u30eb\u9001\u4fe1\uff1a&#039; . $to . &#039; ( &#039; . $message . &#039; )&#039;;\n    }\n}\n\n\nclass UserService {\n    protected $notifier;\n\n    public function __construct(NotifierInterface $notifier) {\n        $this-&gt;notifier = $notifier;\n    }\n\n    public function sendNotification(string $to, string $message): void {\n        $this-&gt;notifier-&gt;send($to, $message);\n    }\n}\n\n\napp()-&gt;bind(NotifierInterface::class, function() {\n    return new MailSender();\n});\n\n\n$service = app(UserService::class);\n$to = &#039;address&#039;;\n$message = &#039;message2&#039;;\napp()-&gt;call(&#x5B;$service, &#039;sendNotification&#039;], &#x5B;&#039;to&#039; =&gt; $to, &#039;message&#039; =&gt; $message]);\n\/\/ \u30e1\u30fc\u30eb\u9001\u4fe1\uff1aaddress ( message2 ) \u3068\u8868\u793a\u3055\u308c\u308b\u3002\n<\/pre><\/div>\n\n\n<h3 id=\"toc10\" class=\"wp-block-heading\">\u30af\u30e9\u30b9\u540d\u306b\u5fdc\u3058\u3066\u6ce8\u5165\u3059\u308b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u5909\u3048\u308b<\/h3>\n\n\n\n<p>\u4e0b\u8a18\u30b3\u30fc\u30c9\u3092Tinker\u3067\u5b9f\u884c\u3059\u308b\u3068\u826f\u3044\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\ninterface NotifierInterface {\n    public function send(string $to, string $message): void;\n}\n\n\nclass MailSender implements NotifierInterface {\n    public function send(string $to, string $message): void {\n        \/\/ \u30e1\u30fc\u30eb\u9001\u4fe1\u51e6\u7406        \n        echo &#039;\u30e1\u30fc\u30eb\u9001\u4fe1\uff1a&#039; . $to . &#039; ( &#039; . $message . &#039; )&#039;;\n    }\n}\n\n\nclass PushSender implements NotifierInterface {\n    public function send(string $to, string $message): void {\n        \/\/ \u30d7\u30c3\u30b7\u30e5\u901a\u77e5\u51e6\u7406\n        echo &#039;\u30d7\u30c3\u30b7\u30e5\u901a\u77e5\uff1a&#039; . $to . &#039; ( &#039; . $message . &#039; )&#039;;\n    }\n}\n\n\nclass UserService {\n    protected $notifier;\n\n    public function __construct(NotifierInterface $notifier) {\n        $this-&gt;notifier = $notifier;\n    }\n\n    public function sendNotification(string $to, string $message): void {\n        $this-&gt;notifier-&gt;send($to, $message);\n    }\n}\n\n\nclass AdminService {    \n    protected $notifier;\n\n    public function __construct(NotifierInterface $notifier) {\n        $this-&gt;notifier = $notifier;\n    }\n\n    public function sendNotification(string $to, string $message): void {\n        $this-&gt;notifier-&gt;send($to, $message);\n    }\n}\n\n\/\/ bind\u30e1\u30bd\u30c3\u30c9\u306e\u4ee3\u308f\u308a\u306b\u5b9f\u884c\u3059\u308b\u3068\u8003\u3048\u3066\u826f\u3044\u3002\napp()-&gt;when(UserService::class)\n    -&gt;needs(NotifierInterface::class)\n    -&gt;give(MailSender::class);\n\napp()-&gt;when(AdminService::class)\n    -&gt;needs(NotifierInterface::class)\n    -&gt;give(PushSender::class);\n\napp(UserService::class)-&gt;sendNotification(&#039;to&#039;, &#039;message&#039;);    \/\/ \u30e1\u30fc\u30eb\u9001\u4fe1\uff1ato ( message )\napp(AdminService::class)-&gt;sendNotification(&#039;to&#039;, &#039;message&#039;);   \/\/ \u30d7\u30c3\u30b7\u30e5\u901a\u77e5\uff1ato ( message )\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u30b5\u30fc\u30d3\u30b9\u30b3\u30f3\u30c6\u30ca\u306b\u3064\u3044\u3066 \u5f79\u5272\u306fLaravel\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u5185\u3067\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u7ba1\u7406\u3002 \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u751f\u6210\u65b9\u6cd5\u3092\u767b\u9332\u3059\u308b\u51e6\u7406\u3092\u30d0\u30a4\u30f3\u30c9\u3068\u547c\u3076\u3002 \u6307\u5b9a\u3055\u308c\u305f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u3057\u3066\u8fd4\u3059\u3053\u3068\u3092\u89e3\u6c7a\u3059\u308b\u3068\u547c\u3076\u3002 \u30d0\u30a4\u30f3\u30c9\u306e\u5b9a\u7fa9\u5834\u6240 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[96],"tags":[88,81],"class_list":["post-1047","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-laravel","tag-php"],"_links":{"self":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1047"}],"version-history":[{"count":1,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1047\/revisions"}],"predecessor-version":[{"id":1048,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1047\/revisions\/1048"}],"wp:attachment":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}