動機
我想透過 DI 容器將 Service 注入到 Command 和 Controller。
另外,Service 使用 Repository 注入。
文件中並沒有提到嵌套注入這種情況。
文件
https://book.cakephp.org/4/en/development/dependency-injection.html
如何實施
服務和儲存庫
interface SomeRepository { public getAll(): array; }
class SomeRepositoryImpl implements SomeRepository { public function getAll(): array { print('getAll()'); return []; } }
class SomeService { private $someRepository; public function __construct(SomeRepository $someRepository) { $this->someRepository = $someRepository; } public function doSomething(): void { $this->someRepository->getAll(); } }
命令
use Cake\Command\Command; use Cake\Console\Arguments; use Cake\Console\ConsoleIo; use Cake\Console\ConsoleOptionParser; class SomeCommand extends Command { private SomeService $service; public function __construct(SomeService $service) { parent::__construct(); $this->service = $service; } public static function getDescription(): string { return 'Inject service through provider'; } public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser { $parser = parent::buildOptionParser($parser); return $parser; } public function execute(Arguments $args, ConsoleIo $io) { $this->service->doSomething(); } }
use Cake\Core\ContainerInterface; use Cake\Core\ServiceProvider; class CommandServiceProvider extends ServiceProvider { protected $provides = [ SomeCommand::class, ]; public function services(ContainerInterface $container): void { $container ->add(SomeCommand::class) ->addArgument(SomeService::class); } }
控制器
use Cake\Controller\Controller; class SomeController extends Controller { public function index(SomeService $service) { $service->doSomething(); print('index()'); } }
註冊到DI容器
use Cake\Core\ContainerInterface; use Cake\Core\ServiceProvider; class SomeServiceProvider extends ServiceProvider { protected $provides = [ SomeService::class, ]; public function services(ContainerInterface $container): void { $container ->add(SomeRepository::class, SomeRepositoryImpl::class); $container ->add(SomeService::class) ->addArgument(SomeRepository::class); } }
// Application.php class Application extends BaseApplication { // ... public function services(ContainerInterface $container): void { $container->addServiceProvider(new SomeServiceProvider()); $container->addServiceProvider(new ControllerServiceProvider()); } // ...
以上是CakePHP 上層的 DI 容器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

stickysessensureuserRequestSarerOutedTothesMeServerForsessionDataConsisterency.1)sessionIdentificeAssificationAssigeaSsignAssignSignSuserServerServerSustersusiseCookiesorUrlModifications.2)一致的ententRoutingDirectSsssssubsequeSssubsequeSubsequestrequestSameSameserver.3)loadBellankingDisteributesNebutesneNewuserEreNevuseRe.3)

phpoffersvarioussessionsionsavehandlers:1)文件:默認,簡單的ButMayBottLeneckonHigh-trafficsites.2)Memcached:高性能,Idealforsforspeed-Criticalapplications.3)REDIS:redis:similartomemememememcached,withddeddeddedpassistence.4)withddeddedpassistence.4)databases:gelifforcontrati forforcontrati,有用

PHP中的session是用於在服務器端保存用戶數據以在多個請求之間保持狀態的機制。具體來說,1)session通過session_start()函數啟動,並通過$_SESSION超級全局數組存儲和讀取數據;2)session數據默認存儲在服務器的臨時文件中,但可通過數據庫或內存存儲優化;3)使用session可以實現用戶登錄狀態跟踪和購物車管理等功能;4)需要注意session的安全傳輸和性能優化,以確保應用的安全性和效率。

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

絕對會話超時從會話創建時開始計時,閒置會話超時則從用戶無操作時開始計時。絕對會話超時適用於需要嚴格控制會話生命週期的場景,如金融應用;閒置會話超時適合希望用戶長時間保持會話活躍的應用,如社交媒體。

服務器會話失效可以通過以下步驟解決:1.檢查服務器配置,確保會話設置正確。 2.驗證客戶端cookies,確認瀏覽器支持並正確發送。 3.檢查會話存儲服務,如Redis,確保其正常運行。 4.審查應用代碼,確保會話邏輯正確。通過這些步驟,可以有效診斷和修復會話問題,提升用戶體驗。

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver Mac版
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Atom編輯器mac版下載
最受歡迎的的開源編輯器