laravel的服务提供者感觉就是单例模式的实现(单例模式,即是在整个会话中这个类有且仅有一个实例)
我们在使用的要明确的知道某些概念,比如一个用户服务提供者,将会注册用户服务或是其它服务(如身份信息服务、邮寄地址服务等等)。
用户服务提供者:身份信息服务、邮寄地址服务
命令:创建一个服务提供者
php artisan make:provider TanyongProvider
在自己的服务提供者中注册自己的服务
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class TantongProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//注册服务 User
$this->app->singleton(\App\Services\User\UserInterface::class,\App\Services\User\UserService::class);
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
然后我们就可以在其它任何地方获取到这个接口的实例
public function index7()
{
//$userService = app()->get(UserInterface::class);
$userService = app()->get('custom_tanyong');
return $userService->get(1);
}
以下是UserInterface 和 UserService的实现
UserInterface
<?php
/**
* @copyright Copyright©2022,浙江销巴科技有限公司保留所有权利
* Notes:描述该文件的用途
* History:文件历史
* tanyong 2022/5/5
*/
namespace App\Services\User;
use App\Http\Requests\UserFormRequest;
use App\Models\User;
interface UserInterface
{
/**
* Notes:获取用户信息
* Author:tanyong
* DateTime:2022/5/5
* @param int $id
* @param array|null $fields
* @return User
*/
public function get(int $id,array $fields=null);
/**
* Notes:用户注册
* Author:tanyong
* DateTime:2022/5/5
* @param UserFormRequest $userRegisterForm
* @return mixed
*/
public function register(UserFormRequest $userRegisterForm);
/**
* Notes:删除用户
* Author:tanyong
* DateTime:2022/5/6
* @param int $id 用户id
* @return mixed
*/
public function delete(int $id);
/**
* Notes:查询分页器
* Author:tanyong
* DateTime:2022/5/10
* @param int $pageSize
* @return mixed
*/
public function getPageList(int $pageSize = 10);
/**
* Notes:获取用户信息
* Author:tanyong
* DateTime:2022/5/10
* @param string $username
* @param array $fields
* @return mixed
*/
public function findUsername(string $username,array $fields=[]);
}
UserService
<?php
/**
* @copyright Copyright©2022,浙江销巴科技有限公司保留所有权利
* Notes:描述该文件的用途
* History:文件历史
* tanyong 2022/5/5
*/
namespace App\Services\User;
use App\Exceptions\AppException;
use App\Exceptions\Ecode;
use App\Http\Requests\UserFormRequest;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
class UserService implements UserInterface
{
public function get(int $id, array $fields = null)
{
$user = User::where('id',$id);
if(!empty($fields))
$user->select($fields);
return $user->first();
}
public function register(UserFormRequest $userRegisterForm)
{
$userORM = new User();
$userORM->username = $userRegisterForm->username;
$userORM->nickname = $userRegisterForm->nickname;
$userORM->password = Hash::make($userRegisterForm->password);
$userORM->email = $userRegisterForm->email;
if($userORM->save())
return $userORM;
else
throw new AppException(Ecode::REGISTER_ERROR);
}
public function delete(int $id)
{
return User::where('id',$id)->delete();
}
public function getPageList(int $pageSize = 10)
{
return User::simplePaginate($pageSize);
}
public function findUsername(string $username, array $fields = [])
{
$query = User::where([
['username','=',$username]
]);
if(!empty($fields))
$query->select($fields);
return $query->first();
}
}