Wednesday, June 15, 2016

Creating a Modular Application in Laravel | Modul Structure in Laravel

Laravel Repository pattern


What is Moduler Application why we use?
Modular application means dividing  all your code in Different Module. In those Module are having Controller, Route, View, Model, Helper etc. just like your fresh laravel application installation file.

See the following Diagram How it organized:

pattern demo


The basic idea or importance of using this module architecture is diving the giant code into small meaning modules so that it helps to use your project more manageable and enjoyable.
Every Module contains Controller, Route, View, Model, Helper etc. So we can create number of modules in our application. There might be several ways to approach this, But I have used the following approach. Let's start ..

1.  First we need to install this package in your laravel Project : /L5Modular
    It's very easy process. If you can't don't hesitate to mention in comment. I will tell the process

2. Create a directory called Modules inside app directory. This directory will have separate folder for each of the modules. For instance there can be folder called User, one called Company etc. Let's create a folder inside this Modules folder name Course.

3. Now Let's create another three directory called Controllers, Views, Models and a file called routes.php inside that App\Modules\Course.

Actually there is nothing special in Module. Means routes.php is like normal routes.php file as we used in normal architecture in Laravel. What we need to focus mainly is namespacing.  We have to give proper namespacing to each controller/model that we created.

4. So final directory structure may be looked like the following Diagram :


app\
    Modules\
        Course\
            Controllers\
            Models\
            Views\
            routes.php
       

5. Now make Controller called CourseController.php inside the Controllers folder. Make a Model called Course.php. and Make a view page called courseSavePage.blade.php where I will load the form to store course data.
(Just check out whether your application's  .env file properly configured with database otherwise application will not run properly).

6. Here I'm posting what I wrote in  CourseController.php,  Course.php,  courseSavePage.blade.php, routes.php . What you need to focus is how i maintain the namespacing in those file. If you got the whole namespacing which i have used on those it will be easy for you to understand how actually that modular  structure works.

First Course.php:


<?php

namespace App\Modules\Course\Models;
use Illuminate\Database\Eloquent\Model;


class Course extends Model
  {
      protected $table='courses';
      protected $fillable = ['id','name','code'];
 
  }

Secondly CourseController.php: 




<?php

namespace App\Modules\Course\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Course\Models\Course;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;

class CourseController extends Controller
{
   public function getcourseSavePage()
   {     
     return view('Course::courseSavePage');
   }    
    public function postcourseSave(Request $request)
    {
      $this->validate($request,[
            'code' => 'required|unique:courses|max:5',          
            'name' => 'required|unique:courses'      
            
            ]);
            $course = new Course();           
            $course->code = $request->Input(['code']);            
            $course->name = $request->Input(['name']);                
            $course->save();            
        return redirect('courseSavePage');          
    }
}

Third one is courseSavePage.blade.php:



<html>
 
    <head>
      <title> Test Module</title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    </head>


 <body>
  <div class="container" >
   <h3> Create Course </h3>
 {!! Form::open(array('route' => 'saveCourse','class'=>'form-horizontal','method'=>'POST'))  !!}
    {!! Form::token(); !!}
    {!!   csrf_field() ; !!} 
   
     <div class="form-group">
       <label>Code</label>
       <input type="text" name="code" class="form-control" required placeholder="Code">
     </div>
        
     <div class="form-group">
       <label>Name</label>
       <input type="text" name="name" class="form-control" required placeholder="Name">
     </div>
     
     <button type="submit" class="btn btn-default">Submit</button>
  {!! Form::close() !!}
  </div>
 </body>
</html>

Fourth one is the routes.php:



<?php


Route::group(array('module' => 'Course','middleware' => 'web','namespace' => 'App\Modules\Course\Controllers'), function() {
Route::get('/courseSavePage',[
  'uses'=>'CourseController@getcourseSavePage',
  'as'=>'courseSavePage'
 ]);

Route::post('/saveCourse',[
  'uses'=>'CourseController@postcourseSave',
  'as'=>'saveCourse'
 ]);
});

7. So That's it if you access the url /courseSavePage program will load the blade view page and you can store the required data by using other route /saveCourse

That's it today. Hope you enjoy it and implement in your recent project. It will not only make your project manageable but also make it structured. So try to get used to it.

Thank you and stay tune for more next tutorial.















Related Articles

1 comment:

  1. Hi,
    as long as you have installed the Artem-Schander/L5Modular package anyway, you can fire the artisan comand "make:module course" from your projects root. So there is no need to create all the files and folders by hand ;)

    ReplyDelete