How to Implement Laravel Modules Structure with Nwidart Package
Tags: laravel, modular, php, tutorial
Published: 2022-05-05 04:57:04

Originally posted @ https://codeanddeploy.com visit and download the sample code:
https://codeanddeploy.com/blog/laravel/how-to-implement-laravel-modules-structure-with-nwidart-package

In this post, I will show you how to implement Laravel modules structure in with Nwidart package. This will help us to make our application clean and organized code that is easy to maintain and reusable to any modules within the application.

 

Some developers recommend implementing a **Laravel modules** because it comes with own controllers, model, resources and more. That help us not to break our application even if we remove one of our laravel module as long as not a base module that connect with other modules.

 

[Codeanddeploy](https://codeanddeploy.com/) also using laravel module structures.

 

When using Laravel modules it will generate with the following:

 

1. Config

2. Console

3. Database

4. Entities

5. Http

6. Providers

7. Resources

8. Routes

9. Tests

 

Now let's start installing our Laravel modules.

 

## Step 1: Laravel Installation
If you don't have a Laravel 8 install in your local just run the following command below:

```php
composer create-project --prefer-dist laravel/laravel laravel-modules-example

cd laravel-modules-example
```

## Step 2: Laravel Module Package Installation
Now, let's install our Laravel module package by Nwidart. Run the following command below:

```php
composer require nwidart/laravel-modules
```

## Step 3: Publish Configuration File
Run the following command to publish the configuration file.

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

## Step 4: Setup Autoloading
Now, we need to add `"Modules\\":``"Modules/",` to your composer under `autoload` > `psr-4`. See example below:

```php
"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Modules\\": "Modules/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    }
},
```
Then run the following command after setup above:


```php
composer dump-autoload
```
Now, we already setup our Laravel module package. Next let's create a module for our Laravel application.

 

## Step 5: Create Module
Syntax of create module command:

```php
php artisan make:module module_name
```
Then run the following command to create module let's do an example for Posts module.

```php
php artisan make:module posts
```

After running above commands it will generate our Posts module under Modules folder. See below Laravel module structures:

```php
app/
bootstrap/
vendor/
Modules/
  ├── Posts/
      ├── Assets/
      ├── Config/
      ├── Console/
      ├── Database/
          ├── Migrations/
          ├── Seeders/
      ├── Entities/
      ├── Http/
          ├── Controllers/
          ├── Middleware/
          ├── Requests/
      ├── Providers/
          ├── PostsServiceProvider.php
          ├── RouteServiceProvider.php
      ├── Resources/
          ├── assets/
              ├── js/
                ├── app.js
              ├── sass/
                ├── app.scss
          ├── lang/
          ├── views/
      ├── Routes/
          ├── api.php
          ├── web.php
      ├── Repositories/
      ├── Tests/
      ├── composer.json
      ├── module.json
      ├── package.json
      ├── webpack.mix.js
```
Now, we successfully generated our `Posts` module. Let's test it by running the command below:

```php
php artisan serve
```
Then run the URL to your browser:

```php
http://127.0.0.1:8000/posts
```

Then you will see the result below:

![dg](https://ecn-storage.s3.us-west-2.amazonaws.com/articles/implement-laravel-modules-structure-with-nwidart-package-fuZqIOHFFKx980.webp?ctm=1637468740)

For more details about it click [Laravel modules](https://nwidart.com/laravel-modules/v6/introduction).

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-implement-laravel-modules-structure-with-nwidart-package if you want to download this code.

Happy coding :)