Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Interfaces

I used the following tutorial to get an idea about interfaces:

http://vegibit.com/what-is-a-laravel-interface/

But I wanted to change the directory of where I am putting my interfaces to "App/Models/Interfaces". And so I did. But now I cannot get it to work anymore. Here is my code:

Routes.php

App::bind('CarInterface', 'Subaru');

Route::get('subaru', function()
{
$car = App::make('CarInterface');  
$car->start();
$car->gas();
$car->brake(); 
});

Model Subaru.php

<?php

use App\Models\Interfaces\CarInterface;

class Subaru implements CarInterface {

..etc

Interface CarInterface

<?php namespace App\Models\Interfaces;

interface CarInterface {
public function start();
public function gas();
public function brake();
}

I added this in my composer.json:

"psr-0": {
"Interfaces": "app/models/interfaces"
}

And I even added this in my start/global.php file:

ClassLoader::addDirectories(array(

app_path().'/models/interfaces',
like image 842
Hardist Avatar asked Jun 22 '15 09:06

Hardist


People also ask

What are PHP interfaces?

A PHP interface defines a contract which a class must fulfill. If a PHP class is a blueprint for objects, an interface is a blueprint for classes. Any class implementing a given interface can be expected to have the same behavior in terms of what can be called, how it can be called, and what will be returned.

What is Laravel service container?

The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection. Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods.

What are contracts in Laravel?

Laravel's Contracts are a set of interfaces that define the core services provided by the framework. For example, a Queue contract defines the methods needed for queueing jobs, while the Mailer contract defines the methods needed for sending e-mail.

What is a interface?

In English, an interface is a device or system that unrelated entities use to interact.


1 Answers

In my recent laravel 5 project, I'm used to prepare my logics as Repository method. So here's my current directory structure. For example we have 'Car'.

So first I just create directory call it libs under app directory and loaded it to composer.json

"autoload": {
        "classmap": [
            "database",
            "app/libs" //this is the new changes (remove this comment)
        ]
    }

after that I create a subfolder call it Car . Under the Car folder create two file 'CarEloquent.php' for eloquent implementation and CarInterface.php as interface.

CarInterface

namespace App\libs\Car;
interface CarInterface {
    public function getAll();
    public function create(array $data);
    public function delete($id);
    public function getByID($id);
    public function update($id,array $data);
}

CarEloquent

namespace App\lib\Car;

use App\lib\Car\CarInterface;
use App\Car; //car model
class CarEloquent implements CarInterface {
    protected $car;

    function __construct(Car $a) {
        $this->car = $a;
    }
    public function getAll(){
        return $this->car->all();
    }
}

Then create Car Service Provider to bind ioc controller. For create Car service provider you can also use php artisan command by laravel. php artisan make:provider CarServiceProvider

ServiceProvider

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class CarServiceProvider extends ServiceProvider {
   public function register() {
        $this->app->bind('App\lib\Car\CarInterface', 'App\lib\Car\CarEloquent');
    }

}

And final step would be add these service provider to config/app.php provider array.

'providers' => [
  'App\Providers\CatServiceProvider',
]

And finally we are ready to use our repository method in our controller.

Example Controller

namespace App\Http\Controllers;
use App\lib\Car\CarInterface as Car;
class CarController extends Controller {
    protected $carObject;
    public function __construct(Car $c) {
        $this->carObject = $c;
    }
    public function getIndex(){
        $cars = $this->carObject->getAll();
        return view('cars.index')->with('cars',$cars);
    }
}

Main purpose to achieve here call repository method to controller, however you need use them as per your requirement.

Update

CarEloqent basically help us to improve database implementation, for example in future if you want to implement same functionality for other database like redis you just add another class CarRedis and change implementation file path from server provider.

Update 1: Good Resource

http://programmingarehard.com/2014/03/12/what-to-return-from-repositories.html

[book] From Apprentice to Artisan by Taylor Otwell

Very good explanation about repository method and software design principle commonly called separation of concerns. You should read this book.

If you still have any confusion to achieve these behaviors let me know and however I will keep eye on this question to update this answer, if I find some things to change or update or as per requirement.

like image 141
Safoor Safdar Avatar answered Oct 21 '22 15:10

Safoor Safdar