zoukankan      html  css  js  c++  java
  • Swoft 容器使用

     

    可以借助Swoft下的Bean类操作容器

    示例:

    将类绑定至容器

    use SwoftBeanAnnotationBean;
    
    /**
     * @Bean("imageLogic")
     */
    class ImageLogic extends BaseLogic
    {
        /**
         * 根据id获取图片
         * @param int $id
         * @return array
         */
        public static function getOne(int $id): array
        {
            return Query::table(Image::class)->where('id',$id)->limit(1)->get(['url','thumb_url'])->getResult();
        }
    }

    从容器中取出对象:

    方法一:注入

    use SwoftBeanAnnotationInject;
    use AppModelsLogicImageLogic;
    
    class IndexController{
        /**
         * @Inject("imageLogic")
         * @var ImageLogic
         */
        private $imageLogic;
      
        public function index()
        {
            $this->imageLogic->getOne();
        }         
    }

    方法二:通过 BeanFactory 类获取

    use SwoftBeanBeanFactory;
    
    class IndexController
    {
        /**
         * @RequestMapping(route="index",method=RequestMethod::GET)
         */
        public function index(Request $request){
            //判断容器中是否存在该实例
            var_dump(BeanFactory::hasBean('imageLogic'));
            //从容器中获取
            $bean = BeanFactory::getBean('imageLogic');
            return $bean->getOne();
        }
    
    }
  • 相关阅读:
    教你如何剖析源码
    Java 简介
    java 入门-helloWorld
    linux yum 命令
    Linux vi/vim
    Linux 磁盘管理
    Linux 用户和用户组管理
    Linux 文件与目录管理
    Linux 文件基本属性
    Linux安装Mysql
  • 原文地址:https://www.cnblogs.com/brady-wang/p/13338974.html
Copyright © 2011-2022 走看看