{"id":93,"date":"2024-03-22T03:20:22","date_gmt":"2024-03-22T03:20:22","guid":{"rendered":"http:\/\/localhost\/w643\/?p=93"},"modified":"2024-03-25T07:36:54","modified_gmt":"2024-03-25T07:36:54","slug":"bai-5-service-container-phan-1","status":"publish","type":"post","link":"https:\/\/blog.hnk90.com\/?p=93","title":{"rendered":"B\u00e0i 5. Service Container &#8211; Ph\u1ea7n 1 &#8211; Introduction"},"content":{"rendered":"\n<p>Ki\u1ebfn th\u1ee9c ph\u1ea7n n\u00e0y s\u1ebd h\u01a1i d\u00e0i v\u00e0 kh\u00f3 hi\u1ec3u cho ng\u01b0\u1eddi m\u1edbi b\u1eaft \u0111\u1ea7u. <br>C\u00e1c b\u1ea1n c\u00f3 th\u1ec3 tham kh\u1ea3o t\u1ea1i \u0111\u00e2y: https:\/\/laravel.com\/docs\/11.x\/container#binding<br>Tuy nhi\u00ean n\u00f3 s\u1ebd h\u01a1i kh\u00f3 hi\u1ec3u cho ng\u01b0\u1eddi m\u1edbi b\u1eaft \u0111\u1ea7u. M\u00ecnh s\u1ebd ch\u1eaft l\u1ecdc v\u00e0 gi\u1ea3i th\u00edch m\u1ed9t c\u00e1ch &#8220;d\u00e2n gi\u00e3&#8221;, d\u1ec5 hi\u1ec3u nh\u1ea5t cho c\u00e1c b\u1ea1n. H\u00e3y chu\u1ea9n b\u1ecb m\u1ed9t t\u00e1ch coffee v\u00e0 m\u1ed9t tinh th\u1ea7n tho\u1ea3i m\u00e1i tr\u01b0\u1edbc khi b\u1eaft \u0111\u1ea7u nh\u00e9.<\/p>\n\n\n\n<p><strong>1. L\u00fd do kh\u1edfi \u0111\u1ea7u<\/strong><\/p>\n\n\n\n<p>G\u1ec9a s\u1eed c\u00e1c b\u1ea1n c\u00f3 2 classes l\u00e0:  UserController v\u00e0 UserInfo<br>trong \u0111\u00f3 <br>UserController: \u0111\u01a1n gi\u1ea3n nhi\u1ec7m v\u1ee5 l\u00e0 tr\u1ea3 v\u1ec1 th\u00f4ng tin ng\u01b0\u1eddi d\u00f9ng<br>UserInfo: l\u00e0m nhi\u1ec7m truy xu\u1ea5t th\u00f4ng tin ng\u01b0\u1eddi d\u00f9ng<br>C\u00f3 ngh\u0129a l\u00e0 UserController mu\u1ed1n tr\u1ea3 v\u1ec1 th\u00f4ng tin ng\u01b0\u1eddi d\u00f9ng th\u00ec ph\u1ea3i th\u00f4ng qua UserInfo<\/p>\n\n\n\n<p>V\u1edbi PHP \u0111\u01a1n thu\u1ea7n, c\u00e1c b\u1ea1n s\u1ebd l\u00e0m nh\u01b0 th\u1ebf n\u00e0y <\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"$userInfo = new UserInfo() ;\n\/* Ti\u1ebfp theo UserController s\u1ebd d\u1ef1a v\u00e0o th\u00f4ng tin l\u1ea5y \u0111\u01b0\u1ee3c b\u00ean tr\u00ean \u0111\u1ec3 x\u1eed l\u00fd ti\u1ebfp \nv\u00e0 tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3. *\/\n$userInformation = new UserController($userInfo);\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9\">$userInfo<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">UserInfo<\/span><span style=\"color: #D8DEE9FF\">() <\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/* Ti\u1ebfp theo UserController s\u1ebd d\u1ef1a v\u00e0o th\u00f4ng tin l\u1ea5y \u0111\u01b0\u1ee3c b\u00ean tr\u00ean \u0111\u1ec3 x\u1eed l\u00fd ti\u1ebfp <\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">v\u00e0 tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3. *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">$userInformation<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">UserController<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">$userInfo<\/span><span style=\"color: #D8DEE9FF\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Chuy\u1ec7n g\u00ec s\u1ebd x\u1ea3y ra n\u1ebfu ch\u00fang ta c\u00f3 th\u00eam 2 classes: <br>UserAddress &#8211; l\u00e0m nhi\u1ec7m v\u1ee5 cung c\u1ea5p th\u00f4ng tin c\u1ee7a user. <br>UserAccount &#8211; l\u00e0m nhi\u1ec7m v\u1ee5 tr\u1ea3 ra c\u00e1c th\u00f4ng tin chung nh\u01b0: t\u00ean, tu\u1ed5i, s\u1ed1 \u0111i\u1ec7n tho\u1ea1i,&#8230;<br>V\u1ec1 m\u1eb7t coding ch\u00fang ta s\u1ebd ph\u1ea3i l\u00e0m nh\u01b0 th\u1ebf n\u00e0y<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ kh\u1edfi t\u1ea1o v\u00e0 truy xu\u1ea5t th\u00f4ng tin user address.\n$userAddress = new UserAddress();\n\/\/ kh\u1edfi t\u1ea1o v\u00e0 truy xu\u1ea5t th\u00f4ng tin user account.\n$userAccount= new UserAccount();\n\/\/ UserRepository c\u1ea7n address \u0111\u1ec3 cung c\u1ea5p \u0111\u1ea7y \u0111\u1ee7 th\u00f4ng tin user.\n$userInfo = new UserInfo($userAddress, $userAccount);\n\/\/ cu\u1ed1i c\u00f9ng r\u1ed3i \u0111\u1ebfn UserController\n$userController = new UserController($userInfo);\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\">\/\/ kh\u1edfi t\u1ea1o v\u00e0 truy xu\u1ea5t th\u00f4ng tin user address.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">$userAddress<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">UserAddress<\/span><span style=\"color: #D8DEE9FF\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ kh\u1edfi t\u1ea1o v\u00e0 truy xu\u1ea5t th\u00f4ng tin user account.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">$userAccount<\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">UserAccount<\/span><span style=\"color: #D8DEE9FF\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ UserRepository c\u1ea7n address \u0111\u1ec3 cung c\u1ea5p \u0111\u1ea7y \u0111\u1ee7 th\u00f4ng tin user.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">$userInfo<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">UserInfo<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">$userAddress<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">$userAccount<\/span><span style=\"color: #D8DEE9FF\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ cu\u1ed1i c\u00f9ng r\u1ed3i \u0111\u1ebfn UserController<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">$userController<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">UserController<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">$userInfo<\/span><span style=\"color: #D8DEE9FF\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u1ede \u0111\u00e2y ch\u00fang ta nh\u00ecn th\u1ea5y s\u1ef1 ph\u1ee5 thu\u1ed9c l\u1eabn nhau \u1edf m\u1ee9c \u0111\u01a1n gi\u1ea3n. Nh\u01b0ng h\u00e3y t\u01b0\u1edfng t\u01b0\u1ee3ng trong m\u1ed9t d\u1ef1 \u00e1n th\u00ec s\u1ef1 ph\u1ee5 thu\u1ed9c s\u1ebd r\u1ea5t nhi\u1ec1u, t\u1ea1o ra s\u1ef1 c\u1ed3ng k\u1ec1nh v\u00e0 ph\u1ee9c t\u1ea1p trong qu\u00e1 tr\u00ecnh ph\u00e1t tri\u1ec3n. <br>V\u00ed d\u1ee5: Sau n\u00e0y n\u1ebfu mu\u1ed1n s\u1eed d\u1ee5ng UserInfo \u1edf m\u1ed9t ch\u1ed7 kh\u00e1c (Ch\u1eb3ng h\u1ea1n nh\u01b0 OrderController &#8211; l\u00e0m nhi\u1ec7m v\u1ee5 tr\u1ea3 ra th\u00f4ng tin v\u1ec1 \u0111\u01a1n h\u00e0ng), c\u00e1c b\u1ea1n s\u1ebd l\u1ea1i ph\u1ea3i kh\u00e1i b\u00e1o nh\u01b0 tr\u00ean m\u1ed9t l\u1ea7n n\u1eefa. Ho\u1eb7c UserAccount l\u1ea1i c\u00f3 th\u00eam s\u1ef1 ph\u1ee5 thu\u1ed9c kh\u00e1c (Ch\u1eb3ng h\u1ea1n nh\u01b0 ph\u1ee5 thu\u1ed9c v\u00e0o UserBankInfo &#8211; l\u00e0m nhi\u1ec7m v\u1ee5 cung c\u1ea5p th\u00f4ng tin thanh to\u00e1n c\u1ee7a user), c\u00e1c b\u1ea1n l\u1ea1i ph\u1ea3i khai b\u00e1o th\u00eam s\u1ef1 ph\u1ee5 thu\u1ed9c, update l\u1ea1i code \u1edf nh\u1eefng n\u01a1i c\u00e1c b\u1ea1n \u0111\u00e3 t\u1eebng khai b\u00e1o,&#8230; N\u00f3i chung r\u1ea5t r\u01b0\u1eddm r\u00e0 t\u1ed1n c\u00f4ng.<\/p>\n\n\n\n<p>V\u1eady l\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 gi\u1ea3i quy\u1ebft v\u1ea5n \u0111\u1ec1 n\u00e0y ?<\/p>\n\n\n\n<p><strong>2. Inversion Of Control<\/strong> <strong>Container<\/strong> (IoC container)<\/p>\n\n\n\n<p>D\u1ecbch ngh\u0129a l\u00e0 &#8220;\u0110\u1ea3o ng\u01b0\u1ee3c s\u1ef1 ki\u1ec3m so\u00e1t&#8221; =)). C\u01a1 b\u1ea3n hi\u1ec3u nh\u01b0 sau<\/p>\n\n\n\n<p>V\u1edbi v\u00ed d\u1ee5 truy\u1ec1n th\u1ed1ng \u1edf tr\u00ean, mu\u1ed1n UserController tr\u1ea3 v\u1ec1 th\u00f4ng tin user th\u00ec b\u1ea1n ph\u1ea3i khai b\u00e1o UserInfo tr\u01b0\u1edbc UserController. T\u01b0\u01a1ng t\u1ef1, mu\u1ed1n c\u00f3 c\u00f3 th\u00f4ng tin v\u1ec1 UserAccount th\u00ec c\u00e1c b\u1ea1n l\u1ea1i ph\u1ea3i khai b\u00e1o UserAccount tr\u01b0\u1edbc UserInfo&#8230;<\/p>\n\n\n\n<p>Ph\u01b0\u01a1ng ph\u00e1p &#8220;Inversion Of Control&#8221; s\u1ebd &#8220;\u0111\u1ea3o ng\u01b0\u1ee3c&#8221; \u0111i\u1ec1u n\u00e0y.<br>Hi\u1ec3u nh\u01b0 sau:<br>IoC Container s\u1ebd &#8220;\u0111\u0103ng k\u00fd&#8221; s\u1ef1 ph\u1ee5 thu\u1ed9c tr\u01b0\u1edbc<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ Tr\u01b0\u1edbc h\u1ebft \u0111\u1eebng quan t\u00e2m t\u1edbi \u0111o\u1ea1n m\u00e3 d\u01b0\u1edbi \u0111\u00e2y, n\u00f3 ch\u1ec9 l\u00e0 \u0111o\u1ea1n m\u00e3 gi\u1ea3 \u0111\u1ec3 minh h\u1ecda\n\/\/ \u0110\u0103ng k\u00fd dependences v\u1edbi IoC Container\nIoc::register('userInfo', function(){\n    $account = new UserAccount();\n    $address = new UserAddress();\n    return new UserInfo($account, $address);\n});\n\/\/ Sau khi \u0111\u0103ng k\u00fd UserInfo v\u1edbi Ioc Container r\u1ed3i. Ch\u00fang ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng UserInfo \u1edf b\u1ea5t k\u1ef3 \u0111\u00e2u ch\u00fang ta mu\u1ed1n m\u00e0 kh\u00f4ng quan t\u00e2m UserInfo \u0111ang c\u1ea7n nh\u1eefng ph\u1ee5 thu\u1ed9c n\u00e0o \n\/\/ Gi\u1edd mu\u1ed1n s\u1eed d\u1ee5ng cho UserController th\u00ec \u0111\u01a1n gi\u1ea3n l\u00e0 g\u1ecdi UserInfo ra v\u00e0 x\u00e0i\n$userInfo = IoC::resolve('userInfo');\n$userController = new UserController($userInfo)\n\/\/ Ho\u1eb7c s\u1eed d\u1ee5ng cho OrderController\n$userInfo = IoC::resolve('userInfo');\n$orderControlelr = new OrderController($userInfo)\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\">\/\/ Tr\u01b0\u1edbc h\u1ebft \u0111\u1eebng quan t\u00e2m t\u1edbi \u0111o\u1ea1n m\u00e3 d\u01b0\u1edbi \u0111\u00e2y, n\u00f3 ch\u1ec9 l\u00e0 \u0111o\u1ea1n m\u00e3 gi\u1ea3 \u0111\u1ec3 minh h\u1ecda<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ \u0110\u0103ng k\u00fd dependences v\u1edbi IoC Container<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">Ioc<\/span><span style=\"color: #ECEFF4\">:<\/span><span style=\"color: #D8DEE9FF\">:<\/span><span style=\"color: #88C0D0\">register<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">userInfo<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">function<\/span><span style=\"color: #ECEFF4\">(){<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #D8DEE9\">$account<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">UserAccount<\/span><span style=\"color: #D8DEE9FF\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #D8DEE9\">$address<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">UserAddress<\/span><span style=\"color: #D8DEE9FF\">()<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">UserInfo<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">$account<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">$address<\/span><span style=\"color: #D8DEE9FF\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">}<\/span><span style=\"color: #D8DEE9FF\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ Sau khi \u0111\u0103ng k\u00fd UserInfo v\u1edbi Ioc Container r\u1ed3i. Ch\u00fang ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng UserInfo \u1edf b\u1ea5t k\u1ef3 \u0111\u00e2u ch\u00fang ta mu\u1ed1n m\u00e0 kh\u00f4ng quan t\u00e2m UserInfo \u0111ang c\u1ea7n nh\u1eefng ph\u1ee5 thu\u1ed9c n\u00e0o <\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ Gi\u1edd mu\u1ed1n s\u1eed d\u1ee5ng cho UserController th\u00ec \u0111\u01a1n gi\u1ea3n l\u00e0 g\u1ecdi UserInfo ra v\u00e0 x\u00e0i<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">$userInfo<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> IoC<\/span><span style=\"color: #ECEFF4\">:<\/span><span style=\"color: #D8DEE9FF\">:<\/span><span style=\"color: #88C0D0\">resolve<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">userInfo<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #D8DEE9FF\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">$userController<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">UserController<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">$userInfo<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ Ho\u1eb7c s\u1eed d\u1ee5ng cho OrderController<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">$userInfo<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> IoC<\/span><span style=\"color: #ECEFF4\">:<\/span><span style=\"color: #D8DEE9FF\">:<\/span><span style=\"color: #88C0D0\">resolve<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">userInfo<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #D8DEE9FF\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">$orderControlelr<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">OrderController<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">$userInfo<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Nh\u01b0 v\u1eady, v\u1edbi vi\u1ec7c ch\u1ec9 c\u1ea7n \u0111\u0103ng k\u00fd m\u1ed9t l\u1ea7n, c\u00e1c b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00e1c class m\u00e0 kh\u00f4ng c\u1ea7n quan t\u00e2m t\u1edbi s\u1ef1 ph\u1ee5 thu\u1ed9c c\u1ee7a n\u00f3 n\u1eefa. R\u1ea5t ti\u1ec7n ph\u1ea3i kh\u00f4ng<\/p>\n\n\n\n<p>Trong laravel, t\u01b0 t\u01b0\u1edfng gi\u1ea3i quy\u1ebft b\u00e0i to\u00e1n c\u0169ng t\u01b0\u01a1ng t\u1ef1 nh\u01b0 tr\u00ean, h\u1ecd g\u1ecdi n\u00f3 l\u00e0 Service Container. Trong b\u00e0i sau ch\u00fang ta s\u1ebd xem laravel l\u00e0m nh\u01b0 n\u00e0o nh\u00e9. <\/p>\n\n\n\n<p>Hay ngh\u1ec9 ng\u01a1i v\u00e0 chu\u1ea9n b\u1ecb xem b\u00e0i ti\u1ebfp nh\u00e9 ! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ki\u1ebfn th\u1ee9c ph\u1ea7n n\u00e0y s\u1ebd h\u01a1i d\u00e0i v\u00e0 kh\u00f3 hi\u1ec3u cho ng\u01b0\u1eddi m\u1edbi b\u1eaft \u0111\u1ea7u. C\u00e1c b\u1ea1n c\u00f3 th\u1ec3 tham kh\u1ea3o t\u1ea1i \u0111\u00e2y: https:\/\/laravel.com\/docs\/11.x\/container#bindingTuy nhi\u00ean n\u00f3 s\u1ebd h\u01a1i kh\u00f3 hi\u1ec3u cho ng\u01b0\u1eddi m\u1edbi b\u1eaft \u0111\u1ea7u. M\u00ecnh s\u1ebd ch\u1eaft l\u1ecdc v\u00e0 gi\u1ea3i th\u00edch m\u1ed9t c\u00e1ch &#8220;d\u00e2n gi\u00e3&#8221;, d\u1ec5 hi\u1ec3u nh\u1ea5t cho c\u00e1c b\u1ea1n. H\u00e3y chu\u1ea9n b\u1ecb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry","category-laravel"],"_links":{"self":[{"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=93"}],"version-history":[{"count":5,"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":155,"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=\/wp\/v2\/posts\/93\/revisions\/155"}],"wp:attachment":[{"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}