php swoft 中的数据分层

 不仅仅局限于 MVC 。将数据在model 这一个层面剖析开,优雅的处理数据  逻辑,缓存,业务,数据库操作的烦恼。

这个思路也适用于 thinkphp,hyperf,imi 等框架。不再简单的 实现 controller->model->view 的处理过程。

简化代码,每一层清晰地定义相应处理的数据。

 controller 层面 

<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace AppHttpController;

use Swoft;
use SwoftHttpMessageContentType;
use SwoftHttpMessageResponse;
use SwoftHttpServerAnnotationMappingController;
use SwoftHttpServerAnnotationMappingRequestMapping;
use SwoftHttpServerAnnotationMappingRequestMethod;
use SwoftHttpMessageRequest;
use SwoftViewRenderer;
use Throwable;
use function context;

use AppModelDataHomeData;

/**
 * Class HomeController
 * @Controller("/Home")
 */
class HomeController
{
    /**
     * @RequestMapping("/Home/HomeTest",method={RequestMethod::GET})
     *
     * @return Response
     */
    public function HomeTest()
    {
        $request = context()->getRequest();

        $HomeData = new HomeData();

        $Detail = $HomeData->DataTest();

        $res = Result(200, '获取成功', $Detail);

        return context()->getResponse()->withData($res);
    }
    
}

Data 层中 进行缓存判断,资源调用。

<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace AppModelData;

use AppModelDaoHomeDao;
use AppModelEntityHome;

use SwoftRedisExceptionRedisException;
use SwoftRedisPool;
use SwoftRedisRedis;

class HomeData
{
    public function DataTest(){

        $ReductionRedis = Redis::rawCommand('GET', 'FullReduction');

        $ReductionInfo = json_decode($ReductionRedis, true);

        if (empty($ReductionInfo)) {
            $HomeDao = new HomeDao();

            $ReductionInfo = $HomeDao->HomeDaoTest();
        }

        return $ReductionInfo;
    }


}

Dao层,返回数据。操作实例。 【Entity】中,就是直接指定数据文件了。

<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace AppModelDao;

use SwoftRedisPool;
use SwoftRedisRedis;

use AppModelEntityHome;

class HomeDao
{

    public function HomeDaoTest(){

        $Home = new Home();

        $DailyActivity = $Home->DailyActivity();

        return $DailyActivity;
    }




}
原文地址:https://www.cnblogs.com/heijinli/p/14150862.html