我目前正在開發一個個人項目,並實施了服務層。我更喜歡獨立存儲服務,所以我沒有一個文件內的大型圖書館。下面是否需要通過創建一個「容器」來抽象服務
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
類或我的事務類需要更新。我正在考慮在事務中定義一個執行方法,只是在輸入中傳遞一個密鑰來定義正在使用的事務的類型。
這是在我的應用程序中訪問/實現服務的最佳路線嗎?
謝謝你。我正在用「正確」的方式來完成任務。我幾個月前從意大利麪條程序代碼轉換到oop mvc,所以我仍然不確定我對特定問題的處理方法是否是正確的方式/方法。謝謝。 – ICJ