ehabafia's avatar
Level 37

Horizon in production

I did install Laravel horizon, and did php artisan vendor:publish and published the HorizonServiceProvider.

I added few emails to the gate to allow me to access horizon in production.

Gate::define('viewHorizon', function ($user = null) {
    return in_array($user->email, [
        '[email protected]'
    ]);
});

which still gave me a 403 Forbidden, then I start playing with it locally by changing my .env to production and didn't work no matter what.

then I noticed that the HorizonServiceProvider is not seen by Laravel all together, like, I tried to dd("HERE"); still didn't show.

I am using Laravel 11, do I need somehow to register HorizonServiceProvider to make it seen, and if yes, how?

Thank you very much for your support.

0 likes
3 replies
LaryAI's avatar
Level 58

It sounds like the HorizonServiceProvider is not being registered correctly in your Laravel application. In Laravel, service providers need to be registered in the config/app.php file under the providers array. Here’s how you can ensure that the HorizonServiceProvider is registered:

  1. Register the HorizonServiceProvider: Open the config/app.php file and add the HorizonServiceProvider to the providers array.

    'providers' => [
        // Other Service Providers
    
        Laravel\Horizon\HorizonServiceProvider::class,
    ],
    
  2. Ensure the Gate is Defined Correctly: Make sure that the gate definition is placed in the boot method of your AuthServiceProvider or another appropriate service provider. Here’s an example of how you can define it in the AuthServiceProvider:

    namespace App\Providers;
    
    use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
    use Illuminate\Support\Facades\Gate;
    
    class AuthServiceProvider extends ServiceProvider
    {
        public function boot()
        {
            $this->registerPolicies();
    
            Gate::define('viewHorizon', function ($user = null) {
                return in_array($user->email, [
                    '[email protected]'
                ]);
            });
        }
    }
    
  3. Check Your Environment Configuration: Ensure that your .env file has the correct environment settings. For example, make sure APP_ENV is set to production if you are testing in a production-like environment.

    APP_ENV=production
    
  4. Clear Configuration Cache: If you have cached your configuration, you need to clear it to ensure that the changes take effect. Run the following Artisan command:

    php artisan config:cache
    
  5. Check Horizon Configuration: Ensure that the config/horizon.php file is correctly configured. You might want to check the environments array to ensure that Horizon is enabled for the production environment.

    'environments' => [
        'production' => [
            'supervisor-1' => [
                'connection' => 'redis',
                'queue' => ['default'],
                'balance' => 'simple',
                'processes' => 10,
                'tries' => 3,
            ],
        ],
    ],
    

By following these steps, you should be able to register the HorizonServiceProvider correctly and define the gate to allow access to Horizon in production. If you still encounter issues, double-check your configurations and ensure that there are no typos or misconfigurations.

ehabafia's avatar
ehabafia
OP
Best Answer
Level 37

Ok, I added the following to bootstrap/app.php

    ->withProviders([
        \App\Providers\HorizonServiceProvider::class,
    ])

Now it starts to work, not sure why do I need to register the service provider, although it suppose to be auto discover.

1 like
aldesrahim's avatar

i think it's not auto discover, because it lives inside the App namespace, which treated like "custom" service provider, and it's actually not discovered automatically, you need to define that in the bootstrap/app.php

Please or to participate in this conversation.