Understand Basic Of Laravel Contracts

Understand Basic Of Laravel Contracts


  • Creator of Laravel, Taylor Otwell brings awesome features in Laravel 5 called Contracts.
  • Contracts are a set of interfaces which helps a developer to make code loosely coupled base OR in another term we can say Contracts are interfaces that provide core services.
  • So in common, Laravel Contracts is a wrapper of different interfaces that property of `illuminate/contracts`.
Now what are the ‘Facades’?:
Facades also interface and like a Contracts so let’s first understand what are the Facades.
  • Do they have any similarity or relationship or anything else? Let’s understand something about Facades first.
  • Facades are also static interfaces that provide access methods to the classes of service container.
  • We can use Facades `type­hint` like we already implement the route.
Route::bind('mystore', function ($id){
 return App\Store::get();
In the above code, Facades solved the contracts out of service container. But the quality of Contracts is we can define conspicuous dependencies for your classes and make your application more loosely coupled. Here Facades work fine but in some case, we want to create something extra, you need to use Contracts.
How to write?
Contracts implementation is very simple. Let’s understand the Contracts implementation with sample example:
Suppose I have a ‘StoreController’ with that I will maintain my all store list and for that, I need insert these data into the database. So what I will do, I’ll create an eloquent model ‘Store’ and bind it with a route into route.php.
After that, I will use that model ‘Store’ all CRUD operations using the resource. In between, first I check whether the user is logged in OR not? If logged in then only can able act on model CRUD otherwise not. Example code:
public function insert(Request $request)
  if (Auth::check()) {
     // If user is logged in...
From the above code, I used Facades `Auth` to check the user is logged in OR not? So here if I don’t want more decoupled state it works fine and it’s what? code with SOLID principle and here Contracts comes for the solution. For that what I do? just inject ‘Auth’ Facade through constructor:
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard as Auth;

class StoreController extends Controller
     * Display a listing of the resource.
     * @return Response
    protected $auth;
    public function __construct(Auth $auth) {//Inject `Auth` Facade...
     $this­->auth = $auth;    

    public function insert(Request $request)
As you see, when we use this line of code in our ‘StoreController’: use Illuminate\Auth\Guard as Auth;`
We actually inject an instance based on concretion, not an abstraction. Whenever we call any methods in this instance it is aware of your framework. But we need to make it completely unaware of our Framework and become loosely coupled.
To do that we just have to change one line of code in our ‘StoreController’ instead of that line of code just change `use Illuminate\Auth\Guard as Auth;` to `use Illuminate\Contracts\Auth\Guard as Auth;` and do not need to make change the store() method. This is what loosely coupled provided by Contracts.
When to use?
  • In some situation, many developers have use facades and others have use contracts and its depends on developers logic and them test and like as developer teams. contracts and facades both are used to create Laravel applications.You will look few practical differences when your class keeping responsibilities.
  • In some case, code requirement is changed to use some specific code/package/service what we do is? we need to change the controller code. but this is the correct way? No. because this will consume our time to re-code that. But is we use that contract and implement that method like service which adheres to the contract and implements those methods. Let’s understand with an example to more clarity:
I have created a `ShippingServiceContract` with methods like `getShippingRegions()`, `getShippingCost()`, `getShippingTrackStatus()`…etc like all basic methods.And create `BludartDeliveryService` which again adhere to Contract and implements those methods.
Now I will use this `ShippingServiceContract` Contract by injecting it into my controller which is called dependency injection and start using it. After a few days, I think, I don’t want to use Bludart and for replacement, I need to use some other shipping provider. So now what I need to do is, just create `SomeOtherDeliveryService` that also adhere to Contract and change binding from `SomeOtherDeliveryService` to `BludartDeliveryService` and no need to change the controller code that’s it.
For the binding between contract and implementation, we can use the service provider such as `app/Providers/AppServiceProvider.php`
The Laravel Contracts package provides a special way to extend Laravel core components explicitly and provides a stable API for developers to access the framework’s behaviour.

Hope this blog is quickly and easily guide for Laravel Contracts. Need more assistance regarding Laravel Web Development Services Contact us now!


How To Create Laravel Basic Package



Here we are discussing and learn about ‘How to create Laravel basic package’. Hope you are aware of Laravel, don’t worry if not, you can learn the basic of Laravel from Laravel official site.

I tried to explain these things in a basic term so anybody can easily implement the basic skeleton of Laravel package.

Let’s we start point by point:

Every laravel package have common folder structure like: <vendorname>/<packagename>. You can create own folder ‘packages’ at root level OR you can create your folder under ‘vendor’ folder. I’ll use ‘vendor’ folder for this explanation.

  • Create package folder under ‘vendor’ directory(Folder name format: <vendor>/<vendorname>/<packagename>)

Here,  <vendorname> could be your name OR your company name. (I.e. addweb/sample)

  • Run composer init Command at after navigating to this folder in CLI and answer the composer questions for the first setup and create src folder like vendor/addweb/sample/src

At this point, a composer will gather basic require information about your package, create composer.json and put that information into it.

  • For local testing add this package path into app root composer.json under autoload[] like:
"psr-4": {
                "App\\": "app/",
                "Addweb\\Sample\\": "vendor/addweb/sample/src"

With this step, your package will get loaded using PSR-4 autoloading but for that, we need tell composer using the command, we will check it with next step.
NOTE: Step for package submit to packagist.org. We will discuss in next blog on How to deploy our package at packagist.org.

  • Autoload this package using composer dump-autoload at root folder CLI

By run above autoload command, we actually tell Composer to autoload the all require things from composer.json and looks for all of the classes it needs to include.

Laravel official document say’s: “Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel’s core services, are bootstrapped via service providers”.

Means that service provider is we can say the heart of the application. So lets we move forward with creating our package service provider.

  • Create service provider using PHP artisan make: provide sample service provider at root folder CLI

As per this point, your package provider is generated with the basic skeleton, you can put some extra ingredient as per your requirement. But before it we need to move this provider file into our package folder: vendor/addweb/sample/src/sampleServiceProvider.php

  • Open that moved file and change namespace like namespace addweb\sample;

By default created skeleton need to change namespace with actual one so that composer will hook our provider class using the PSR-4 autoloading standard

Now let’s register our service provider with Laravel application.

  • Register our service provider into config/app.php into providers[] array. Here we need to add our service provider into providers[] like:
'providers' => [

   * My Package Service Providers...




Now, let create and register our testing route:

  • Create new routes.php into our package src folder to handle our package used the route and register that routes.php into our service provider boot() to initiate our package used routes:

	Route::get(‘firstroute’, function(){
	echoPrint from package’;

Public function boot(){
	Include __DIR__.’/routes.php’;


Now our basic package is ready to server. Just Run PHP artisan serv at the root of your application folder and open that URL into your browser.

If you are using localhost then you can check link like localhost:8000/firstroute at your browser address bar.  Hope this helps you well, feel free to add your comments/feedbacks. Need more assistance regarding Laravel Development Service Get in touch today!

Original Posted: AddWeb Solution


AdminLTE Implement Skills In Laravel 5
Let me walk you through AdminLTE Introduction:

  • AdminLTE is a popular open source WebApp template for admin Dashboard and control panel.It is responsive template using Bootstrap 3 and CSS framework.
  • AdminLTE is now used by thousands of users around the world.
  • AdminLTE is a based on modular design and it easily customizes and built upon.
  • This package is switched default laravel scaffolding / boilerplate to adminLTE template.
  • In laravel AdminLTE is managed by “ Bower “.

Feature of AdminLTE:

  • AdminLTE provides Sidebar mini feature to the user with the ability to collapse the sidebar without losing accessibility to side navigation.
  • Control sidebar is a new section of the layout. This sidebar use for many purposes in right sidebar
    • AdminLTE provides the 6 new skin originally. That switch the color of sidebar in dashboard.

Integrate AdminLTE:

  • If you have integrated AdminLTE so first you have to create a new project using laravel because it is easy to use and more comfortable.
  • First, You have to use laravel installer, In the terminal type the command.

composer global require “laravel/installer=~1.1”

  • Here, this command will install composer in few minutes. Then get the fresh copy of laravel using below command.

laravel new myapp

  • Here,  we create new laravel copy and its name is myapp.
  • Laravel setup is completely installed in your system then you have able to see the welcome page on your site.
  • Once, it’s process is done then after you have navigated to the public directory in a terminal command.
  • After you have navigated to the public directory you have to install AdminLTE using Bower

    bower install admin-lte

  •  Once, you have to successfully installed adminLTE in laravel then you should have folder called bower_componets and inside it you will find admin-lte.
  • Laravel is provide the greate templating system out of the box called Blade. Then after we have to convert the regular HTML page to Blade.
  • First you have to create a new blade in “ resources/view “ directory and then you have to create route for this blade.
  • If you have to create the route and blade then you will copy the “ bower_components/admin-lte/starter.html ” in new view and modify the code. Then after you will visit the page so it has blank.
  • Then after you will split the template into the three files like sidebar.blade.php, header.blade.php, footer.blade.php . so you have create new blade in view directory.
  • Then after you have create a new controller using the controller command

    php artisan make:controller TestController –plain

  • After creating controller you will do work in controller and you will create the new route for this controller

    Route::get(‘test’, ‘TestController@index‘);

  • After creating route you will check your test page so you will display the output for your developing view.

Install AdminLTE template:

  • A laravel 5 package that switch the default  Laravel scaffolding / boilerplate to adminLTE template with bootstrap 3.0.
  • AdminLTE is the so simple to install and use.
  • AdminLTE is install globally with compose

       composer global require “acacha/adminlte-laravel-installer=~2.0”

  • And convert any fresh laravel installation to AdminLTE to use with:-

    laravel new laravel-with-admin-lte cd laravel-with-admin-lte adminlte-laravel install

  • It successfully installed so you can use AdminLTE if you are use llum.
  • You are start to using AdminLTE project. Llum will be configure database (sqlite), execute migrations , install devtools and serve for you to use it.

Hope this helped you to get most out of the system. Feel free to share your reviews and need assistance for Hire Laravel Developer.

Understand The Implementation of REST API In Laravel 5

In this blog, we are going to demonstrate the understanding to implement REST API in Laravel 5. Starting with some quick introduction part;
  • Rest API is backend implementing also it is a simple basic HTTP authentication to store data for different users of the application.
  • We will use the middleware to provide the authentication for the rest call.
  • The middleware will authorize the user at every request without storing in session or using token.
Implement REST API:
composer create-project restAPP –prefer-dist
  • This terminal command to we create the new application and that name is restAPP.
  • Then after we create the model for basic authentication and we are using the email for login parameter.
  • Complete the project creation after we have created the controller and model, using artisan command.
    php artisan make:model Todo
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\User;
class Todo extends Model {
 protected $fillable = ['title','isDone'];
    public function user(){
        return $this->belongsTo('App\User');
In the model we have to add the reference to the user model and method to retrieve it.
    php artisan make:controller TodoController
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Todo;
use Auth;
use Request;
class TodoController extends Controller {
    * Display a listing of the resource.
    * @return Response
   public function index() {
         $todos = Todo::where('user_id','=',Auth::user()->id)->get();
     return $todos;
    * Store a newly created resource in storage.
    * @return Response
   public function store() {
      $todo = new Todo(Request::all());
      $todo->user_id = Auth::user()->id;
      return $todo;
In controller, we have to add a basic method to create and retrieve data from the logged users.
It’s all complete then we have to create a middleware to for authentication for possible to access data only if the user passes in the HTTP request his email and password. Let’s create middleware.
    php artisan make:middleware simpleAuthMiddleware
<?php namespace App\Http\Middleware;
use Closure;
class SimpleAuthMiddleware
    * Handle an incoming request.
    * @param  \Illuminate\Http\Request $request
    * @param  \Closure $next
    * @return mixed
   public function handle($request, Closure $next)
      return Auth::onceBasic('username') ?: $next($request);
After creating middleware we have to register in “ kernel.php “ file and give the key for easily use this middleware.
‘simpleauth’ => ‘App\Http\Middleware\SimpleAuthMiddleware’, After register this middleware we have to create the route path for Todo Controller and middleware
Route::get('api/todo', ['uses' => 'TodoController@index','middleware'=>'simpleauth']);
Route::post('api/todo', ['uses' => 'TodoController@store','middleware'=>'simpleauth']);
If you try with the postman on every request of email and password will be prompted. You will also pass this info on URL or HTTP header.

Hope this helped you to get most out of the system. Feel free to share your reviews or need assistance for Hire Laravel Developer then get in touch with us. Pick the best answer for your requirements.