使用 Restler 创建 RESTful API 时,我应该使用什么身份验证系统和数据库管理器?

What authentication system & database manager should I use when creating a RESTful API using Restler?

目前我正在使用 Composer 包创建 RESTful API。我已经找到了 "luracast/restler",这是一个非常强大的包,可以创建 API.

但我仍在寻找一个软件包来管理我的数据库和身份验证系统。这里有一些requirements/features我想看的

数据库管理员:

(例如

array(
 'table1' => array(
  array(
   'row1'=> 'value1',
   'row2'=> 'value2'
  ),
  array(
   'row1'=> 'value3',
   'row2'=> 'value4'
  )
 )
)

将变为 INSERT INTO table1 ('row1', 'row2') VALUES ('value1', 'value2'), ('value3', 'value4' ) )

认证系统:

有人有什么建议吗?

(不是一个包)但你可以试试 Lumen 它重量轻,可以做所有这些事情。您还可以找到其他 api bootstrap 包来启动您的应用程序

为了帮助您满足此类需求,我们在 restler/application package. Every branch in that has different template for different use cases, The one you need is in eloquent 分支中创建了 restler 应用程序模板。

您可以使用以下 composer 命令安装它

composer create-project restler/application=dev-eloquent my_api

安装完成后,您可以进入目录

cd my_api

首先您需要编辑数据库配置文件(app/config/database.php)。一旦决定了要使用的数据库(sqlite、mysql、postgres 等),请更新连接下的相关信息。如果我想使用 mysql 并且数据库名称是 my_api,用户名是 root,密码是 test,我的配置将如下所示

<?php

return array(
    'default' => 'mysql',
    'connections' => array(
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => '127.0.0.1',
            'database'  => 'my_api',
            'username'  => 'root',
            'password'  => 'test',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
);

Note:- I have removed irrelevant portions from the configuration file for clarity, you should keep them in your file

配置数据库后,您可以使用以下命令检查连接

php artisan migrate:install

Migration table created successfully.

接下来您将创建一个用于创建新 table 的迁移文件,创建一个 api 人们可以留下反馈的地方怎么样?

php artisan migrate:make --create=feedbacks create_feedbacks_table

Created Migration: 2015_08_05_120727_create_feedbacks_table
Generating optimized autoload files

编辑 app/database/migrations/2015_08_05_120727_create_feedbacks_table.php 文件以具有以下内容

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFeedbacksTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('feedbacks', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->text('message');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('feedbacks');
    }

}

我们在这里创建反馈 table,其中包含姓名、电子邮件和反馈列。接下来我们将 运行 迁移工具,以便创建此 table。

php artisan migrate

**************************************
*     Application In Production!     *
**************************************

Do you really wish to run this command? yes
Migrated: 2015_08_05_120727_create_feedbacks_table

现在我们可以用下面的命令生成一个模型class

php artisan model:make Feedback

Model created successfully.
Generating optimized autoload files

此处我们将型号名称指定为 table 名称的单数形式。 app/models/Feedback.php是根据反馈的table结构生成的table

restler 使用文件顶部的注释来了解模型公开了哪些属性

/**
 * Class Feedback
 *
 * @property-read  int    $id
 * @property       string $name
 * @property       string $email
 * @property       string $message
 * @property-read  string $created_at {@type date}
 * @property-read  string $updated_at {@type date}
 * 
 */

接下来让我们用以下内容创建app\controllers\Feedbacks.php

<?php

use Luracast\Restler\RestException;

class Feedbacks {
    /**
    * Get all feedbacks
    *
    * return array {@type Feedback}
    */
    public function index(){
        return Feedback::all();
    }

    public function get($id){
        if(!$feedback = Feedback::find($id)){
            throw new RestException(404, 'feedback not found');
        }
        return $feedback;
    }

    public function post(Feedback $feedback){
        $feedback->save();
        return $feedback;
    }

    public function delete($id){
        if(!$feedback = Feedback::find($id)){
            throw new RestException(404, 'feedback not found');
        }
        $feedback->delete();
        return ['success'=>true];
    }

}

接下来编辑 public/index.php 添加以下行

$r->addApiClass('Feedbacks');

就是这样,您可以使用

启动网络服务器
php artisan serve

Web app development server started on http://localhost:8000

将您的网络浏览器指向 http://localhost:8000/explorer/ 并玩得开心:)