目前,我正在使用Composer包创建一个RESTful API。我已经找到了“潜伏/餐厅”,这是一个非常强大的包,用于创建API。
但是我仍然在寻找一个包来管理我的数据库和一个认证系统。以下是我希望看到的一些要求/特性。
数据库管理器:
(例如
array(
'table1' => array(
array(
'row1'=> 'value1',
'row2'=> 'value2'
),
array(
'row1'=> 'value3',
'row2'=> 'value4'
)
)
)
将插入到table1 ('row1','row2')值(‘value4 1’,'value2'),(‘value4 3’,'value4')中
认证系统:
有人有什么建议吗?
发布于 2015-08-05 13:03:40
为了帮助您满足这种需求,我们在餐厅/应用包中创建了restler应用程序模板。每个分支对于不同的用例都有不同的模板,您需要的是在雄辩分支中。
您可以使用以下composer命令安装它
composer create-project restler/application=dev-eloquent my_api
一旦安装完成,您就可以进入目录。
cd my_api
首先,您需要编辑数据库配置文件(app/config/database.php
)。一旦您决定要使用哪个数据库(sqlite、mysql、postgres等),就可以在connections下更新相关信息。如果我想使用mysql,并且数据库名是my_api,用户名是root,密码是测试的,我的配置将类似于
<?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' => '',
),
),
);
注意:为了清晰起见,我已经从配置文件中删除了不相关的部分,您应该将它们保存在您的文件中。
配置好数据库后,可以使用以下命令检查连接
php artisan migrate:install
Migration table created successfully.
接下来,您将创建一个用于创建新表的迁移文件,那么创建一个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');
}
}
在这里,我们创建了具有名称、电子邮件和反馈列的反馈表。接下来,我们将运行迁移工具,以便创建这个表。
php artisan migrate
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command? yes
Migrated: 2015_08_05_120727_create_feedbacks_table
现在,我们可以使用以下命令生成一个模型类
php artisan model:make Feedback
Model created successfully.
Generating optimized autoload files
这里,我们将模型名指定为表名的单数版本。基于反馈表的表结构生成app/models/Feedback.php
。
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/并玩得开心:)
发布于 2015-08-05 07:17:26
(不是标牌)但是你可以试试内腔,它重量轻,可以做所有这些事情。您还可以找到rest引导包来启动应用程序。
https://stackoverflow.com/questions/31825740
复制相似问题