PHP(8)RESTful - Model Route Controller - Simple Example
All the sample and details are in project easyphprest project.
1. Model
I am using the default things, so I directly create a Class named Book under app/Book.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
protected $fillable = ['title', 'author', 'isbn'];
}
?>
2. Route
Under directory app/Http/, there is a file routes.php, It will take care of all the routes configurations.
$app->get('/', function() use ($app) {
return $app->welcome();
});
$app->group(['prefix' => 'api/v1', 'namespace' => 'App\Http\Controllers'], function($app){
$app->get('book', 'BookController@index');
$app->get('book/{id}', 'BookController@getbook');
$app->post('book', 'BookController@createBook');
$app->put('book/{id}', 'BookController@updateBook');
$app->delete('book/{id}', 'BookController@deleteBook');
});
It will mapping all the requests to Controller, formatted and received all the parameters. That is exactly what the framework should do for us. It is quite similar to other frameworks in java/scala/python.
3. Controller
The actually controller is app/Http/Controllers/BookController.php
<?php
namespace App\Http\Controllers;
use App\Book;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class BookController extends Controller{
public function index(){
$Books = Book::all();
return response()->json($Books);
}
public function getBook($id){
$Book = Book::find($id);
return response()->json($Book);
}
public function createBook(Request $request){
$Book = Book::create($request->all());
return response()->json($Book);
}
public function deleteBook($id){
$Book = Book::find($id);
$Book->delete();
return response()->json('deleted');
}
public function updateBook(Request $request, $id){
$Book = Book::find($id);
$Book->title = $request->input('title');
$Book->author = $request->input('author');
$Book->isbn = $request->input('isbn');
$Book->save();
return response()->json($Book);
}
}
?>
That is just basic implementation, we may need validation and other logic there. The good things is that there is the simple ORM in this framework as well. I did not take care of any DAO logic now.
Command to start our service
> php artisan serve
Then, we can directly use POST man to test all our things.
I do not know how long this shared link will work, I will just paste it here.
https://www.getpostman.com/collections/e3273823c70a058eedfe
References:
http://coderexample.com/restful-api-in-lumen-a-laravel-micro-framework/
http://sillycat.iteye.com/blog/2238841
分享到:
相关推荐
instrumented-restful-fast-request-pro-2023.1.7.1 可用版,亲测,可用。
Java SpringBoot 课程 restful crud 实验资源 restful 风格的增删改查
提供restful风格的框架, 拥有客户端和服务端两部分, 具体实现功能代码需要自己添加。
SAP-BO-Restful-SDK-开发指导手册
资源来自pypi官网。 资源全名:flask-restful-swagger-3-0.2.0.tar.gz
simple-restful-api-php-源码.rar
laravel-restful-api-starter, 使用 Laravel 和MongoDB构建一个 RESTful API #Laravel Restful API启动程序现在查看一下 WIKI !欢迎捐赠对于代码和 doc: ) !是什么?这里启动器基于以下功能:Laravel 4.2: 使用轻...
simple-restful-api-源码.rar
spring boot学习的web开发课程资源 spring boot学习的web开发课程资源 spring boot学习的web开发课程资源 spring boot学习的web开发课程资源 spring boot学习的web开发课程资源 spring boot学习的web开发课程...
Java-RESTful-Web-Service.rar
本资源类似于 Postman 的 IDEA 插件Restful Fast Request,在IDEA安装该插件后,找到插件安装目录,【C:\\... API Buddy\\lib】替换掉里面的jar文件【instrumented-restful-fast-request-pro-2023.1.7.1.jar】即可使用
Metamorphic-Testing-of-RESTful-Web-APIs.pdf
struct2-restful-Demo
Building-RESTful-Python-Web-Services.pdf
Restful风格的CRUD练习资料;
----------------------- RESTful-API后台系统架构设计(Java)全文共6页,当前为第1页。 RESTful-API后台系统架构设计(Java)全文共6页,当前为第2页。 RESTful-API后台系统架构设计(Java)全文共6页,当前为第3页。 ...
RESTful-API-design-OCTO-Quick-Reference-Card-2.2
RESTful-Unity旨在为提供一个简单的RESTful HTTP服务器。 通过http请求从Unity场景访问并封装游戏对象的状态。 这个项目是在3D原型制作原型期间创建的,其中可以通过访问和控制游戏对象。 这是概念验证项目,因此...
restful-react 构建与RESTful API交互的React应用程序会带来一系列问题,挑战和潜在陷阱。 该项目旨在消除此类陷阱,并在制作此类应用程序时为开发人员提供愉悦的体验。 可以将它视为React组件和挂钩形式的的薄包装...
Java SpringBoot 课程 restful crud 实验资源 ...GitHub中有源码 https://github.com/kevinkda/MarkdownRepository/tree/master/Develop%20Demo/Java%20Framework/Spring/SpringBoot/spring-boot-restful-crud-01