承接 gquental/cakephp-service-container 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gquental/cakephp-service-container

Composer 安装命令:

composer require gquental/cakephp-service-container

包简介

Service Container for CakePHP

README 文档

README

This project was created to provide the feature of a container of services and also dependency injection in the CakePHP framework.

##Installation

In order to install it, there is two options, install it through composer or manually.

###composer

You just need to require the package gquental/cakephp-service-container in your composer.json and then run the install command. After this process is completed, a folder called ServiceContainer will be created inside your CakePHP plugins folder.

###manually

Just clone the project inside your CakePHP plugins folder and name the directory ServiceContainer

##Usage

###Configuration file

The first step after the Plugin is properly installed is to create the configuration file and then register it in the CakePHP config bootstrap file (app/Config/bootstrap.php).

For example, if you name the file services.php, you will need to write this line in your bootstrap file:

// 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');

####Configuration file example

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

###Using the ServiceContainer Component

In order to call the services inside the container, you will need to use the CakePHP component provided by the Plugin, in order to do so you will need to put the following entry in the components array.

$components = ['ServiceContainer.ServiceContainer'];

With the component prepared, you can call a service by calling the getService method.

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

###Dependency injection

Lets say that in your container we have two classes, the User and the Contact class. The contact class needs as a dependency the user instance and also a integer with the maximum number of phones that it should have.

We can pass all of this dependencies through the service container instead of instancing it manually in the Contact class. In order to do so we will to pass arguments to the Contact class in the services container configuration file.

In the arguments we can pass primitive values or another service already in the configuration file, only by passing a string with the name of the service preceded by a @, like this for the user class: "@User".

Below you will find the examples of how to create this situation

app/Config/services.php

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

someController.php

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

Lib/UserEntity.php

class UserEntity {}

Lib/ContactEntity.php

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

统计信息

  • 总下载量: 127
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-02-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固