webman快速入門1
1 數據庫操作 安裝直接看文檔 我比較喜歡原生的寫法 這里沒用連接池 因為他用單利模式 也反復用 性能不比用了連接池差
//返回值:方法總會返回結果的數組數據。數組中的每個結果都是一個 PHP StdClass 對象,這使你能夠訪問到結果的
$results = DB::select('select * from aa limit 1'); foreach ($results as $key => $value) { echo $value->name; } DB::transaction(function () { // 返回影響的行數 $affected = DB::update('update aa set name = 200 where id = ?', [11]); $affected =DB::insert('insert into aa (name, email) values (?, ?)', ['11', '11']); $affected = DB::delete('delete from aa where id=4'); $affected =DB::statement('drop table aa'); print_r($affected); });
2 redis操作
$key = 'test_key'; Redis::set($key, rand());
3 常用function函數
<?php /** * Here is your custom functions. */ function gff(){ return 11; }
4 寫入日志
Log::info('log test');
5 中間件
H:\phpStudy\WWW\webman\config\middleware.php
<?php /** * This file is part of webman. * * Licensed under The MIT License * For full copyright and license information, please see the MIT-LICENSE.txt * Redistributions of files must retain the above copyright notice. * * @author walkor<walkor@workerman.net> * @copyright walkor<walkor@workerman.net> * @link http://www.workerman.net/ * @license http://www.opensource.org/licenses/mit-license.php MIT License */ return [ '' => [ //support\middleware\AuthCheckTest::class, support\middleware\AccessControlTest::class, ] ];
<?php /** * This file is part of webman. * * Licensed under The MIT License * For full copyright and license information, please see the MIT-LICENSE.txt * Redistributions of files must retain the above copyright notice. * * @author walkor<walkor@workerman.net> * @copyright walkor<walkor@workerman.net> * @link http://www.workerman.net/ * @license http://www.opensource.org/licenses/mit-license.php MIT License */ namespace support\middleware; use Webman\MiddlewareInterface; use Webman\Http\Response; use Webman\Http\Request; class AccessControlTest implements MiddlewareInterface { public function process(Request $request, callable $next) : Response { echo 'wef'; return json(['code' => 0, 'msg' => 'ok']); // 允許uri以 /api 開頭的地址跨域訪問 if (strpos($request->path(), '/api') === 0) { // 如果是options請求,不處理業務 if ($request->method() == 'OPTIONS') { $response = response(''); } else { $response = $next($request); } $response->withHeaders([ 'Access-Control-Allow-Origin' => '*', 'Access-Control-Allow-Methods' => 'GET,POST,PUT,DELETE,OPTIONS', 'Access-Control-Allow-Headers' => 'Content-Type,Authorization,X-Requested-With,Accept,Origin', ]); } else { $response = $next($request); } return $response; } }
支持攔截
