最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Applying Global Middleware to All Queued Jobs in Laravel 12 (Like withMiddleware for Web) - Stack Overflow

programmeradmin1浏览0评论

In Laravel 12, we define global middleware for web requests using the withMiddleware method in bootstrap/app.php, which makes it super easy to apply middleware to all HTTP requests automatically. I’m looking to do something similar for queued jobs: I want a specific middleware to run for all ShouldQueue entities—custom jobs, notifications, queued events, queued mailables, etc.—without having to attach it to each class individually.

Here’s the context: I’m building a multi-tenant app where every Notifiable (like a User) has a tenant_id. I’ve created a middleware (ConfigureJobTenantMiddleware) that sets tenant-specific configurations (e.g., mail settings, app URL) based on this tenant_id. Right now, I can apply it to custom jobs by defining a middleware() method, but I want it to work universally, including for Laravel’s built-in queueables like SendQueuedNotifications, without modifying every class.

Any insights or best practices for Laravel 12 would be awesome—especially if there’s a cleaner way to do this without hacking too deep into the framework!

I know there are many Tenancy packages but I want to implement mine to learn and understand Laravel as a beginner.

Thanks!

In Laravel 12, we define global middleware for web requests using the withMiddleware method in bootstrap/app.php, which makes it super easy to apply middleware to all HTTP requests automatically. I’m looking to do something similar for queued jobs: I want a specific middleware to run for all ShouldQueue entities—custom jobs, notifications, queued events, queued mailables, etc.—without having to attach it to each class individually.

Here’s the context: I’m building a multi-tenant app where every Notifiable (like a User) has a tenant_id. I’ve created a middleware (ConfigureJobTenantMiddleware) that sets tenant-specific configurations (e.g., mail settings, app URL) based on this tenant_id. Right now, I can apply it to custom jobs by defining a middleware() method, but I want it to work universally, including for Laravel’s built-in queueables like SendQueuedNotifications, without modifying every class.

Any insights or best practices for Laravel 12 would be awesome—especially if there’s a cleaner way to do this without hacking too deep into the framework!

I know there are many Tenancy packages but I want to implement mine to learn and understand Laravel as a beginner.

Thanks!

Share Improve this question asked Mar 30 at 17:04 Joe NyamburaJoe Nyambura 2035 silver badges14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

The Queue facade has a createPayloadUsing method which allows you to register a callback that is executed when the job payload is created. You should be able to use this to inject middleware into your jobs.

The code below injects a middleware (ConfigureJobTenantMiddleware) into the job and makes sure it runs before the middleware defined by the job itself.

use Illuminate\Support\Facades\Queue;
use Illuminate\Contracts\Queue\ShouldQueue;

Queue::createPayloadUsing(function ($connection, $queue, $payload) {
    if (isset($payload['data']['command'])) {
        $job = unserialize($payload['data']['command']);

        if ($job instanceof ShouldQueue) {
            $middleware = [new ConfigureJobTenantMiddleware()];

            if (method_exists($job, 'middleware')) {
                $middleware = array_merge($middleware, $job->middleware());
            }

            $job->middleware = $middleware;

            $payload['data']['command'] = serialize($job);
        }
    }

    return $payload;
});
发布评论

评论列表(0)

  1. 暂无评论