如何使一个Laravel REST API第一的Web应用程序 [英] How to make a REST API first web application in Laravel

查看:182
本文介绍了如何使一个Laravel REST API第一的Web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Laravel的API首次应用。我不知道什么是做到这一点的最佳方法,我将解释什么,我试图做的,但请随时给予解答如何做到这一点以不同的方式。

I want to make an API first application in Laravel. I don't know what is the best approach to do this, I will explain what I am trying to do, but please feel free to give answers how to do this in a different way.

我不希望被写在我的javascript前端所有与angular.js或类似的东西解析API的JSON输出。我希望我的Laravel应用程序产生HTML视图。我试图下井有两个控制器,一个在为API和一个用于网络的道路。为展示用户操作我的routes.php文件是这样的:

I don't want all my frontend to be written in javascript and parse the JSON output of the API with angular.js or something similar. I want my Laravel application to produce the HTML views. I am trying to go down the road of having two controllers one on for the API and one for the web. For the show User action my routes.php looks like this:

# the web controller
Route::controller('user', 'WebUserController');

# the api controller 
Route::group(array('prefix' => 'api'), function() {
    Route::resource('user', 'UserController');
});

所以 /用户将带我到 WebUserController / API /用户将带我到 UserController的。现在,我希望把我的所有逻辑API UserController的中,并调用它的行动从 WebUserController 。这里是code对他们俩的:

So /user will take me to WebUserController and /api/user will take me to the UserController. Now I want to put all my logic in the API UserController, and call its actions from the WebUserController. Here is the code for both of them:

class UserController extends BaseController 
{
    public function show($id)
    {
        $user = User::find($id);
        return Response::json(array('success'=>true,'user'=>$user->toArray()));
    }
}

class WebUserController extends UserController 
{
    public function getView($id) 
    {
         # call the show method of the API's User Controller
         $response =  $this->show($id);
         return View::make('user.view')->with('data', $response->getData());
    }
}

WebUserController 我能够获得与响应的JSON内容的getData(),但我我不能够得到头和状态code(它们得到保护照亮\\ HTTP \\ JsonResponse 的属性)。

In the WebUserController I am able to get the json content of the response with getData(), but I am not able to get the headers and status code (they are protected properties of Illuminate\Http\JsonResponse).

我认为我的做法可能不是最好的,所以我愿意接受建议,如何使这个应用程序。

I think that my approach might not be the best, so I am open to suggestions how to make this app.

修改:如何让头和响应的地位问题已经回答了德鲁·刘易斯,但我仍然认为,有可能是一个更好的方法如何设计这种

EDIT: The question how to get the headers and status of the response has been answered by Drew Lewis, but I still think that there might be a better way how to design this

推荐答案

您应该利用存储库/网关的设计模式:请参阅答案<一个href=\"http://stackoverflow.com/questions/18817615/managing-relationships-in-laravel-adhering-to-the-repository-pattern\">here.

You should utilize the Repository / Gateway design pattern: please see the answers here.

例如,使用用户模型打交道时,首先创建一个用户系统信息库。在用户存储库的责任是(执行CRUD操作)数据库进行通信。该用户系统信息库扩展一个共同的基础库,并实现了包​​含所有您需要的方法的接口:

For example, when dealing with the User model, first create a User Repository. The only responsibility of the user repository is to communicate with the database (performing CRUD operations). This User Repository extends a common base repository and implements an interface containing all methods you require:

class EloquentUserRepository extends BaseRepository implements UserRepository
{
    public function __construct(User $user) {
        $this->user = $user;
    }


    public function all() {
        return $this->user->all();
    }

    public function get($id){}

    public function create(array $data){}

    public function update(array $data){}

    public function delete($id){}

    // Any other methods you need go here (getRecent, deleteWhere, etc)

}

然后,创建一个服务提供商,结合你的用户信息库界面,您的口才用户库。每当您需要的用户信息库(由解决它通过IoC容器或注射在构造函数的依赖),Laravel自动为您刚刚创建的雄辩用户信息库的一个实例。这是这样,如果你改变奥姆斯比雄辩其他的东西,你可以简单地更改此服务提供商和其他任何更改到codeBase类是必需的:

Then, create a service provider, which binds your user repository interface to your eloquent user repository. Whenever you require the user repository (by resolving it through the IoC container or injecting the dependency in the constructor), Laravel automatically gives you an instance of the Eloquent user repository you just created. This is so that, if you change ORMs to something other than eloquent, you can simply change this service provider and no other changes to your codebase are required:

use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider {

    public function register() {
        $this->app->bind(
            'lib\Repositories\UserRepository',        // Assuming you used these
            'lib\Repositories\EloquentUserRepository' // namespaces
        );
    }

}

接下来,创建一个用户网关,谁的目的是为了跟任意数量的存储库,并执行应用程序的任何业务逻辑:

Next, create a User Gateway, who's purpose is to talk to any number of repositories and perform any business logic of your application:

use lib\Repositories\UserRepository;

class UserGateway {

    protected $userRepository;

    public function __construct(UserRepository $userRepository) {
        $this->userRepository = $userRepository;
    }

        public function createUser(array $input)
        {
            // perform any sort of validation first
            return $this->userRepository->create($input);
        }

}

最后,创建您的用户的网络控制器。该控制器会谈到你的用户网关:

Finally, create your User web controller. This controller talks to your User Gateway:

class UserController extends BaseController 
{
    public function __construct(UserGatway $userGateway)
    {
        $this->userGateway = $userGateway;
    }

    public function create()
    {
        $user = $this->userGateway->createUser(Input::all());

    }
}

通过构建这样的应用程序的设计,你会得到几个好处:你获得的关注非常清晰的分离,因为你的应用程序将秉承的单一职责原则(从你的数据库逻辑分离你的业务逻辑)。这使您可以在一个更容易的方式执行单元和集成测试,让你的控制器一样苗条越好,以及让您轻松换出雄辩任何其他数据库,如果你在未来的愿望。

By structuring the design of your application in this way, you get several benefits: you achieve a very clear separation of concerns, since your application will be adhering to the Single Responsibility Principle (by separating your business logic from your database logic) . This enables you to perform unit and integration testing in a much easier manner, makes your controllers as slim as possible, as well as allowing you to easily swap out Eloquent for any other database if you desire in the future.

例如,如果改变从雄辩到蒙戈,只需要改变的事情是服务提供商的结合,以及创造一个实现UserRepository接口的MongoUserRepository。这是因为存储库是说你的数据库中的的东西 - 它没有别的知识。因此,新MongoUserRepository可能看起来是这样的:

For example, if changing from Eloquent to Mongo, the only things you need to change are the service provider binding as well as creating a MongoUserRepository which implements the UserRepository interface. This is because the repository is the only thing talking to your database - it has no knowledge of anything else. Therefore, the new MongoUserRepository might look something like:

class MongoUserRepository extends BaseRepository implements UserRepository
{
    public function __construct(MongoUser $user) {
        $this->user = $user;
    }


    public function all() {
        // Retrieve all users from the mongo db
    }

    ...

}

和服务提供者现在将UserRepository接口绑定到新MongoUserRepository:

And the service provider will now bind the UserRepository interface to the new MongoUserRepository:

 $this->app->bind(
        'lib\Repositories\UserRepository',       
        'lib\Repositories\MongoUserRepository'
);

纵观所有网关你一直在引用UserRepository,所以通过这个变化就等于告诉Laravel,而不是使用旧的雄辩一个新MongoUserRepository。没有其他的变化是必需的。

Throughout all your gateways you have been referencing the UserRepository, so by making this change you're essentially telling Laravel to use the new MongoUserRepository instead of the older Eloquent one. No other changes are required.

这篇关于如何使一个Laravel REST API第一的Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆