Laravel自定义中间件用于2个数据库

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

Laravel custom middleware for 2 databases

问题

我不知道如何为两个数据库的令牌创建中间件。

(首先,我的英文不是很好,我在Laravel方面是个初学者)我创建了一个Laravel项目,其中有两个Eloquent Authenticatable,它们分别使用不同的数据库。我成功地将令牌保存到了这两个数据库的个人访问令牌中,但我不知道如何为这些令牌创建中间件。第一个数据库(默认数据库)没有任何问题,但第二个数据库有问题。
备注1:我在身份验证方面使用了Sanctum。
备注2:我之所以必须将两个数据库分开,是因为我的工作要求如此。

英文:

I don’t know how to create middleware for two databases tokens.

(First of all my english is not very well and I am beginner in laravel) I created a Laravel project that has two Eloquent Authenticatables that separate the database. I successfully saved tokens to personal access tokens for both databases, but I don’t know how to create middleware for those tokens. The first database (the default database) does not have any problems, but the second database does.
Note 1: I’m using Sanctum for authentication.
Note 2: The reason that I have to separate two databases is because my work requires

答案1

得分: 0

尝试制作您自己的自定义中间件。中间件然后在handle函数内,您可以使用数据库查询来选择另一个数据库。

修改您的 app/config/database.php 如下所示。

return array(
    'default' => 'mysql',
    'connections' => array(
        # 主数据库
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'host1',
            'database'  => 'database1',
            'username'  => 'user1',
            'password'  => 'pass1',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        # 次要数据库
        'mysql2' => array(
            'driver'    => 'mysql',
            'host'      => 'host2',
            'database'  => 'database2',
            'username'  => 'user2',
            'password'  => 'pass2',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
);

然后在您的 middleware 中。

namespace App\Http\Middleware;
 
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
 
class BeforeMiddleware
{
    public function handle(Request $request, Closure $next): Response
    {
       // 在这里编写代码
       // DB::connection('mysql')->table('yourtable');

        return $next($request);
    }
}
英文:

Try to make your own custom middleware. Middleware then inside the handle function instead of using eloquent. you may use DB queries to select another database.

Modify your app/config/database.php like the following.

<?php
return array(
    'default' => 'mysql',
    'connections' => array(
        # primary
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'host1',
            'database'  => 'database1',
            'username'  => 'user1',
            'password'  => 'pass1'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        # secondary
        'mysql2' => array(
            'driver'    => 'mysql',
            'host'      => 'host2',
            'database'  => 'database2',
            'username'  => 'user2',
            'password'  => 'pass2'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
);

Then in your middleware.

<?php
 
namespace App\Http\Middleware;
 
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
 
class BeforeMiddleware
{
    public function handle(Request $request, Closure $next): Response
    {
       // code here
       // DB::connection('mysql')->table('yourtable');

        return $next($request);
    }
}

huangapple
  • 本文由 发表于 2023年2月24日 17:13:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554631.html
匿名

发表评论

匿名网友

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

确定