Laravel-modules: How to register console command in my eg Blog Module

Created on 13 Sep 2017  路  3Comments  路  Source: nWidart/laravel-modules

Hello Guys, thank you for your nice work... I am beginner in Laravel and this is my first time I am trying to use this package in a L5.5 project. can you please explain where I should register my commands for a particular Module. I am able to register it in the Console/Kernel.php but I don't want to put it there as this would list my command in the main artisan commands. .I want my command to be available in the Module eg Blog I created it. Please don't blame me for my question, it might be basic though I am beginner. Thanks for any help...

Most helpful comment

All 3 comments

You can do this in your module service provider like so:

$this->commands([YourCommand::class])

This method accepts either a string or an array of multiple commands.

Thanks for your swift reply, I did that and I when I run php artisan module:smc:report SMCReport I got this error
[Symfony\Component\Console\Exception\CommandNotFoundException] There are no commands defined in the "module:smc" namespace. Did you mean this? module
please see my ServiceProvider Class below:
`
namespace Modules\SMCReport\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Factory;
use Modules\SMCReport\Console\GenerateSMCReport;

class SMCReportServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
 * Boot the application events.
 *
 * @return void
 */
public function boot()
{
    $this->registerTranslations();
    $this->registerConfig();
    $this->registerViews();
    $this->registerFactories();
    $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
    $this->commands([GenerateSMCReport::class]);
}

/**
 * Register the service provider.
 *
 * @return void
 */
public function register()
{
    //
}

/**
 * Register config.
 *
 * @return void
 */
protected function registerConfig()
{
    $this->publishes([
        __DIR__.'/../Config/config.php' => config_path('smcreport.php'),
    ], 'config');
    $this->mergeConfigFrom(
        __DIR__.'/../Config/config.php', 'smcreport'
    );
}

/**
 * Register views.
 *
 * @return void
 */
public function registerViews()
{
    $viewPath = resource_path('views/modules/smcreport');

    $sourcePath = __DIR__.'/../Resources/views';

    $this->publishes([
        $sourcePath => $viewPath
    ]);

    $this->loadViewsFrom(array_merge(array_map(function ($path) {
        return $path . '/modules/smcreport';
    }, \Config::get('view.paths')), [$sourcePath]), 'smcreport');
}

/**
 * Register translations.
 *
 * @return void
 */
public function registerTranslations()
{
    $langPath = resource_path('lang/modules/smcreport');

    if (is_dir($langPath)) {
        $this->loadTranslationsFrom($langPath, 'smcreport');
    } else {
        $this->loadTranslationsFrom(__DIR__ .'/../Resources/lang', 'smcreport');
    }
}

/**
 * Register an additional directory of factories.
 * @source https://github.com/sebastiaanluca/laravel-resource-flow/blob/develop/src/Modules/ModuleServiceProvider.php#L66
 */
public function registerFactories()
{
    if (! app()->environment('production')) {
        app(Factory::class)->load(__DIR__ . '/../Database/factories');
    }
}

/**
 * Get the services provided by the provider.
 *
 * @return array
 */
public function provides()
{
    return [];
}

}
`

and when I do php artisan module my new command is not listed in the module commands

Was this page helpful?
0 / 5 - 0 ratings

Related issues

invaders-xx picture invaders-xx  路  3Comments

dagulo picture dagulo  路  3Comments

developh picture developh  路  3Comments

ghost picture ghost  路  4Comments

almamund picture almamund  路  3Comments