Dependency Injection Laravel 5

In laravel 5, We can inject services or classes. These dependency injection techniques are very useful when you 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 your 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 you 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;
    }
 	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
	$allBooks = $this->repo->all();
        return View('books.bookList', compact('allBooks'));
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store(PublishBookRequest $requestData)
	{
        $this->repo->insertData($requestData);
        return redirect()->route('book.index')->with('message', 'New record has been added!');
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id, PublishBookRequest $requestData)
	{
        $this->repo->updateData($id, $requestData);
        return redirect()->route('book.index')->with('message', 'Record has been updated!');
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
    {
        $this->repo->deleteData($id);
        return redirect()->route('book.index')->with('message', 'Record deleted successfully!');
    }
}


 

Something to say? Tell us in comment section.