php Laravel 接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30976140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:10:26  来源:igfitidea点击:

Laravel Interfaces

phplaravelinterfacenamespaces

提问by Hardist

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

我使用以下教程来了解接口:

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

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:

但是我想将我放置接口的目录更改为“App/Models/Interfaces”。所以我做到了。但现在我不能让它工作了。这是我的代码:

Routes.php

路由.php

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

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

Model Subaru.php

模型斯巴鲁.php

<?php

use App\Models\Interfaces\CarInterface;

class Subaru implements CarInterface {

..etc

Interface CarInterface

接口 CarInterface

<?php namespace App\Models\Interfaces;

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

I added this in my composer.json:

我在我的 composer.json 中添加了这个:

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

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

我什至在我的 start/global.php 文件中添加了这个:

ClassLoader::addDirectories(array(

app_path().'/models/interfaces',

回答by Safoor Safdar

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'.

在我最近的 laravel 5 项目中,我习惯于将我的逻辑准备为 Repository 方法。所以这是我当前的目录结构。例如,我们有“汽车”。

So first I just create directory call it libsunder appdirectory and loaded it to composer.json

所以首先我只是创建目录libsapp目录下调用它并将其加载到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.phpas interface.

之后我创建了一个子文件夹调用它Car。在 Car 文件夹下创建两个文件 'CarEloquent.php' 用于 eloquent 实现和CarInterface.php作为接口。

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

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

然后创建 Car Service Provider 来绑定 ioc 控制器。对于创建汽车服务提供者,您还可以使用 laravel 的 php artisan 命令。 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.phpprovider array.

最后一步是将这些服务提供config/app.php者添加到提供者数组中。

'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 redisyou just add another class CarRedisand change implementation file path from server provider.

CarEloqent 基本上可以帮助我们改进数据库实现,例如,将来如果您想为其他数据库实现相同的功能,就像redis您只需添加另一个类CarRedis并从服务器提供程序更改实现文件路径。

Update 1: Good Resource

更新一:好资源

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

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

[book] From Apprentice to Artisan by Taylor Otwell

[书] 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.

如果您仍然对实现这些行为有任何困惑,请告诉我,但是如果我发现一些要更改或更新的内容或根据要求,我会密切关注此问题以更新此答案。