Skip to content
Advertisement

How to load another Console/Kernel.php in Laravel project?

I have a running Laravel application which has the default Console/Kernel.php where all my scheduled commands are defined as usual.

But now I have another Laravel application that I am trying to merge with this existing application, and I have created a folder inside the existing Laravel application and I am using a Service Provider to load all the things. This way I can keep the code of the two projects separate but at the same time all features under the same repository.

<?php

namespace SecondApp;

use IlluminateSupportServiceProvider;

class SecondAppServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        include __DIR__ . '/routes/web.php';
        include __DIR__ . '/routes/api.php';

        $this->app->register('SecondAppAppProvidersAppServiceProvider');
        $this->app->register('SecondAppAppProvidersAuthServiceProvider');
        $this->app->register('SecondAppAppProvidersBroadcastServiceProvider');
        $this->app->register('SecondAppAppProvidersEventServiceProvider');
        $this->app->register('SecondAppAppProvidersJobServiceProvider');
        $this->app->register('SecondAppAppProvidersMailerServiceProvider');
        $this->app->register('SecondAppAppProvidersRouteServiceProvider');
        $this->app->register('SecondAppAppProvidersStorageServiceProvider');
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        $this->loadMigrationsFrom(__DIR__ . '/database/migrations/');

        $this->publishes([
            __DIR__ . '/config/' => config_path()
        ], 'second-app-config');

        $this->publishes([
            __DIR__ . '/resources/' => base_path('resources')
        ], 'second-app-resources');
    }
}

This is what my service somewhat looks like. Everything else seems to work well, roues, migrations, registering service providers and so on, but this second project now also has Console/Kernel.php file. And those commands are not being called yet, obviously because laravel doesn’t know about it. So I am wondering how can I tell Laravel to look at that as well? Is this possible, or will I have merge the code into one main Kernel.php?

I have the same question about Http/Kernel.php as well, but I am sure if someone can suggest how to make one work, I can make the other work as well.

Advertisement

Answer

To load commands, need to add them like this in the register method of service provider

$this->commands([
    AppConsoleCommandsCommandOne::class,
    AppConsoleCommandsCommandTwo::class,
]);

and so on, keep adding commands to the array.

And to schedule these commands refer to this answer -> How to schedule Artisan commands in a package?

So add something like this to the boot method of your service provider:

$this->callAfterResolving(Schedule::class, function (Schedule $schedule) {
    $schedule->command('some:command')->everyMinute();
});
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement