Software development with modules is essential if we are developing expandable software. In this repository i am gonna describe how i configure for the modules in laravel5.3 application. For this Sujip Dai and Jyot Helped me a lot. Let's get started !
Documentation for the framework can be found on the Laravel website.
Let's create the one directory for modules inside of root directory of Laravel. For example create "modules" directory inside of your project directory.
Now create the vendor directory and module name, For this create "lara" directory inside of modules directory and "cart" directory inside of "lara" directory.
We can create required directories inside of that cart directory. Let's Create the src folder and make cartServiceProvider.php class and write something like this type of code.
<?php
namespace Lara\Cart;
use Illuminate\Support\ServiceProvider;
class CartServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->registerIncludes();
$this->registerModelEvents();
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'cart');
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
$this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'cart');
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
public function registerIncludes()
{
foreach (new \DirectoryIterator(__DIR__ . '/../routes/') as $fileInfo) {
if (!$fileInfo->isDot()) {
include __DIR__ . '/../routes/' . $fileInfo->getFilename();
}
}
}
/**
* Register the Event Subscriber(Observer) for Models
*
* @return void
*/
public function registerModelEvents()
{
}
}
In config/app.php add the following line in providers array .
Lara\Cart\CartServiceProvider::class,
Now provide the path up to vendor inside of composer.json file of root folder, Which becomes like this
"autoload": {
"classmap": [
"database",
"modules/lara"
],
"psr-4": {
"App\\": "app/"
}
},
we can create composer.json file inside of cart directory, to make go up to your module and enter the command
composer init
Add your required information in this file and autoload your module using this line of code.
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"Lara\\Cart\\": "src/"
}
}
Create the routes directory inside of cart module and create the web.php file, In which write this route for testing purpose
Route::get('/test',function(){
return "This is Testing";
});
Now run the command below this, and if it work you can ensure that your process is right till now !
php artisan route:list
Now we have to make a controller and route will call the controller's method that's why make a route like this.
Route::get('/test',[
'as'=>'module.test',
'uses' => 'Lara\Cart\Http\Controllers\CartController@index'
]);
Inside of cart/src folder create the Http/Controllers/CartController.php and inside controller write the something like this type of code.
<?php
namespace Lara\Cart\Http\Controllers;
use App\Http\Controllers\Controller;
/**
* Class CartController
* @package Lara\Cart\Http\Controllers
*/
class CartController extends Controller
{
/**
* @return string
*/
public function index()
{
return view('cart::carts.index');
}
}
Here cart is the module name and carts is the directory inside of resources/views directory.
Let's make the layouts directory and master.blade.php file for the master view and write something like this
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
@yield('content')
</body>
</html>
In index.blade.php file which is situated inside of carts directory just add the content.
@extends('cart::carts.layouts.master')
@section('content')
<h1>Hello This is Content </h1>
@endsection
This is the simple flow of to make configure for the modules to develop the modular application. we can add the styles and javascript for each module. Again some stylesheet and javascript files are required for all modules for this we can make the common module which is the master for all the modules.
To add the migrations folder inside carts modules and run this command
php artisan make:migration --path=modules/lara/cart/database/migrations create_carts_table --create=carts
We are using Javascript file and scss file and compile that files using gulp, For this laravel-elixir need to be installed. For this install npm using the command
npm install
Create the assets folder inside of cart/resources folder and make two directories js and saas for javascript and scss file respectively.
I followed these links before starting this project
[http://laraveldaily.com/how-to-create-a-laravel-5-package-in-10-easy-steps/] (http://laraveldaily.com/how-to-create-a-laravel-5-package-in-10-easy-steps/) http://stackoverflow.com/questions/27661322/how-create-package-in-laravel-5 https://laracasts.com/discuss/channels/tips/developing-your-packages-in-laravel-5