### Install Laravel Modules Package with Composer
Source: https://laravelmodules.com/docs/12/getting-started/installation-and-setup
Installs the nwidart/laravel-modules package using Composer. This command should be run in your project's root directory.
```shell
composer require nwidart/laravel-modules
```
--------------------------------
### Publish Laravel Modules Package Files
Source: https://laravelmodules.com/docs/12/getting-started/installation-and-setup
Publishes the package's configuration files and stub files to your Laravel project. This allows for customization of the package's behavior.
```shell
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
```
--------------------------------
### Publish Vite Modules Loader
Source: https://laravelmodules.com/docs/12/getting-started/installation-and-setup
Publishes the vite-modules-loader.js file, specifically for versions 10.0.3 and above. This is used for integrating Vite with module loading.
```shell
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag="vite"
```
--------------------------------
### Configure Autoloading with Merge Plugin
Source: https://laravelmodules.com/docs/12/getting-started/installation-and-setup
Configures the autoloading of modules by adding the 'merge-plugin' to the 'extra' section of your composer.json file. This ensures module classes are discoverable.
```json
"extra": {
"laravel": {
"dont-discover": []
},
"merge-plugin": {
"include": [
"Modules/*/composer.json"
]
}
}
```
--------------------------------
### Publish Laravel Modules Stubs
Source: https://laravelmodules.com/docs/12/getting-started/installation-and-setup
Publishes only the stub files for the Laravel Modules package. Stubs are used for generating module files and can be customized.
```shell
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag="stubs"
```
--------------------------------
### Example: Generate Livewire Component
Source: https://laravelmodules.com/docs/12/resources/livewire
Provides examples of generating Livewire components for a module, including different naming conventions.
```bash
php artisan module:make-livewire Pages/AboutPage Core
php artisan module:make-livewire Pages\AboutPage Core
php artisan module:make-livewire pages.about-page Core
```
--------------------------------
### PestPHP setup and basic tests
Source: https://laravelmodules.com/docs/12/advanced/tests
This demonstrates how to set up PestPHP tests by importing the TestCase and provides examples of basic tests for viewing and deleting contacts. It assumes the necessary models and routes are available.
```php
uses(TestsTestCase::class);
```
```php
authenticate();
$this->get(route('app.contacts.index'))->assertOk();
});
test('can delete contact', function() {
$this->authenticate();
$contact = Contact::factory()->create();
$this->delete(route('app.contacts.delete', $contact->id))->assertRedirect(route('app.contacts.index'));
$this->assertDatabaseCount('contacts', 0);
});
```
--------------------------------
### Module Configuration File Example
Source: https://laravelmodules.com/docs/12/basic-usage/configuration
An example of a basic module configuration file named 'config.php' that returns an array with a module name.
```php
'ModuleName',
];
```
--------------------------------
### Install Laravel Modules Livewire Package
Source: https://laravelmodules.com/docs/12/resources/livewire
Installs the 'laravel-modules-livewire' package using Composer.
```bash
composer require mhmiton/laravel-modules-livewire
```
--------------------------------
### Install Spatie Laravel Permission Package
Source: https://laravelmodules.com/docs/12/resources/spatie-laravel-permission
Installs the spatie/laravel-permission package using Composer. This is the initial step to integrate roles and permissions management into your Laravel application.
```bash
composer require spatie/laravel-permission
```
--------------------------------
### Publish Laravel Modules Configuration
Source: https://laravelmodules.com/docs/12/getting-started/installation-and-setup
Publishes only the configuration file for the Laravel Modules package. This allows for specific configuration changes without affecting other package files.
```shell
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag="config"
```
--------------------------------
### Install Module
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Installs a module by its given name, typically in the format 'vendor/module'.
```php
Module::install('nwidart/hello');
```
--------------------------------
### Install npm Dependencies
Source: https://laravelmodules.com/docs/12/basic-usage/compiling-assets
Installs all the necessary Node.js dependencies listed in the package.json file.
```bash
npm install
npm install
```
--------------------------------
### Install Laravel Module Package (Composer)
Source: https://laravelmodules.com/docs/12/advanced/publishing-modules
Installs the nwidart/laravel-module-installer plugin, which automatically moves module files to the designated directory upon installation.
```bash
composer require nwidart/laravel-module-installer
```
--------------------------------
### Example of Substituted Controller with Specific Module
Source: https://laravelmodules.com/docs/12/resources/custom-module-generator
Provides a concrete example of the controller structure after placeholders have been replaced with actual module and model names (e.g., 'Contacts' module and 'Contact' model).
```php
namespace Modules\Contacts\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Contacts\Models\Contact;
class {Module}Controller extends Controller
{
public function index()
{
$contacts = Contact::get();
return view('contacts::index', compact('contacts'));
}
}
```
--------------------------------
### Installing Node.js Dependencies
Source: https://laravelmodules.com/docs/12/basic-usage/compiling-assets
Installs the project's Node.js dependencies, as defined in the package.json file, which are required for running Mix and other build tools.
```bash
npm install
```
--------------------------------
### Placeholder Examples for Module Naming Conventions
Source: https://laravelmodules.com/docs/12/resources/custom-module-generator
Demonstrates different placeholder formats for module names based on casing and separators (underscore or hyphen). These are fundamental for defining module identity.
```plaintext
{module_}
{module_}
```
```plaintext
{module-}
{module-}
```
```plaintext
{Module}
{Module}
```
```plaintext
{module}
{module}
```
--------------------------------
### Run Vite for Development (Hot Reloading)
Source: https://laravelmodules.com/docs/12/basic-usage/compiling-assets
Starts the Vite development server, enabling hot-reloading for rapid development and watching for file changes.
```bash
npm run dev
npm run dev
```
--------------------------------
### PHPUnit test method example
Source: https://laravelmodules.com/docs/12/advanced/tests
An example of a PHPUnit test method that authenticates, creates a contact, asserts a redirect after deletion, and verifies the database count.
```php
/** @test */
public function can_delete_contact(): void
{
$this->authenticate();
$contact = Contact::factory()->create();
$this->delete(route('app.contacts.delete', $contact->id))->assertRedirect(route('app.contacts.index'));
$this->assertDatabaseCount('contacts', 0);
}
```
--------------------------------
### List All Available Modules
Source: https://laravelmodules.com/docs/12/advanced/artisan-commands
Displays a comprehensive list of all modules that are currently installed and available within the Laravel project. This is a core command for module management and overview.
```php
php artisan module:list
```
```php
php artisan module:list
```
--------------------------------
### PestPHP test method example
Source: https://laravelmodules.com/docs/12/advanced/tests
An example of a PestPHP test function that authenticates, creates a contact, asserts a redirect after deletion, and verifies the database count.
```php
test('can_delete_contact', function() {
$this->authenticate();
$contact = Contact::factory()->create();
$this->delete(route('app.contacts.delete', $contact->id))->assertRedirect(route('app.contacts.index'));
$this->assertDatabaseCount('contacts', 0);
});
```
--------------------------------
### Installing laravel-mix-merge-manifest
Source: https://laravelmodules.com/docs/12/basic-usage/compiling-assets
Installs the laravel-mix-merge-manifest package as a development dependency to prevent the main Laravel Mix configuration from overwriting module-specific manifest files.
```bash
npm install laravel-mix-merge-manifest --save-dev
```
--------------------------------
### Install Symfony Filesystem Component
Source: https://laravelmodules.com/docs/12/resources/custom-module-generator
This command installs the Symfony Filesystem component using Composer, which is required for file manipulation operations within the Artisan command.
```bash
composer require symfony/filesystem
```
--------------------------------
### JSON Translation Content Example
Source: https://laravelmodules.com/docs/12/advanced/languages
Provides an example of the content for a JSON translation file, mapping English keys to French values. This file would be named according to the locale, e.g., 'fr.json'.
```json
{
"Name": "Nom",
"Subject": "Sujette",
}
```
--------------------------------
### Module Statuses JSON Example
Source: https://laravelmodules.com/docs/12/basic-usage/configuration
Illustrates the structure of the `module_statuses.json` file, showing how to manually enable or disable modules. This file tracks the enabled status of each module.
```json
{
"Users": true
}
```
--------------------------------
### Render Livewire Components
Source: https://laravelmodules.com/docs/12/resources/livewire
Demonstrates the syntax for rendering Livewire components within a Blade view. The component tag follows the pattern . This example shows a generic and a specific 'core' module component.
```html
```
--------------------------------
### Setup Lumen Configuration for Laravel Modules
Source: https://laravelmodules.com/docs/12/getting-started/lumen
This snippet demonstrates how to set up the configuration file for the laravel-modules package in a Lumen project. It involves creating a 'config' directory and copying the default configuration file to 'config/modules.php'.
```shell
mkdir config
cp vendor/nwidart/laravel-modules/config/config.php config/modules.php
```
--------------------------------
### Install Custom Module (Composer)
Source: https://laravelmodules.com/docs/12/advanced/publishing-modules
Installs a custom module package into the Laravel project using the composer require command.
```bash
composer require nwidart/article-module
```
--------------------------------
### Install Module Package via Composer
Source: https://laravelmodules.com/docs/12/advanced/publishing-modules
Specifies a private package for Composer to require and defines its repository location. This allows for the installation of custom or private Laravel modules.
```json
"vendorname/moduleName-module": "@dev"
"repositories": [
{
"type": "vcs",
"url": "git@bitbucket.org:vendorname/moduleName-module.git"
}
]
```
--------------------------------
### Configure Module Installation Directory (Composer JSON)
Source: https://laravelmodules.com/docs/12/advanced/publishing-modules
Specifies a custom directory for module installation within the project's composer.json file. This is an alternative to the default 'Modules/' folder.
```json
{
"extra": {
"module-dir": "Custom"
}
}
```
--------------------------------
### Laravel Module File Structure Example
Source: https://laravelmodules.com/docs/12/getting-started/introduction
This code snippet illustrates the typical file structure generated by the `php artisan module:make Blog` command. It shows the organization of files within the module, covering essential components like controllers, models, service providers, configuration files, database migrations and seeders, views, and route definitions. This structure is key to maintaining modularity in a Laravel application.
```file-structure
├── app
│ ├── Http
│ │ └── Controllers
│ │ │ └── BlogController.php
│ ├── Models
│ └── Providers
│ │ ├── BlogServiceProvider.php
│ │ └── RouteServiceProvider.php
├── config
│ └── config.php
├── database
│ ├── factories
│ ├── migrations
│ └── seeders
│ │ └── BlogDatabaseSeeder.php
├── resources
│ ├── assets
│ │ ├── js
│ │ │ └── app.js
│ │ └── sass
│ │ │ └── app.scss
│ └── views
│ │ ├── layouts
│ │ │ └── master.blade.php
│ │ └── index.blade.php
├── routes
│ ├── api.php
│ └── web.php
├── tests
│ ├── Feature
│ └── Unit
├── composer.json
├── module.json
├── package.json
└── vite.config.js
```
--------------------------------
### Install Laravel Module Generator Package
Source: https://laravelmodules.com/docs/12/resources/laravel-module-generator
Installs the Dcblogdev's Module Generator package using Composer. This is the initial step to integrate the package into your Laravel project.
```bash
composer require dcblogdev/laravel-module-generator
```
--------------------------------
### Get All Enabled Modules
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Retrieves all modules that are currently enabled.
```php
Module::allEnabled();
```
--------------------------------
### Nested Module Configuration Files
Source: https://laravelmodules.com/docs/12/basic-usage/configuration
Shows an example of organizing module configuration files in nested folders and how to access them using a nested path.
```php
config/
admin/admin.php
admin.php
config.php
user.php
```
--------------------------------
### Get All Modules
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Retrieves all registered modules in the Laravel application.
```php
Module::all();
```
--------------------------------
### Get All Modules as Collection
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Returns all registered modules as a Laravel Collection instance.
```php
Module::toCollection();
```
--------------------------------
### Get Scanned Paths
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Retrieves all configured paths that are scanned for modules.
```php
Module::getScanPaths();
```
--------------------------------
### Get Enabled Modules as Collection
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Retrieves all enabled modules as a Laravel Collection instance.
```php
Module::collections();
```
--------------------------------
### Placeholder Examples for Model Naming Conventions
Source: https://laravelmodules.com/docs/12/resources/custom-module-generator
Shows placeholder variations for model names, emphasizing CamelCase and all lowercase formats. These are crucial for defining data models within modules.
```plaintext
{Model}
{Model}
```
```plaintext
{model}
{model}
```
--------------------------------
### Boot All Modules
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Boots all available modules, making them active and ready for use.
```php
Module::boot();
```
--------------------------------
### Get Module Requirements
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Retrieves the required dependencies for a given module.
```php
Module::getRequirements('module name');
```
--------------------------------
### Get Package Config Value
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Retrieves a configuration value from the package, using a dot-notation key.
```php
Module::config('composer.vendor');
```
--------------------------------
### Initialize Git Repository
Source: https://laravelmodules.com/docs/12/advanced/publishing-modules
Initializes a new Git repository in the current directory, preparing the project for version control.
```bash
git init
```
--------------------------------
### Get Module Description
Source: https://laravelmodules.com/docs/12/advanced/module-methods
Retrieves the description of the module, typically from its configuration file.
```php
$module->getDescription();
```
--------------------------------
### Get Ordered Modules
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Retrieves all modules ordered by their priority defined in the `module.json` file.
```php
Module::getOrdered();
```
--------------------------------
### Update npm Dependencies
Source: https://laravelmodules.com/docs/12/basic-usage/compiling-assets
Updates all the installed Node.js dependencies to their latest compatible versions.
```bash
npm update
npm update
```
--------------------------------
### GET /app/{module}/create
Source: https://laravelmodules.com/docs/12/resources/custom-module-generator
Displays the form for creating a new module. Requires authentication.
```APIDOC
## GET /app/{module}/create
### Description
Displays the form for creating a new module.
### Method
GET
### Endpoint
`/app/{module}/create`
### Parameters
#### Path Parameters
- **module** (string) - Required - The identifier for the module.
### Response
#### Success Response (200)
- HTML form for creating a new module.
```
--------------------------------
### Defining a Module Event Listener Class (PHP)
Source: https://laravelmodules.com/docs/12/advanced/registering-module-events
Provides an example of a listener class for handling module events. The `handle` method contains the logic to be executed when a specific module event is triggered.
```php
namespace App\Listeners;
class ModuleBootListener
{
public function handle($event)
{
// Your logic when the module boots
}
}
```
--------------------------------
### Get Used Module for CLI
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Gets the module currently being used in a CLI session.
```php
Module::getUsedNow();
// OR
Module::getUsed();
```
--------------------------------
### GET /app/{module}
Source: https://laravelmodules.com/docs/12/resources/custom-module-generator
Retrieves a list of all modules. This endpoint is part of the web routes and requires authentication.
```APIDOC
## GET /app/{module}
### Description
Retrieves a list of all modules.
### Method
GET
### Endpoint
`/app/{module}`
### Parameters
#### Path Parameters
- **module** (string) - Required - The identifier for the module.
### Response
#### Success Response (200)
- **modules** (array) - A list of module objects.
#### Response Example
```json
{
"modules": [
{
"id": 1,
"name": "Example Module"
}
]
}
```
```
--------------------------------
### Get Module App Path
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Returns the 'app' path for a module. If a module lacks an 'app' folder, it returns the module's root path.
```php
Module::getAppPath();
```
--------------------------------
### Load and Publish Views in Laravel Module
Source: https://laravelmodules.com/docs/12/advanced/module-resources
This code demonstrates how to load views from a module and optionally publish them to the main application's view directory. It configures the view path and uses `loadViewsFrom` for module-specific views. Assumes a module named 'Blog'.
```php
$viewPath = base_path('resources/views/modules/blog');
$sourcePath = __DIR__.'/../resources/views';
$this->publishes([
$sourcePath => $viewPath
]);
$this->loadViewsFrom(array_merge(array_map(function ($path) {
return $path . '/modules/blog';
}, Config::get('view.paths')), [$sourcePath]), 'blog');
$viewPath = base_path('resources/views/modules/blog');
$sourcePath = __DIR__.'/../resources/views';
$this->publishes([
$sourcePath => $viewPath
]);
$this->loadViewsFrom(array_merge(array_map(function ($path) {
return $path . '/modules/blog';
}, Config::get('view.paths')), [$sourcePath]), 'blog');
```
--------------------------------
### Use Module Layout in Livewire Component
Source: https://laravelmodules.com/docs/12/resources/livewire
This example demonstrates how to specify a custom layout from a module for a Livewire component's render method. It overrides the default layout by chaining the ->layout() method.
```php
public function render()
{
return view('contacts::livewire.feedback')->layout('contacts::layouts.app');
}
```
--------------------------------
### Standard Laravel Module Directory Structure
Source: https://laravelmodules.com/docs/12/getting-started/upgrade
This outlines the typical directory structure for a newly generated Laravel module. It includes folders for application code (app), configuration (config), database (database), resources (resources), routes (routes), and tests (tests), along with key configuration files like composer.json and module.json.
```tree
Modules
└── Blog
├── app
│ ├── Http
│ │ └── Controllers
│ │ └── BlogController.php
│ ├── Models
│ └── Providers
│ ├── BlogServiceProvider.php
│ └── RouteServiceProvider.php
├── config
│ └── config.php
├── database
│ ├── factories
│ ├── migrations
│ └── seeders
│ └── BlogDatabaseSeeder.php
├── resources
│ ├── assets
│ │ ├── js
│ │ │ └── app.js
│ │ └── sass
│ │ └── app.scss
│ └── views
│ ├── layouts
│ │ └── master.blade.php
│ └── index.blade.php
├── routes
│ ├── api.php
│ └── web.php
├── tests
│ ├── Feature
│ └── Unit
├── composer.json
├── module.json
├── package.json
└── vite.config.js
```
--------------------------------
### Generate Laravel Module with nwidart/laravel-modules
Source: https://laravelmodules.com/docs/12/getting-started/introduction
This command generates a new module named 'Blog' using the nwidart/laravel-modules package. It creates a standard file structure for the module, including directories for controllers, models, providers, configuration, database files, resources (views, assets), and routes. This structure promotes modularity and organization within a Laravel project.
```shell
php artisan module:make Blog
```
--------------------------------
### Module Configuration (module.json)
Source: https://laravelmodules.com/docs/12/basic-usage/creating-a-module
Presents the structure of the module.json file used to configure module details, providers, and files.
```json
{
"name": "Blog",
"alias": "blog",
"description": "",
"keywords": [],
"priority": 0,
"providers": [
"Modules\Blog\Providers\BlogServiceProvider"
],
"files": []
}
```
--------------------------------
### Get All Cached Modules
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Retrieves all modules that have been cached.
```php
Module::getCached();
```
--------------------------------
### Get All Disabled Modules
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Retrieves all modules that are currently disabled.
```php
Module::allDisabled();
```
--------------------------------
### Create a Plain Module using Artisan Flags
Source: https://laravelmodules.com/docs/12/basic-usage/creating-a-module
Shows how to create a module without default resources (controller, seed class, etc.) using the `--plain` or `-p` flag.
```php
php artisan module:make Blog --plain
```
```php
php artisan module:make Blog -p
```
--------------------------------
### Create an API Module using Artisan Flag
Source: https://laravelmodules.com/docs/12/basic-usage/creating-a-module
Illustrates how to create a module specifically for API usage with the `--api` flag.
```php
php artisan module:make Blog --api
```
--------------------------------
### Add Remote Origin and Push to GitHub
Source: https://laravelmodules.com/docs/12/advanced/publishing-modules
Configures the Git remote to point to a GitHub repository and pushes the initial commit to the main branch. This is typically done once to establish the connection.
```bash
git remote add origin git@github.com:username/quotes-module.git
git push -u origin main
```
--------------------------------
### Build Assets with Vite
Source: https://laravelmodules.com/docs/12/basic-usage/compiling-assets
Compiles all front-end assets (CSS, JavaScript) for production.
```bash
npm run build
npm run build
```
--------------------------------
### Get Module Count
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Retrieves the total count of all registered modules.
```php
Module::count();
```
--------------------------------
### Publish Laravel Permission Migrations and Config
Source: https://laravelmodules.com/docs/12/resources/spatie-laravel-permission
Publishes the necessary migration files and configuration settings for the Spatie Laravel Permission package. This command integrates the package's database structure and settings into your project.
```bash
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
```
--------------------------------
### Get Module Name
Source: https://laravelmodules.com/docs/12/advanced/module-methods
Returns the human-readable name of the module.
```php
$module->getName();
```
--------------------------------
### Enable a Laravel Module
Source: https://laravelmodules.com/docs/12/basic-usage/configuration
Command to enable a specific module in Laravel Modules. This command also creates the `module_statuses.json` file if it doesn't exist, initializing module statuses.
```bash
php artisan module:enable ModuleName
```
--------------------------------
### Module Folder Structure
Source: https://laravelmodules.com/docs/12/basic-usage/creating-a-module
Displays the default file and folder structure for a newly created Laravel module.
```directory
Modules/
├── Blog/
├──app
├── Http/
├── Controllers/
BlogController.php
├── Providers/
├── BlogServiceProvider.php
├── RouteServiceProvider.php
├── config/
├──config.php
├── database/
├── factories/
├── migrations/
├── seeders/
├── BlogDatabaseSeeder.php
├── resources/
├── assets/
├── views/
├── routes/
├── api.php
├── web.php
├── tests/
├── composer.json
├── module.json
├── package.json
├── vite.config.js
```
--------------------------------
### Get Specific Module Path
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Retrieves the path of a specific module by its name.
```php
Module::getModulePath('name');
```
--------------------------------
### Get Module Root Path
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Returns the root path of the modules package.
```php
Module::getPath();
```
--------------------------------
### Run All PHPUnit Tests
Source: https://laravelmodules.com/docs/12/advanced/tests
This command executes all defined test suites configured in the PHPUnit XML file, including unit, feature, and module tests.
```bash
vendor/bin/phpunit
```
--------------------------------
### Add Custom Commands
Source: https://laravelmodules.com/docs/12/basic-usage/configuration
Shows how to add custom console commands to the package by merging them with the default commands provided by the ConsoleServiceProvider.
```php
'commands' => ConsoleServiceProvider::defaultCommands()
->merge([
// New commands go here
])->toArray(),
```
--------------------------------
### Add and Commit Files to Git
Source: https://laravelmodules.com/docs/12/advanced/publishing-modules
Stages all changes in the current directory and commits them with a descriptive message, creating the initial commit for the repository.
```bash
git add .
git commit -m 'first commit'
```
--------------------------------
### Get Asset URL from Module
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Generates a URL for an asset within a specific module.
```php
Module::asset('blog:img/logo.img');
```
--------------------------------
### Get Used Storage Path
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Retrieves the storage path currently being used by the package.
```php
Module::getUsedStoragePath();
```
--------------------------------
### Get Module Assets Path
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Retrieves the assets path for a specific module by its name.
```php
Module::assetPath('name');
```
--------------------------------
### Running Laravel Mix Tasks
Source: https://laravelmodules.com/docs/12/basic-usage/compiling-assets
Executes Laravel Mix tasks using npm scripts. 'npm run dev' compiles assets for development, and 'npm run production' compiles and optimizes assets for deployment.
```bash
npm run dev
pm run production
```
--------------------------------
### Scan Modules
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Scans the configured paths for available modules.
```php
Module::scan();
```
--------------------------------
### Add Bitbucket Remote Origin
Source: https://laravelmodules.com/docs/12/advanced/publishing-modules
Connects the local Git repository to a remote repository hosted on Bitbucket by adding the remote origin URL.
```bash
git remote add origin git@bitbucket.org:username/quotes-module.git
```
--------------------------------
### Publish Laravel Modules Livewire Configuration
Source: https://laravelmodules.com/docs/12/resources/livewire
Publishes the configuration file for the 'laravel-modules-livewire' package using Artisan.
```bash
php artisan vendor:publish --provider="Mhmiton\LaravelModulesLivewire\LaravelModulesLivewireServiceProvider"
```
--------------------------------
### Get All Modules Assets Path
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Retrieves the collective assets path for all modules managed by the package.
```php
Module::getAssetsPath();
```
--------------------------------
### Get Modules by Status
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Retrieves modules based on their status: 1 for active, 0 for inactive.
```php
Module::getByStatus(1);
```
--------------------------------
### Complete Laravel Module Stub Configuration
Source: https://laravelmodules.com/docs/12/basic-usage/configuration
Comprehensive configuration for Laravel Modules, defining stub file paths, replacements for dynamic content, and enabling/disabling features like gitkeep. This allows deep customization of module generation.
```php
'stubs' => [
'enabled' => false,
'path' => base_path('vendor/nwidart/laravel-modules/src/Commands/stubs'),
'files' => [
'routes/web' => 'routes/web.php',
'routes/api' => 'routes/api.php',
'views/index' => 'resources/views/index.blade.php',
'views/master' => 'resources/views/layouts/master.blade.php',
'scaffold/config' => 'config/config.php',
'composer' => 'composer.json',
'assets/js/app' => 'resources/assets/js/app.js',
'assets/sass/app' => 'resources/assets/sass/app.scss',
'vite' => 'vite.config.js',
'package' => 'package.json',
],
'replacements' => [
'routes/web' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE', 'CONTROLLER_NAMESPACE'],
'routes/api' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE', 'STUDLY_NAME', 'CONTROLLER_NAMESPACE'],
'vite' => ['LOWER_NAME'],
'json' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE', 'PROVIDER_NAMESPACE'],
'views/index' => ['LOWER_NAME'],
'views/master' => ['LOWER_NAME', 'STUDLY_NAME'],
'scaffold/config' => ['STUDLY_NAME'],
'composer' => [
'LOWER_NAME',
'STUDLY_NAME',
'VENDOR',
'AUTHOR_NAME',
'AUTHOR_EMAIL',
'MODULE_NAMESPACE',
'PROVIDER_NAMESPACE',
],
],
'gitkeep' => true,
]
```
--------------------------------
### Get Module Priority
Source: https://laravelmodules.com/docs/12/advanced/module-methods
Returns the priority assigned to the module, which can affect loading order.
```php
$module->getPriority();
```
--------------------------------
### Rendering Views with Custom Namespaces (PHP)
Source: https://laravelmodules.com/docs/12/basic-usage/custom-namespaces
Illustrates how to render views using the custom module namespace 'blog'. This enables modular view organization and access within the Laravel application.
```php
view('blog::index')
view('blog::partials.sidebar')
```
--------------------------------
### Get Module Name in Lowercase
Source: https://laravelmodules.com/docs/12/advanced/module-methods
Retrieves the module's name converted to lowercase.
```php
$module->getLowerName();
```
--------------------------------
### Enable, Migrate, and Seed Laravel Module
Source: https://laravelmodules.com/docs/12/advanced/publishing-modules
Artisan commands to activate a module, run its database migrations, and seed its data. These commands are essential for integrating a module into the Laravel application.
```bash
php artisan module:enable moduleName
php artisan module:migrate moduleName
php artisan module:seed moduleName
```
--------------------------------
### Create a New Module using Artisan
Source: https://laravelmodules.com/docs/12/basic-usage/creating-a-module
Demonstrates the artisan command to create a new module with a specified name. It also shows how to create multiple modules in a single command.
```php
php artisan module:make posts
```
```php
php artisan module:make customers contacts users invoices quotes
```
--------------------------------
### Get Module Requirements
Source: https://laravelmodules.com/docs/12/advanced/module-methods
Retrieves an array listing the aliases of other modules that the current module depends on.
```php
$module->getRequires();
```
--------------------------------
### Navigate to Module Directory
Source: https://laravelmodules.com/docs/12/basic-usage/compiling-assets
Changes the current directory to the newly created module's directory.
```bash
cd Modules/Blog
cd Modules/Blog
```
--------------------------------
### Get Module Path
Source: https://laravelmodules.com/docs/12/advanced/module-methods
Returns the absolute file system path to the module's directory.
```php
$module->getPath();
```
--------------------------------
### Composer Merge Plugin: Enable confirmation prompt
Source: https://laravelmodules.com/docs/12/getting-started/upgrade
When upgrading to v11 of the package, you may be prompted to enable the Composer Merge Plugin. Press 'y' to allow its execution, which is now required for merging composer files from modules.
```bash
Do you trust "wikimedia/composer-merge-plugin" to execute code and wish to enable it now? (writes "allow-plugins" to composer.json) [y,n,d,?]
```
--------------------------------
### GET /app/{module}/edit/{id}
Source: https://laravelmodules.com/docs/12/resources/custom-module-generator
Displays the form for editing an existing module. Requires authentication.
```APIDOC
## GET /app/{module}/edit/{id}
### Description
Displays the form for editing an existing module.
### Method
GET
### Endpoint
`/app/{module}/edit/{id}`
### Parameters
#### Path Parameters
- **module** (string) - Required - The identifier for the module.
- **id** (integer) - Required - The ID of the module to edit.
### Response
#### Success Response (200)
- HTML form for editing the specified module.
```
--------------------------------
### Initialize Factory in Laravel Module Service Provider
Source: https://laravelmodules.com/docs/12/advanced/module-resources
This code sets up Laravel's factory instance within a module's service provider, specifying the directory where factories are located. This is necessary for using the factory pattern within your module. Assumes a module named 'Blog'.
```php
$this->app->singleton(Factory::class, function () {
return Factory::construct(__DIR__ . '/database/factories');
});
$this->app->singleton(Factory::class, function () {
return Factory::construct(__DIR__ . '/database/factories');
});
```
--------------------------------
### Configure Custom Livewire Modules
Source: https://laravelmodules.com/docs/12/resources/livewire
Shows how to configure custom modules in the `config/modules-livewire.php` file. This involves uncommenting and defining custom module configurations, including path and namespace. The configuration allows for specifying custom namespaces, view paths, and component registration names.
```php
/*
|--------------------------------------------------------------------------
| Custom modules setup
|--------------------------------------------------------------------------
|
*/
// 'custom_modules' => [
// 'Chat' => [
// 'path' => base_path('libraries/Chat'),
// 'module_namespace' => 'Libraries\\Chat',
// // 'namespace' => 'Http\\Livewire',
// // 'view' => 'Resources/views/livewire',
// // 'name_lower' => 'chat',
// ],
// ],
/*
|--------------------------------------------------------------------------
| Custom modules setup
|--------------------------------------------------------------------------
|
*/
// 'custom_modules' => [
// 'Chat' => [
// 'path' => base_path('libraries/Chat'),
// 'module_namespace' => 'Libraries\\Chat',
// // 'namespace' => 'Http\\Livewire',
// // 'view' => 'Resources/views/livewire',
// // 'name_lower' => 'chat',
// ],
// ]
```
--------------------------------
### Get Module Name in StudlyCase
Source: https://laravelmodules.com/docs/12/advanced/module-methods
Obtains the module's name formatted in StudlyCase (e.g., MyModuleName).
```php
$module->getStudlyName();
```
--------------------------------
### Caching Configuration (Deprecated)
Source: https://laravelmodules.com/docs/12/basic-usage/configuration
Shows the deprecated caching configuration options. These can be removed from the config file starting from v11.1.0.
```php
'cache' => [
'enabled' => false,
'driver' => 'file',
'key' => 'laravel-modules',
'lifetime' => 60,
],
```
```php
'cache-key' => 'activator.installed',
'cache-lifetime' => 604800,
```
--------------------------------
### Create New Module with Artisan
Source: https://laravelmodules.com/docs/12/resources/spatie-laravel-permission
Creates a new module directory and boilerplate files for a custom module using the Artisan command. This is useful for organizing features like roles and permissions within specific application modules.
```bash
php artisan module:make Roles
```
--------------------------------
### Running Vite Build and Development Commands
Source: https://laravelmodules.com/docs/12/basic-usage/compiling-assets
Provides the npm commands to compile and watch for changes in module assets using Vite. 'npm run build' compiles assets for production, while 'npm run dev' enables hot-reloading for development.
```bash
npm run build
```
```bash
npm run dev
```
--------------------------------
### Get Module Name in Snake Case
Source: https://laravelmodules.com/docs/12/advanced/module-methods
Fetches the module's name formatted in snake_case (e.g., my_module_name).
```php
$module->getSnakeName();
```
--------------------------------
### Get Data from module.json
Source: https://laravelmodules.com/docs/12/advanced/module-methods
Retrieves a specific piece of data from the module's `module.json` configuration file using a key.
```php
$module->get('description');
```
--------------------------------
### Get Data from composer.json
Source: https://laravelmodules.com/docs/12/advanced/module-methods
Fetches a specific attribute from the module's `composer.json` file using a key (e.g., 'name', 'version').
```php
$module->getComposerAttr('name');
```
--------------------------------
### Publish and Merge Configuration Files in Laravel Module
Source: https://laravelmodules.com/docs/12/advanced/module-resources
This snippet shows how to publish a configuration file to the Laravel config directory and merge it with existing configurations. It's essential for making module-specific settings available globally. Assumes a module named 'Blog'.
```php
$this->publishes([
__DIR__.'/../config/config.php' => config_path('blog.php'),
], 'config');
$this->mergeConfigFrom(
__DIR__.'/../config/config.php',
'blog'
);
$this->publishes([
__DIR__.'/../config/config.php' => config_path('blog.php'),
], 'config');
$this->mergeConfigFrom(
__DIR__.'/../config/config.php',
'blog'
);
```
--------------------------------
### Autoloading: Module composer.json for new modules
Source: https://laravelmodules.com/docs/12/getting-started/upgrade
Newly generated modules will have their `composer.json` files configured with specific PSR-4 autoloading paths, allowing classes to be loaded from a dedicated folder within the module.
```json
"autoload": {
"psr-4": {
"Modules\Blog\": "app/",
"Modules\Blog\Database\Factories\": "database/factories/",
"Modules\Blog\Database\Seeders\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Modules\Blog\Tests\": "tests/"
}
}
```
--------------------------------
### GET /{module} (API)
Source: https://laravelmodules.com/docs/12/resources/custom-module-generator
Fetches user information for an authenticated API user. This endpoint is part of the API routes and requires API authentication.
```APIDOC
## GET /{module} (API)
### Description
Fetches user information for an authenticated API user.
### Method
GET
### Endpoint
`/{module}`
### Parameters
#### Path Parameters
- **module** (string) - Required - The identifier for the module.
### Response
#### Success Response (200)
- **user** (object) - The authenticated user object.
#### Response Example
```json
{
"user": {
"id": 1,
"name": "John Doe",
"email": "john@example.com"
}
}
```
```
--------------------------------
### Run all PestPHP tests or filtered tests
Source: https://laravelmodules.com/docs/12/advanced/tests
Commands to run the entire PestPHP test suite or to filter tests by name or module using the `--filter` flag.
```bash
vendor/bin/pest
```
```bash
vendor/bin/pest --filter 'contacts'
```
--------------------------------
### Load Config and Service Provider in Lumen
Source: https://laravelmodules.com/docs/12/getting-started/lumen
This code demonstrates how to load the 'modules' configuration and register the Lumen service provider for the laravel-modules package within the 'bootstrap/app.php' file in a Lumen project.
```php
$app->configure('modules');
$app->register(
\Nwidart\Modules\LumenModulesServiceProvider::class
)
```
--------------------------------
### Get Extra Module Path
Source: https://laravelmodules.com/docs/12/advanced/module-methods
Constructs and returns a path to a subdirectory or file within the module, relative to the module's root directory.
```php
$module->getExtraPath('Assets');
```
--------------------------------
### Accessing Configuration with Custom Namespaces (PHP)
Source: https://laravelmodules.com/docs/12/basic-usage/custom-namespaces
Shows how to retrieve configuration values using the custom module namespace 'blog'. This facilitates module-specific configuration management.
```php
Config::get('blog.name')
```
--------------------------------
### Get Module Path
Source: https://laravelmodules.com/docs/12/basic-usage/helpers
Retrieves the absolute path to a specified module within the Laravel project. The function appends the module name to the base module directory.
```php
$path = module_path('Blog');
$path = module_path('Blog');
```
--------------------------------
### Show Module Model Attributes and Relations
Source: https://laravelmodules.com/docs/12/advanced/artisan-commands
Provides a detailed overview of a module's model, including its attributes and defined relations. This command is helpful for understanding the data structure and relationships within a module.
```php
php artisan module:show-model Blog
```
```php
php artisan module:show-model Blog
```
--------------------------------
### Create New Module with Vite Assets
Source: https://laravelmodules.com/docs/12/basic-usage/compiling-assets
This command generates a new module and sets up initial assets and the vite.config.js configuration file for it.
```php
php artisan module:make Blog
php artisan module:make Blog
```
--------------------------------
### Register Modules
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Registers all available modules.
```php
Module::register();
```
--------------------------------
### Get Module Path with Subpath
Source: https://laravelmodules.com/docs/12/basic-usage/helpers
Retrieves the absolute path to a specific file or directory within a module. It appends a given string to the module's base path.
```php
$path = module_path('Blog', 'Http/controllers/BlogController.php');
$path = module_path('Blog', 'Http/controllers/BlogController.php');
```
--------------------------------
### Configure Custom Module Directory
Source: https://laravelmodules.com/docs/12/advanced/publishing-modules
Defines a custom directory for installed modules within the `composer.json` file. This is used in conjunction with packages like `laravel-module-installer` to manage module placement.
```json
"extra": {
"module-dir": "Custom"
}
```
--------------------------------
### Publish Configuration and Stubs
Source: https://laravelmodules.com/docs/12/resources/laravel-module-generator
Publishes the configuration file and stubs for the module generator. This allows for customization of module templates and settings.
```bash
php artisan vendor:publish --provider="Dcblogdev\ModuleGenerator\ModuleGeneratorServiceProvider"
```
--------------------------------
### Modified Controller Stub Content (No Docblocks)
Source: https://laravelmodules.com/docs/12/resources/custom-module-generator
This is an example of a modified `controllers.stub` file where the docblocks have been removed. This custom stub can be used to generate controllers without documentation comments.
```php
validate([
'name' => 'required|string'
]);
{Model}::create([
'name' => $request->input('name')
]);
return redirect(route('app.{module}.index'));
}
public function edit($id)
{
${model} = {Model}::findOrFail($id);
return view('{module}::edit', compact('{model}'));
}
```
--------------------------------
### Include Files in Module Configuration
Source: https://laravelmodules.com/docs/12/basic-usage/creating-a-module
Demonstrates how to specify files to be included within the module's configuration using the 'files' array in module.json.
```json
{
"files": [
"start.php"
]
}
```
--------------------------------
### Create Livewire Component for Contacts Module
Source: https://laravelmodules.com/docs/12/resources/livewire
This command generates a new Livewire component named 'Feedback' within the 'Contacts' module. It creates the necessary class file and Blade view, along with a Livewire tag for embedding.
```bash
php artisan module:make-livewire Feedback Contacts
```
--------------------------------
### Register Multi-Word Livewire Component
Source: https://laravelmodules.com/docs/12/resources/livewire
Registers a Livewire component with a multi-word class name, using hyphens in the component name.
```php
Livewire::component('contacts::contact-form', ContactForm::class);
```
--------------------------------
### Livewire Component Blade View
Source: https://laravelmodules.com/docs/12/resources/livewire
This is the Blade view for the Livewire component. It contains simple HTML to confirm that the component is loaded and displays its origin module.
```html
The Feedback livewire component is loaded from the Contacts module.
```
--------------------------------
### Base Module Structure Overview
Source: https://laravelmodules.com/docs/12/resources/custom-module-generator
Provides a visual representation of the standard file and directory structure for a base module within the Laravel Modules package. This structure helps organize module-specific code.
```text
base-module
config
config.php
app/Http
Controllers
ModuleController.php
app/Models
Model.php
app/Providers
ModuleServiceProvider.php
RouteServiceProvider.php
database
factories
ModelFactory.php
migrations
create_module_table.php
seeders
ModelDatabaseSeeder.php
resources
assets
js
app.js
sass
views
create.blade.php
edit.blade.php
index.blade.php
routes
api.php
web.php
tests
Feature
ModuleTest.php
Unit
composer.json
module.json
package.json
vite.config.js
```
--------------------------------
### Autoloading: Configure `composer.json` with merge-plugin
Source: https://laravelmodules.com/docs/12/getting-started/upgrade
To enable automatic autoloading of module classes, configure the `extra.merge-plugin.include` section in your root `composer.json` file to include module composer files.
```json
"extra": {
"laravel": {
"dont-discover": []
},
"merge-plugin": {
"include": [
"Modules/*/composer.json"
]
}
},
```
--------------------------------
### Find Module or Throw Exception
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Finds a module by its name, returning the Module instance. Throws `Nwidart\Modules\Exeptions\ModuleNotFoundException` if the module does not exist.
```php
Module::findOrFail('module-name');
```
--------------------------------
### Import Facade
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Imports the Module facade for use in Laravel applications. This is a prerequisite for calling any module-related methods.
```php
use Nwidart\Modules\Facades\Module;
```
--------------------------------
### Publish module configuration files
Source: https://laravelmodules.com/docs/12/advanced/artisan-commands
Publishes the configuration files for a specific module. If no module is provided, it publishes all modules' configuration files.
```php
php artisan module:publish-config Blog
```
--------------------------------
### Call Macro from Module Repository
Source: https://laravelmodules.com/docs/12/advanced/facade-methods
Calls a previously added macro from the module repository.
```php
Module::hello();
```