为已认证和非认证用户注册不同的服务实现?

huangapple go评论74阅读模式
英文:

Registering different implementation of a service for auththenticated and non-authenticated users?

问题

我想能够根据用户是否已认证来注册不同的服务实现。

但根据我迄今所了解的情况,问题在于ServiceProviders在认证中间件之前执行,因此auth()->check()始终返回false。

那么有没有实现这一目标的方法?

或者我应该只是创建一个工厂类,以解析所需的实例?

还有其他方法吗?

英文:

I would like to be able to register different implementation of a service based on wether user is authenticated or not.

But from what i have found so far the problem is that ServiceProviders get executed before the authentication middleware, hence the auth()->check() always return false.

So is there a way to achieve this?

or should I just create a factory class, which resolves the needed instance?

Any other ways to do it?

答案1

得分: -2

Register Service Bindings (注册服务绑定):

Create Interfaces and Implementations (创建接口和实现):

// app/Services/AuthService.php
namespace App\Services;
interface AuthService {
   public function authenticate();
}

// app/Services/AuthenticatedAuthService.php
namespace App\Services;

class AuthenticatedAuthService implements AuthService {
public function authenticate() {
    // 针对已认证用户的实现
}

// app/Services/NonAuthenticatedAuthService.php
namespace App\Services;
class NonAuthenticatedAuthService implements AuthService {
public function authenticate() {
    // 针对未认证用户的实现
}
}

Open the app/Providers/AppServiceProvider.php file and modify the register() method as follows (打开app/Providers/AppServiceProvider.php文件并按以下方式修改register()方法):

use App\Services\AuthService;
use App\Services\AuthenticatedAuthService;
use App\Services\NonAuthenticatedAuthService;

public function register()
{
$this->app->bind(AuthService::class, function ($app) {
    if (auth()->check()) {
        return new AuthenticatedAuthService();
    } else {
        return new NonAuthenticatedAuthService();
    }
});
}

In this code, we're using Laravel's service container to bind the AuthService interface to the appropriate implementation based on the user's authentication status (在此代码中,我们使用Laravel的服务容器将AuthService接口绑定到根据用户的认证状态选择的适当实现)。

Usage in Controllers or Services (在控制器或服务中的使用):

namespace App\Http\Controllers;
use App\Services\AuthService;
class SomeController extends Controller
{
protected $authService;

public function __construct(AuthService $authService)
{
    $this->authService = $authService;
}

public function someMethod()
{
    $this->authService->authenticate();
    // 方法中的其他代码
}
}

In this example, we're injecting the AuthService into the controller's constructor and then using it within the someMethod() to call the appropriate implementation based on the user's authentication status (在此示例中,我们将AuthService注入控制器的构造函数,然后在someMethod()中使用它,根据用户的认证状态调用适当的实现)。

Make sure to adjust the namespaces and file locations according to your Laravel project's structure (根据您的Laravel项目结构调整命名空间和文件位置)。

By following these steps, you can dynamically switch between different implementations of the AuthService based on whether the user is authenticated or not throughout your Laravel application (通过按照这些步骤操作,您可以根据用户是否已认证,在整个Laravel应用程序中动态切换不同的AuthService实现)。

英文:

Register Service Bindings:

Create Interfaces and Implementations:

// app/Services/AuthService.php
namespace App\Services;
interface AuthService {
   public function authenticate();
}

// app/Services/AuthenticatedAuthService.php
namespace App\Services;

class AuthenticatedAuthService implements AuthService {
public function authenticate() {
    // Implementation for authenticated users
}

// app/Services/NonAuthenticatedAuthService.php
namespace App\Services;
class NonAuthenticatedAuthService implements AuthService {
public function authenticate() {
    // Implementation for non-authenticated users
}
}

Open the app/Providers/AppServiceProvider.php file and modify the register() method as follows:

use App\Services\AuthService;
use App\Services\AuthenticatedAuthService;
use App\Services\NonAuthenticatedAuthService;

public function register()
{
$this->app->bind(AuthService::class, function ($app) {
    if (auth()->check()) {
        return new AuthenticatedAuthService();
    } else {
        return new NonAuthenticatedAuthService();
    }
});
}

In this code, we're using Laravel's service container to bind the AuthService interface to the appropriate implementation based on the user's authentication status.

Usage in Controllers or Services:

namespace App\Http\Controllers;
use App\Services\AuthService;
class SomeController extends Controller
{
protected $authService;

public function __construct(AuthService $authService)
{
    $this->authService = $authService;
}

public function someMethod()
{
    $this->authService->authenticate();
    // Other code for the method
}
}

In this example, we're injecting the AuthService into the controller's constructor and then using it within the someMethod() to call the appropriate implementation based on the user's authentication status.

Make sure to adjust the namespaces and file locations according to your Laravel project's structure.

By following these steps, you can dynamically switch between different implementations of the AuthService based on whether the user is authenticated or not throughout your Laravel application.

huangapple
  • 本文由 发表于 2023年6月12日 17:14:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定