Laravel REST API 和前端 [英] Laravel REST API and frontend

查看:54
本文介绍了Laravel REST API 和前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Laravel 中创建了一个小型数据库项目,并在 Laravel 中添加了 REST API 以将移动应用程序与数据库连接.我应该使用什么来从 Web 应用程序中的数据库获取数据?使用 Laravel 模型很容易,但这是创建另一个控制器来处理表单等而不是使用 rest api 控制器的好方法吗?谢谢

I created a project in Laravel, small database and added REST API in laravel to connect mobile app with database. What should I use to get data from database in web application? Using laravel models is easy but is that a good way to create another controllers to handle forms etc instead using rest api controllers? Thanks

推荐答案

Laravel 也以自己的方式支持 Restful API.为此

Laravel also support for Restful API in own way. for this

  • 您在 Api 文件夹中创建控制器:php artisan make:controller Api/TestController
  • 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 此命令在文件夹 app/Http/Resources/Api/Collections 中创建一个集合打开并更改 toArray($request) 函数并添加一个函数 with($request) 如下代码:

    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 :

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

  • 所以转到 TestController 并创建一个获取所有测试的方法:

  • so go to TestController and create a method for get all tests:

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

  • 这是返回一个包含测试数组的 json 对象.

    this is return a json object that contains array of tests.

    • 获取单个测试:php artisan make:resource Api/Resources TestResource

    然后转到 app/Http/Resources/Api/Collections 中的 TestResource 并更改代码如下:

    then go to TestResource in app/Http/Resources/Api/Collections and change code like following:

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

    所以转到 TestController 并为单个测试创建一个方法

    so go to TestController and create a method for single test

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

    这是laravel 中Rest API 的总结.希望你觉得有用

    this a summary of Rest API in laravel. Hope you find it useful

    这篇关于Laravel REST API 和前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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