PHP code example of gquental / cakephp-service-container

1. Go to this page and download the library: Download gquental/cakephp-service-container library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gquental / cakephp-service-container example snippets


// First parameter is the name of the file without the extension
// The second parameter is the name of the configuration reader,
// which in CakePHP the default is the PHP file reader
Configure::load('services', 'default');

$config = array(
	'Services' => array(
		'ServiceName' => array(
			'name' => 'name of the class',
			'path' => 'path of the class'
			'arguments' => [1, 2, 3]
		)
	)
);

$components = ['ServiceContainer.ServiceContainer'];

public function actionX() {
	$service = $this->ServiceContainer->getService('serviceName');
}

$config = array(
	'Services' => array(
		'User' => array(
			'name' => 'UserEntity',
			'path' => 'Lib'
		),
		'Contact' => array(
			'name' => 'ContactEntity',
			'path' => 'Lib',
			'arguments' => ['@User', 3]
		)
	)
);

public function action() {
	$contact = $this->ServiceContainer->get('Contact');
}

class UserEntity {}

class ContactEntity {
	public $user;
	public $maxPhones;
	
	public function __construct($user, $maxPhones) {
		$this->user = $user;
		$this->maxPhones = $maxPhones;
	}
}