webman快速入门1

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;
    }
}

支持拦截

posted @ 2021-02-21 15:21  newmiracle宇宙  阅读(2672)  评论(0编辑  收藏  举报