lumen Rest API 起步

修改项目文件

.env

DB_DATABASE=<数据库名>
DB_USERNAME=<数据库用户名>
DB_PASSWORD=<数据库密码>

bootstrap/app.php

$app->withFacades();
$app->withEloquent();

数据库迁移

创建数据表

php artisan make:migration create_table_users --create=users

定义数据表

database/migrations/迁移文件

Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
});

运行迁移

php artisan migrate

创建模型

接下来我们在app目录下创建模型文件User.php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable; class User extends Model implements AuthenticatableContract, AuthorizableContract
{
use Authenticatable, Authorizable; /**
* The attributes that are mass assignable.
*
* @var array
*/
protected $table = 'users'; protected $fillable = [
'id', 'name',
]; /**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = []; public $timestamps = false;
}

创建控制器

然后创建控制器文件app/Http/Controllers/UserController.php

namespace App\Http\Controllers;

use App\User;
use DB;
use Illuminate\Http\Request; class UserController extends Controller
{
public function createUser(Request $request)
{
$user = User::create($request->all());
return response()->json($user);
} public function updateUser(Request $request,$id)
{
$user = User::find($id);
$user->name = $request->input('name');
$user->save(); return response()->json($user);
} public function deleteUser($id)
{
$user = User::find($id);
$user->delete(); return response()->json('删除成功');
} public function index($id = null)
{
if (!empty($id)) {
$users = User::find($id);
}else{
$users = User::all();
}
return response()->json($users);
} public function hello()
{
return 'hello';
}
}

定义路由

修改文件bootstrap/app.php

$app->router->group([
'namespace' => 'App\Http\Controllers',
], function ($router) {
require __DIR__.'/../routes/web.php';
require __DIR__.'/../app/Http/routes.php';
}); return $app;

打开app/Http/routes.php并添加路由

$router->get('/hello', array(
'uses' => 'UserController@hello'
)); $router->group(['prefix' => 'api'], function() use ($router){
$router->post('person', 'UserController@createUser');
$router->put('person/{id}','UserController@updateUser');
$router->delete('person/{id}','UserController@deleteUser');
$router->get('person[/{id}]','UserController@index');
});

测试API

curl -i -X POST -H "Content-Type:application/json" http://www.lelumen.test/api/person -d '{"id":2,"name":"test1"}'
curl -i -X POST -H "Content-Type:application/json" http://www.lelumen.test/api/person -d '{"name":"test22"}' curl -H "Content-Type:application/json" http://www.lelumen.test/api/person/1 -X PUT -d '{"name":"ttt"}' curl -H "Content-Type:application/json" http://www.lelumen.test/api/person -X GET curl -H "Content-Type:application/json" http://www.lelumen.test/api/person/1 -X GET curl -X DELETE http://www.lelumen.test/api/person/1

空格引起的奇葩,阿哈哈

参考文件

最新文章

  1. Python爬虫爬取豆瓣电影名称和链接,分别存入txt,excel和数据库
  2. AAS代码运行-第11章-1
  3. html5 svg 圆形进度条
  4. [CareerCup] 13.10 Allocate a 2D Array 分配一个二维数组
  5. UPDATE语句:将一个表里的字段更新到另一个表的字段里的语句
  6. yii 验证器和验证码
  7. 计算机与ARM板通过路由器相连
  8. How to change a product dropdown attribute to a multiselect in Magento
  9. jquery 变量和原生js变量的关系
  10. mybatis源码解读(四)——事务的配置
  11. 吐血记录微信小程序授权获取Unionid及linux下使用bouncycastle解密用户数据 遇到的坑
  12. jeecg自定义datagrid查询
  13. JS prototype 生成机制
  14. TestLink测试管理工具的使用举例—第一篇
  15. sdn的相关学习系列之一mininet的安装
  16. Given d and e, factorize N to attack RSA
  17. 【lazy标记得思想】HDU3635 详细学习并查集
  18. day111 爬虫第一天
  19. [Todo]很不错的Java面试题类型整理,要看
  20. 【转】Android 之ActivityThead、ActivityManagerService 与activity的管理和创建

热门文章

  1. Prometheus时序数据库-磁盘中的存储结构
  2. 215. 数组中的第K个最大元素 + 快速排序 + 大根堆
  3. 数据采集组件:Flume基础用法和Kafka集成
  4. Java I/O流 04
  5. 2020年12月-第02阶段-前端基础-CSS Day04
  6. 如何使用excel制作查分系统
  7. CentOS 8.3安装MySQL 8.0.21后无法登录管理数据库
  8. bjd_ctf
  9. Python读写配置文件模块--Configobj
  10. python网络编程TCP服务多客户端的服务端开发