lumen Rest API 起步
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
空格引起的奇葩,阿哈哈
参考文件
- https://lumen.laravel.com/docs/5.4/database
- https://learnku.com/docs/laravel/5.5/migrations/1329#creating-columns
- https://xueyuanjun.com/post/6449.html
- https://www.jianshu.com/p/1fe2a05451bc
lumen Rest API 起步的更多相关文章
- lumen 构建api(dingo api)
什么是 API API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力, ...
- 用lumen构建API的相关流程
概述 Lumen是一个基于Laravel的微框架,主要用于小型应用和微服务,专注于性能和速度的优化,该框架一个重要的应用就是构建 RESTAPI. 为什么用Lumen构建REST API Lumen访 ...
- Lumen开发:结合Redis实现消息队列(3)
4.运行队列监听器 开启任务监听器 Lumen包含了一个Artisan命令用来运行推送到队列的新任务.你可以使用queue:listen命令运行监听器: php artisan queue:liste ...
- dingo/api 使用 知识
Dingo 能为Laravel提供一整套包括从路由,到认证的RESTful API开发工具 Laravel & Lumen RESTFul API 扩展包:Dingo API(一) —— 安装 ...
- 使用HTML5地理位置定位到城市的方法及注意事项
介绍 本文将简述一下如何通过HTML5和百度地图开放平台提供的API来实现对浏览器的定位.实现效果为显示出用户所在的省市,即: XXX省 XXX市. 实现思路 利用HTML5 提供的API获取到用户的 ...
- Canvas游戏计算机图形教程
TechbrooD 主站 WOW 登录 注册 0首页 1简介 1.1WWW 技术变迁和生态 1.2WWW 学习建议 1.3WWW 互联网基础知识 1.4WWW Web 1.5 WWW Web ...
- DirectX API 编程起步 #01 项目设置
=========================================================== 目录: DirectX API 编程起步 #02 窗口的诞生 DirectX A ...
- lumen 使用 dingo API 在 phpunit 中 404 的解决方法, 以及鉴权问题
1. phpunit.xml 中添加 dingo 相关配置 <env name="API_STANDARDS_TREE" value="x"/> & ...
- DirectX API 编程起步 #02 窗口的诞生
在这篇文章里我们先用 windows API 制作一个窗口出来,以后再用 DirectX API 渲染的东西就会显示在这里,控制台那黑白的画面肯定是没法用的. 每次的代码都会更新到Github 首先贴 ...
随机推荐
- 翻译:《实用的Python编程》03_01_Script
目录 | 上一节 (2.7 对象模型) | 下一节 (3.2 深入函数) 3.1 脚本 在该部分,我们将深入研究编写 Python 脚本的惯例. 什么是脚本? 脚本就是运行和终止一系列语句的程序. # ...
- 后端程序员之路 33、Index搜索引擎实现分析2-对外接口和大体流程
# index_manager的单例是index server对外的唯一接口,part_indexer是index搜索的核心部分,index_manager持有了一组part_indexer. typ ...
- 详解JavaScript中的原型
前言 原型.原型链应该是被大多数前端er说烂的词,但是应该还有很多人不能完整的解释这两个内容,当然也包括我自己. 最早一篇原型链文章写于2019年07月,那个时候也是费了老大劲才理解到了七八成,到现在 ...
- .net 开源模板引擎jntemplate 教程:基础篇之语法
一.基本概念 上一篇我们简单的介绍了jntemplate并写了一个hello world(如果没有看过的,点击查看),本文将继续介绍jntemplate的模板语法. 我们在讲解语法前,首先要了解一下标 ...
- 【免费开源】基于Vue和Quasar的crudapi前端SPA项目实战—环境搭建 (一)
背景介绍和环境搭建 背景 crudapi增删改查接口系统的后台Java API服务已经全部可用,需要一套后台管理UI,主要用户为开发人员或者对计算机有一定了解的工作人员,通过UI配置元数据和处理业务数 ...
- 自己动手实现springboot运行时新增/更新外部接口
最近有个需求:需要让现有springboot项目可以加载外部的jar包实现新增.更新接口逻辑.本着拿来主义的思维网上找了半天没有找到类似的东西,唯一有点相似的还是spring-loaded但是这个东西 ...
- java 给时间增加时间得到一个新的时间(日期)
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd") LocalDate expirationDate String exp ...
- vue Element-ui el-menu 左侧导航条
<template> <!--实现左侧导航条动态渲染(三级)--> <el-menu class="el-menu-vertical-demo" @o ...
- 使用MyBatis的步骤
1.创建空的Java工程,安装MyBatis依赖 <?xml version="1.0" encoding="UTF-8"?> <projec ...
- 轻量易用的微信Sdk发布——Magicodes.Wx.Sdk
概述 最简洁最易于使用的微信Sdk,包括公众号Sdk.小程序Sdk.企业微信Sdk等,以及Abp VNext集成. GitHub地址:https://github.com/xin-lai/Magico ...