Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

neetha_hm's avatar

How to Create Module in Laravel?

HI..,I'm new to Laravel i want create module Can anyone suggest how to do it?

Thank you in advance

0 likes
23 replies
neetha_hm's avatar

@sinnbeck Thank you for replying ., I tired to follow the above steps and i am stuck at this error

Error Class 'Modules\Blog\Providers\Blog ServiceProvider' not found at

C:\xampp\htdocs\extra\laravelModule\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php:208 204▕ * @return \Illuminate\Support\ServiceProvider 205▕ */ 206▕ public function createProvider($provider) 207▕ { ➜ 208▕ return new $provider($this->app); 209▕ } 210▕ } 211▕ 1 C:\xampp\htdocs\extra\laravelModule\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php:144

Illuminate\Foundation\ProviderRepository::createProvider("Modules\Blog\Providers\BlogServiceProvider") 2 C:\xampp\htdocs\extra\laravelModule\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php:61 Illuminate\Foundation\ProviderRepository::compileManifest() Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1

Sinnbeck's avatar

Did you make a new service provider with a space in the name? Blog ServiceProvider

Sinnbeck's avatar

@neetha_hm Well if you look at the error, there is clearly a space in the name? Can you perhaps share the error page (there is a share button)

neetha_hm's avatar

@Sinnbeck i am not able to link any picture here because i have signed up today

Error

Class 'Modules\Blog\Providers\BlogServiceProvider' not found

at C:\xampp\htdocs\extra\laravelModule\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php:208 204▕ * @return \Illuminate\Support\ServiceProvider 205▕ */ 206▕ public function createProvider($provider) 207▕ { ➜ 208▕ return new $provider($this->app); 209▕ } 210▕ } 211▕

1 C:\xampp\htdocs\extra\laravelModule\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php:144 Illuminate\Foundation\ProviderRepository::createProvider("Modules\Blog\Providers\BlogServiceProvider")

2 C:\xampp\htdocs\extra\laravelModule\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php:61 Illuminate\Foundation\ProviderRepository::compileManifest()

-----this is the complete error i am getting in Terminal when i run the below command

php artisan vendor:publish --provider=ogDatabaseSeeder.php"Nwidart\Modules\LaravelModulesServiceProvider

Sinnbeck's avatar

@neetha_hm Ok better error :) Check if you have a file at modules/Blog/Providers/BlogServiceProvider.php

neetha_hm's avatar

@Sinnbeck This is the path C:\xampp\htdocs\extra\laravelModule\Modules\Blog\composer.json

{ "name": "nwidart/blog", "description": "", "authors": [ { "name": "Nicolas Widart", "email": "[email protected]" } ], "extra": { "laravel": { "providers": [], "aliases": {

        }
    }
},
"autoload": {
    "psr-4": {
        "App\": "app/",
        "Modules\": "Modules/"
      }
}

}

Sinnbeck's avatar

@neetha_hm Ah ok so the actual path on your computer is Modules/Blog/Providers/BlogServiceProvider.php not modules/Blog/Providers/BlogServiceProvider.php. Correct ?

neetha_hm's avatar

@Sinnbeck namespace Modules\Blog\Providers;

use Illuminate\Support\ServiceProvider; use Illuminate\Database\Eloquent\Factory;

class BlogServiceProvider extends ServiceProvider { /** * @var string $moduleName */ protected $moduleName = 'Blog';

/**
 * @var string $moduleNameLower
 */
protected $moduleNameLower = 'blog';

/**
 * Boot the application events.
 *
 * @return void
 */
public function boot()
{
    $this->registerTranslations();
    $this->registerConfig();
    $this->registerViews();
    $this->loadMigrationsFrom(module_path($this->moduleName, 'Database/Migrations'));
}

/**
 * Register the service provider.
 *
 * @return void
 */
public function register()
{
    $this->app->register(RouteServiceProvider::class);
}

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

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

    $sourcePath = module_path($this->moduleName, 'Resources/views');

    $this->publishes([
        $sourcePath => $viewPath
    ], ['views', $this->moduleNameLower . '-module-views']);

    $this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->moduleNameLower);
}

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

    if (is_dir($langPath)) {
        $this->loadTranslationsFrom($langPath, $this->moduleNameLower);
    } else {
        $this->loadTranslationsFrom(module_path($this->moduleName, 'Resources/lang'), $this->moduleNameLower);
    }
}

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

private function getPublishableViewPaths(): array
{
    $paths = [];
    foreach (\Config::get('view.paths') as $path) {
        if (is_dir($path . '/modules/' . $this->moduleNameLower)) {
            $paths[] = $path . '/modules/' . $this->moduleNameLower;
        }
    }
    return $paths;
}

}

Sinnbeck's avatar

@neetha_hm It seems that you did everything correctly, so I am unsure why php cant detect that class. Without having the code I am currently at a loss.

Please or to participate in this conversation.