喜欢(❤ ω ❤) laravel 可是它太笨重了
想快速简洁的开发小项目,试一试Lemon 吧。php
Lemon 是一个现代的 PHP 框架,采用 composer 管理组件依赖。mysql
前面已经说过 Lemon 采用 composer 加载。linux
mkdir lemon cd lemon composer require chanywn/lemon
require 'vendor/autoload.php'; use Lemon\Route; Route::get('/', function($request, $response){ return $response->write('Hello lemon'); }); Route::run();
php -S localhost:4000
接下来,打开浏览器,访问 http://localhost:4000laravel
在 Lemon 中的路由是经过匹配的URL模式与回调函数。git
Route::get('/', function($request, $response) { echo 'index'; });
固然也能够这样表示github
Route::get('/', 'hello'); function hello($request, $response){ echo 'index'; }
Route 是路由模块的静态类,您能够经过执行route类的各类静态方法来响应特定方法。sql
Route::get('/method/1', function($request, $response) { echo '我收到了一个 GET 请求'; }); Route::post('/method/2', function($request, $response) { echo '我收到了一个 POST 请求'; }); Route::put('/method/3', function($request, $response) { echo '我收到了一个 GET 请求'; }); Route::delete('/method/4', function($request, $response) { echo '我收到了一个 POST 请求'; }); Route::any('/method/5', function($request, $response) { echo sprintf('我收到了一个 %s 请求', $request->method); });
哈哈,能够愉快的开发 restful api 了。数据库
你可能已经注意到了回调函数中的 $request
和 $response
,这两个参数,这两个参数是Request
和Response
类的实例,是回调函数的必须参数。这两个参数颇有用,以后会介绍。api
route::get('/hello/(:any)', function($request, $response, $name) { echo 'hello ' . $name; });
该方法第一个参数是要匹配的路由URL,其中(:any)
通配符用来匹配任意值。在回调函数中咱们使用了$name
参数来接收这个值。数组
上面典型的路由匹配的是一个,匹配多个值的时候,回调函数中的参数位置对应匹配的值,参数名自定
。
route::get('/(:num)/(:num)/(:num)', function($request, $response, $year, $month, $day) { echo $year . '/' . $month . '/' . $day; });
(:num)
匹配只含有数字的一段。 (:any)
匹配含有任意字符的一段。
获得当前请求的路径、方法、ip
route::get('/', function($request, $response) { echo $request->path .'<br>'; echo $request->method .'<br>'; echo $request->ip .'<br>'; });
接受get参数
localhost:3000/home?name=razor&age=0
route::get('/home', function($request, $response) { var_dump($request->get()); // or echo $request->get('name'); });
接受post参数
route::any('/home', function($request, $response) { var_dump($request->post()); // or echo $request->post('name'); });
判断当前请求类型
route::any('/', function($request, $response) { if($request->isGET()) { echo '当前是 GET 请求'; } if($request->isPost()) { echo '当前是 Post 请求'; } });
重定向
route::get('/', function($request, $response) { return $response->redirect('/home'); });
返回HTTP状态码
route::get('/', function($request, $response) { return $response->statusCode(404); });
渲染视图
route::get('/', function($request, $response) { return $response->view('index'); });
或者
route::get('/', function($request, $response) { return $response->view('index', ['title' => '首页']); });
<?php require '../vendor/autoload.php'; use Lemon\Route; use Lemon\Validation; route::get('/', function($request, $response) { /* * 规则数组,键为要验证的字段,值为要验证的规则 * :姓名 这是别名的写法,能够加也能够不加 */ $rules = [ 'name:姓名' => 'required|min:3|max:9', 'age:年龄' => 'required|integer', 'email:邮箱' => 'required|email', ]; $validator = new Validation($request->get(), $rules); // 判断是否成功验证 if(!$validator->success) { //输出[数组]错误 debug($validator->errors); } });
目前支持的规则有如下几点
return [ 'email' => ':attribute 格式不可用', 'min' => ':attribute 长度必须大于或等于 :min', 'max' => ':attribute 长度必须小于 :max.', 'required' => ':attribute 是必填项', 'numeric' => ':attribute 必须为数字', 'integer' => ':attribute 必须为整数', 'alpha' => ':attribute 必须仅包含字母字符', 'alpha_dash'=> ':attribute 必须仅包含字母、数字、破折号', 'alpha_num' => ':attribute 必须仅包含字母、数字' ];
若是你感兴趣,欢迎扩充验证规程,好比httpurl、ip等等
数据库操做类的命名空间在 Lemon\Database;
,Database 基于Pdo实现的数据库链式查询。
Lemon\Database::set([ 'driver' => 'mysql', 'host' => 'localhost', 'port' => '3306', 'username' => 'root', 'password' => '', 'database' => '', 'charset' => 'utf8' ]);
<?php require '../vendor/autoload.php'; use Lemon\Route; use Lemon\Database; Database::set([ 'driver' => 'mysql', 'host' => 'localhost', 'port' => '3306', 'username' => 'root', 'password' => '', 'database' => '', 'charset' => 'utf8' ]); Route::get('/', function($request, $response){ $users = Database::table('users')->get(); }); Route::run();
get 方法有一个参数,默认是*
,执行成功返回一个数组。
若是你只须要从数据表中获取一行数据,则可使用 first 方法。这个方法将返回单个关联数组:
$user = db::table('users')->where('name', 'John')->first(); echo $user->name;
若是你不须要一整行数据,则能够带上参数来从单条记录中取出单个值。此方法将直接返回字段的值:
$name= db::table('users')->where('name', 'John')->first('name'); echo $name;
若是你的某个表主键名正好叫id
,你能够这样找到它。
db::table('users')->find($id);
若是它叫其它什么名
db::table('users')->find($id, 'user_id');
orderBy 方法容许你根据指定字段对查询结果进行排序。orderBy 方法的第一个参数是你想要用来排序的字段,而第二个参数则控制排序的顺序,能够为 asc 或 desc:
db::table('users')->orderBy('id')->get();
你能够在查询构造器实例中使用 where 方法从而把 where 子句加入到这个查询中。基本的 where 方法须要3个参数。第一个参数是字段的名称。第二个参数是要对字段进行评估的值。第三个参数是运算符,可选参数默认为=
,它能够是数据库所支持的任何运算符。
$users = db::table('users')->where('votes', 100)->get(); $users = db::table('users')->where('votes', 100, '>')->get();
你可使用take 方法来限制查询结果数量,两个参数第一个是起始位置,第二个是取多少条数据:
$users = db::table('users')->take(10, 20)->get();
查询构造器也提供了 insert 方法,用来插入记录到数据表中。insert 方法接收一个包含字段名和值的数组做为参数:
db::table('users')->insert( ['email' => 'john@example.com', 'votes' => 0] );
执行成功返回受影响的行,失败返回false
.
(无)
固然,除了在数据库中插入记录外,你也可使用 update 来更新已存在的记录。update 方法和 insert 方法同样,接收含有字段及值的数组,其中包括要更新的字段。可使用 where 子句来约束 update 查找:
db::table('users')->where('id', 1)->update(['votes' => 1]);
(无)
查询构造器也可以使用 delete 方法从数据表中删除记录。在 delete 前,还可以使用 where 子句来约束 delete 语法:
db::table('users')->delete(); db::table('users')->where('votes',100,'>')->delete();
基于 Lemon 的博客项目
https://github.com/chanywn/lemon
欢迎贡献代码