使用laravel创建REST服务的问题

时间:2017-01-31 13:36:36

标签: laravel rest

我们正在尝试开发一款Android应用程序,该应用程序需要REST API才能显示来自Web服务器的数据。

我们尝试使用Laravel资源创建如下所示的REST服务:

Route::resource('list', 'ListController');


namespace App\Http\Controllers;
use mymodel
class ListController extends Controller
{
    public function getShow($id)
    {
        $Jsondata=list($id);
        return $JsonData;
    }

}

但它没有按预期工作需要一些令牌密钥或其他一些身份验证和授权需要知道如何设置。

2 个答案:

答案 0 :(得分:2)

We should utilize the Repository / Gateway design pattern: 

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 we 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 we need go here (getRecent, deleteWhere, etc) }

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 our User web controller. This controller talks to our 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.

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
    }}

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

 $this->app->bind(
        'lib\Repositories\UserRepository',       
        'lib\Repositories\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.

答案 1 :(得分:0)

第一个是what is not working?,如果你的意思是没有显示它的响应,因为你的功能实际上甚至不是去任何地方,而第二个是你Laravel的新手?

好的,如果这就是你所拥有的:

namespace App\Http\Controllers; 

use mymodel;

class ListController extends Controller 
{ 
    public function getShow($id) 
    { 
        $Jsondata=list($id); return $JsonData; 
    }
}

然后我可以安全地说很多事情都是错的。

根据我的理解,您在路径文件中使用Route::resource('list', 'ListController');创建showeditupdatedestroy路径,并期望看到这些功能(虽然我没有向核心证明这一点)但这就是我的理解。

所以你可以通过

做你的路线来开始laravel生活
Route::get('list', 'ListController@show')

然后将ListController更改为以下内容:

namespace App\Http\Controllers; 

use mymodel;

class ListController extends Controller 
{ 
    public function show($id) 
    { 
        $jsonData= ['my' => 'json data'];
        return $jsonData;
    }
}

如果这适合你,那么很酷的其他很多东西可能已经错误的设置。

  

注意:您可能需要花时间学习使用文档页面中的laravel。如果API是您的兴趣,请尝试DIngo api。还尝试学习使用标记:)

希望这会有所帮助:)