Saturday, July 16, 2016

PHP Dependency Injection with Laravel 5

Hi
Today, we will learn dependency injection in laravel.
In laravel 5, We can inject services or classes. These dependency injection techniques are very useful when we generating a large scale projects. Class dependencies are injected into the class through the constructor or setter methods.
Dependency solutions through service container simplifies the dependency injection logic for our controller by configuring the container. This really becomes crystallise if one class depends on a service, that service depends on other services and those services could also depend on more services, the container will manage that dependency results.
Example of container App/Repositories/BookDbRepository
<?php namespace App\Repositories;
use App\Book;
class BookDbRepository {
    public function all(){
       return Book::all();
    }
    public function insertData($requestData){
        $book = new Book;
        $book->title= $requestData['title'];
        $book->description= $requestData['description'];
        $book->author= $requestData['author'];
        $book->save();
    }
    public function updateData($id, $requestData){
        $book = Book::find($id);
        $book->title = $requestData['title'];
        $book->description = $requestData['description'];
        $book->author = $requestData['author'];
        $book->save();
    }
    public function deleteData($id){
        Book::find($id)->delete();
    }
}
Into BookDbRepository we can see 4 methods are specifies show, insert, update and delete. Where container has all power to change the database. Unfortunately, controller will talk to BookDbRepository for every new query requests of application.
After early changes, Lets see what effects has been done in our controller BookController class.
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Repositories\BookDbRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
class BookController extends Controller {
    protected $repo;
    public function __construct(BookDbRepository $repo)
    {
        $this->repo = $repo;
    }
public function index()
{
$allBooks = $this->repo->all();
        return View('books.bookList', compact('allBooks'));
}
public function store(PublishBookRequest $requestData)
{
        $this->repo->insertData($requestData);
        return redirect()->route('book.index')->with('message', 'New record has been added!');
}
public function update($id, PublishBookRequest $requestData)
{
        $this->repo->updateData($id, $requestData);
        return redirect()->route('book.index')->with('message', 'Record has been updated!');
}

public function destroy($id)
    {
        $this->repo->deleteData($id);
        return redirect()->route('book.index')->with('message', 'Record deleted successfully!');
    }
}
Clearly we can see that in constructor of class BookController , we have injected the instance of BookDbRepository class.
Thanks

1 comment:

  1. Hey! Amazing work. If you are searching for genuine QuickBooks Phone Number dial Quickbooks Customer Service Number 1-855-756-1077 for immediate help.

    ReplyDelete