【发布时间】:2015-09-22 10:37:17
【问题描述】:
我目前正在从事一个个人项目,并且我已经实现了服务层。我更喜欢独立存储服务,因此我在一个文件中没有大型库。下面是文件结构的简要示例
services/
user/
authentication
login
logout
registration
news/
articles
article
我认识一些实现userService 类的人,它将对我目前拥有的所有内容进行分组。我更喜欢我的方法来节省未来编辑的时间 + 我有很多用户服务/功能,所以最好把它分开。最近有人建议我在我的服务文件夹的根目录中实现一个userService 类,并使用它来调用/执行应用程序中所需的服务。下面是我的例子/理解
<?php
/**
*-----------------------------------------------------------------
*
* USER SERVICE CLASS
*
* Simplifies Service Usage Within Application
*
*/
namespace Service;
use \Helper\ServiceAccess;
class UserService extends ServiceAccess {
// Define Service Prefix Key
protected $prefix = 'user/';
}
///////////////////////////////////////////////////////////////////
// Separate File ( Helper Function )
///////////////////////////////////////////////////////////////////
/**
*-----------------------------------------------------------------
*
* SERVICE ACCESS LAYER
*
* Used to Simplify the Process of Executing Services, and Grouping
* Alerts For Simple Front-end Error Messages
*
*/
namespace Helper;
class ServiceAccess extends Base {
public $dependencies = ['factory'];
// Default Service Prefix
protected $prefix = '';
// Alert Container Used By Controller to Set UI Alerts
protected $alerts = [
'errors' => [],
'info' => [],
'success' => [],
'warning' => []
];
/**
* Service Execution Method
*
* Used Within Parent Service Classes Such as UserServices
* TournamentServices
* etc.
*
* @param string $key Refers to the Factory Key ( Excluding Prefix )
* @param mixed $input Any Type of Input to Be Passed to Execute Method of Child Service
*/
public function execute($key, $input = []) {
// Create Service Class Via Factory - Call Execute Method Within Service
$service = $this->factory->make($this->prefix . $key);
$execute = $service->execute($input);
// Get & Merge Alerts From Service
$this->setAlerts($service);
// Return Result From Service Execution
return $execute;
}
/**
* Set Alerts
*
* @param array $alerts Front-End User Alerts Defined By Services
*/
private function setAlerts($service) {
$this->alerts = [
'errors' => array_merge($this->alerts['errors'], (array) $service->get('errors')),
'info' => array_merge($this->alerts['info'], (array) $service->get('info')),
'success' => array_merge($this->alerts['success'], (array) $service->get('success')),
'warning' => array_merge($this->alerts['warning'], (array) $service->get('warning'))
];
}
}
控制器示例
<?php
/**
*-----------------------------------------------------------------
*
* LOGIN CONTROLLER
*
*/
namespace Controller\www;
use \Helper\Controller;
class Login extends Controller {
public $dependencies = ['arena', 'login', 'notification', 'site', 'userservice'];
/**
* Login
*
* Login Runs Through Various Checks Including If User is Banned, Account is Locked,
* or Forgot Password Request Is Active. Then the Entered Password is Matched & if Valid
* User is Logged In
*/
public function index() {
// User Already Logged In Redirect
$this->user->get('id') ? $this->redirect->home() : '';
/**
* User Login
*
* If Successful, Login User, Redirect Home
* Else Set Error Alerts
*/
if ($this->form->post('login')) {
// Define and Sanitize Post Data
$input = $this->input->get(['username', 'password']);
// Execute Login Service Layer - Define Flash Alerts
$login = $this->userservice->execute('login', $input);
$this->alerts($this->userservice->get('alerts'));
// Redirect Home on Successful Login
$login === true ? $this->redirect->home() : '';
}
}
}
ServiceAccess 类中的 execute 方法是我被建议做的其余部分我为我的用户错误处理添加的。我的问题如下
为什么这比直接在应用程序中调用服务更好?
它简化了我的控制器中的服务/警报设置的执行(在控制器中将大约 15 行代码变成了 4 行)但是我有类似user/transactions(处理用户帐户的贷记/借记)之类的服务,并且它们有单独的方法需要使用。所以我想知道是UserService 类还是我的事务类需要更新。我正在考虑在事务中定义一个执行方法,并在输入中传递一个键来定义正在使用的事务类型。
这是在我的应用程序中访问/实现服务的最佳途径吗?
【问题讨论】:
标签: php model-view-controller service architecture