Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/65951569/how-to-load-another-console-kernel-php-in-laravel-project

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...