hyperf框架学习

安装

composer create-project hyperf/hyperf-skeleton

 

修改配置文件
.env

APP_NAME=skeleton
APP_ENV=dev

DB_DRIVER=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=hyperf_test
DB_USERNAME=root
DB_PASSWORD=123456
DB_CHARSET=utf8mb4
DB_COLLATION=utf8mb4_unicode_ci
DB_PREFIX=

REDIS_HOST=127.0.0.1
REDIS_AUTH=(null)
REDIS_PORT=6379
REDIS_DB=0

 

启动
D:\workspace\hyperf-skeleton>swoole-cli -d swoole.use_shortname=off bin/hyperf.php start

数据库组件

composer require hyperf/db-connection
composer require hyperf/database

 

routes.php

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
use Hyperf\HttpServer\Router\Router;
use Hyperf\DbConnection\Db;

Router::addRoute(['GET', 'POST', 'HEAD'], '/', 'App\Controller\IndexController@index');

Router::get('/favicon.ico', function () {
    return '';
});

Router::get('/test', function () {
    $users = Db::select('SELECT * FROM `users` WHERE id >= ?',[0]);
    return ['title' => '测试中。。。', 'users' => $users];
});

 

IndexController.php

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

class IndexController extends AbstractController
{
    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "你好,{$user}.",
        ];
    }
}

  

路由看起来有集中式路由和注解路由两种方式,php7风格的。

运行结果:

 

 

 


安装
https://hyperf.wiki/3.1/#/zh-cn/quick-start/install

hyperf学习过程~~~延展学习积累
https://blog.csdn.net/thinkthewill/article/details/109287157

hyperf 模型连接redis 失败
https://blog.51cto.com/u_16213376/12042690

windows下安装和使用swoole-cli
https://blog.csdn.net/qq_36334798/article/details/140397294

SQLSTATE[HY000] [2002] No such file or directory 问题解决
https://www.cnblogs.com/hwrex/p/18426266

posted @ 2024-12-25 10:23  河北大学-徐小波  阅读(4)  评论(0编辑  收藏  举报