簡單快速的開發WEB應用, PHP 框架 Lemon 介紹

喜歡(❤ ω ❤) laravel 可是它太笨重了
想快速簡潔的開發小項目,試一試Lemon 吧clipboard.pngphp

Lemon 是一個現代的 PHP 框架,採用 composer 管理組件依賴。mysql

clipboard.png

如何使用 Lemon

安裝

前面已經說過 Lemon 採用 composer 加載。linux

第一步,使用 composer 安裝

mkdir lemon

cd lemon

composer require chanywn/lemon

第二步,新建 index.php 文件

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 的功能

路由

在 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,這兩個參數,這兩個參數是RequestResponse類的實例,是回調函數的必須參數。這兩個參數頗有用,以後會介紹。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) 匹配含有任意字符的一段。

Request(請求)

獲得當前請求的路徑、方法、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 請求';    
    }
});

Response(響應)

重定向

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;

find 子句

若是你的某個表主鍵名正好叫id,你能夠這樣找到它。

db::table('users')->find($id);

若是它叫其它什麼名

db::table('users')->find($id, 'user_id');

orderBy 子句

orderBy 方法容許你根據指定字段對查詢結果進行排序。orderBy 方法的第一個參數是你想要用來排序的字段,而第二個參數則控制排序的順序,能夠爲 asc 或 desc:

db::table('users')->orderBy('id')->get();

Where 子句

你能夠在查詢構造器實例中使用 where 方法從而把 where 子句加入到這個查詢中。基本的 where 方法須要3個參數。第一個參數是字段的名稱。第二個參數是要對字段進行評估的值。第三個參數是運算符,可選參數默認爲=,它能夠是數據庫所支持的任何運算符。

$users = db::table('users')->where('votes', 100)->get();

$users = db::table('users')->where('votes', 100, '>')->get();

take 子句

你可使用take 方法來限制查詢結果數量,兩個參數第一個是起始位置,第二個是取多少條數據:

$users = db::table('users')->take(10, 20)->get();

insert 方法

查詢構造器也提供了 insert 方法,用來插入記錄到數據表中。insert 方法接收一個包含字段名和值的數組做爲參數:

db::table('users')->insert(
    ['email' => 'john@example.com', 'votes' => 0]
);

執行成功返回受影響的行,失敗返回false.

自增 ID

(無)

Updates 方法

固然,除了在數據庫中插入記錄外,你也可使用 update 來更新已存在的記錄。update 方法和 insert 方法同樣,接收含有字段及值的數組,其中包括要更新的字段。可使用 where 子句來約束 update 查找:

db::table('users')->where('id', 1)->update(['votes' => 1]);

自增或自減

(無)

Delete 方法

查詢構造器也可以使用 delete 方法從數據表中刪除記錄。在 delete 前,還可以使用 where 子句來約束 delete 語法:

db::table('users')->delete();

db::table('users')->where('votes',100,'>')->delete();

實戰

基於 Lemon 的博客項目

https://blog.codefun.cn/
圖片描述
圖片描述

源碼倉庫

https://github.com/chanywn/lemon

歡迎貢獻代碼

相關文章
相關標籤/搜索