laravel 服务提供者(provider)

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&copy;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&copy;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();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值