简体   繁体   English

Laravel REST API 和前端

[英]Laravel REST API and frontend

I created a project in Laravel, small database and added REST API in laravel to connect mobile app with database.我在 Laravel 中创建了一个小型数据库项目,并在 Laravel 中添加了 REST API 以将移动应用程序与数据库连接。 What should I use to get data from database in web application?我应该使用什么来从 Web 应用程序中的数据库获取数据? Using laravel models is easy but is that a good way to create another controllers to handle forms etc instead using rest api controllers?使用 Laravel 模型很容易,但这是创建另一个控制器来处理表单等而不是使用 rest api 控制器的好方法吗? Thanks谢谢

Laravel also support for Restful API in own way. Laravel 也以自己的方式支持 Restful API。 for this为了这

  • you create your controller in Api folder: php artisan make:controller Api/TestController你在 Api 文件夹中创建你的控制器: php artisan make:controller Api/TestController
  • define your routes in routes/api.php :routes/api.php定义你的路由:

     Route::group(['namespace' => 'Api'], function (){ Route::group(['prefix' => '/test'], function () { Route::get('/', 'TestController@list); Route::get('/single', 'TestController@single'); }); });
  • create a resource collection for data that is an array of collection为作为集合数组的数据创建资源集合

    php artisan make:resource Api/Collections TestCollection this command create a collection in folder app/Http/Resources/Api/Collections open in and change toArray($request) function and add a function with($request) like following code : php artisan make:resource Api/Collections TestCollection此命令在文件夹app/Http/Resources/Api/Collections创建一个集合,打开并更改toArray($request)函数并添加一个函数with($request)如下代码:

     public function toArray($request) { return $this->collection->map(function ($item){ return [ 'id' => $item->id, // $item is instance of Test model 'name' => $item->name, 'description' => $item->description, ]; }); } public function with($request) // optional : this method return with of response { return [ 'status' => true ]; }
  • so go to TestController and create a method for get all tests:所以去 TestController 并创建一个获取所有测试的方法:

     public function list() { $tests = Test::all(); // your Test Model return new TestCollection($test); // TestCollection you created above }

this is return a json object that contains array of tests.这是返回一个包含测试数组的 json 对象。

  • for get a single test : php artisan make:resource Api/Resources TestResource获取单个测试: php artisan make:resource Api/Resources TestResource

    then go to TestResource in app/Http/Resources/Api/Collections and change code like following:然后转到app/Http/Resources/Api/Collections TestResource 并更改如下代码:

     public function toArray($request) { return [ 'id' => $this->id, 'name' => $this->name, // $this is instance of Test model 'description' => $this->description, 'body' => $this->body, 'diff_name' => $this->name_in_table // you can change the name differ from name in model instance ]; }

so go to TestController and create a method for single test所以去 TestController 并为单个测试创建一个方法

public function single(Request $request)
{
    $test = Test::findOrFail($request->id);
    return new TestResource($test);
}

this a summary of Rest API in laravel.这是 laravel 中 Rest API 的总结。 Hope you find it useful希望你觉得它有用

With laravel, you can reuse your api endpoints by taking advantage of the CreateFreshApiToken middleware .使用 laravel,您可以利用CreateFreshApiToken中间件来重用您的 api 端点。

Then you will only need to create new controllers and methods to display views.然后你只需要创建新的控制器和方法来显示视图。 All of the CRUD stuff can be reused.所有 CRUD 的东西都可以重用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM