🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
Lying的服务组件其实是一个对象的单例,并且每个单例都对应一个唯一的ID。 > 配置名:`service`,默认配置写在config/service.php中 [TOC] ### 定义一个组件 ~~~php 'id' => [ 'class' => '组件完整类名', //内置组件可省略 '可配置属性1' => '' '可配置属性2' => '' ], ~~~ 如果服务类没有可配置属性或者想使用默认配置属性,你也可以这样定义: ~~~php 'id' => '组件完整类名', ~~~ 这样,你就定义好了一个组件`id`,你可以使用`\Lying::$maker->get('id')`或者`\Lying::$maker->id`来使用一个组件。 ### 内置核心组件 Lying内置了一些组件以及组件id,这些内置组件无法更改id对应的class,但是可配置组件的属性,内置组件以及ID如下: ~~~php 'hook' => ['class' => 'lying\service\Hook'], 'request' => ['class' => 'lying\service\Request'], 'router' => ['class' => 'lying\service\Router'], 'cookie' => ['class' => 'lying\service\Cookie'], 'encrypter' => ['class' => 'lying\service\Encrypter'], 'dispatch' => ['class' => 'lying\service\Dispatch'], 'response' => ['class' => 'lying\service\Response'], 'view' => ['class' => 'lying\service\View'], 'session' => ['class' => 'lying\service\Session'], 'helper' => ['class' => 'lying\service\Helper'], ~~~ ### 定义相同组件 当然,你可以把某一个服务类设置成两个ID不同的组件(**核心组件除外**),比如两个数据库连接实例: ~~~php 'db1' => [ 'class' => 'lying\db\Connection', 'dsn' => 'mysql:host=127.0.0.1;dbname=db1;charset=utf8', 'user' => 'root', 'pass' => 'root', ], 'db2' => [ 'class' => 'lying\db\Connection', 'dsn' => 'mysql:host=127.0.0.1;dbname=db2;charset=utf8', 'user' => 'root', 'pass' => 'root', ], ~~~ 然后你就可以使用`\Lying::$maker->get('db1')`和`\Lying::$maker->get('db2')`或者`\Lying::$maker->db1`和`\Lying::$maker->db2`来使用连个不同的数据库连接实例了。 Lying为你准备了一些可重复定义的内置组件: ~~~php 'logger' => ['class' => 'lying\service\Logger'], 'cache' => ['class' => 'lying\cache\FileCache'], 'redis' => ['class' => 'lying\service\Redis'], 'db' => ['class' => 'lying\db\Connection'], 'upload' => ['class' => 'lying\upload\Upload'], 'captcha' => ['class' => 'lying\captcha\Captcha'], ~~~ ### 组件工厂 Lying为你提供了一个工厂实例`\Lying::$maker`,你可以使用`\Lying::$maker`来使用你已经定义的或者内置组件,这样做的好处是你的IDE会自动提示你的组件功能,同时也便于组件的调用: ~~~php \Lying::$maker->hook; \Lying::$maker->request; \Lying::$maker->router; \Lying::$maker->cookie; \lying::$maker->encrypter; \Lying::$maker->dispatch; \Lying::$maker->response; \Lying::$maker->view; \Lying::$maker->session; \Lying::$maker->helper \Lying::$maker->logger; \Lying::$maker->cache; \Lying::$maker->redis; \Lying::$maker->db; \Lying::$maker->upload; \Lying::$maker->captcha; ~~~ 对于可重复配置的内置组件,可这样使用不同组件: ~~~php \Lying::$maker->logger('logger1'); //等同于\Lying::$maker->logger1; \Lying::$maker->cache('cache1'); //等同于\Lying::$maker->cache1; \Lying::$maker->redis('redis1'); //等同于\Lying::$maker->redis1; \Lying::$maker->db('db1'); //等同于\Lying::$maker->db1; \Lying::$maker->upload('up1'); //等同于\Lying::$maker->up1; \Lying::$maker->captcha('ca1'); //等同于\Lying::$maker->ca1; ~~~ ### 动态注册服务 在程序里,可以动态注册服务组件: ~~~php \Lying::$maker->register('template', [ 'class' => 'lying\cache\FileCache', 'dir' => DIR_RUNTIME . DS . 'compile', 'gc' => 80, 'suffix' => 'php', 'serialize' => false, ]); ~~~ 或者: ~~~php \Lying::$maker->register('template', 'lying\cache\FileCache'); ~~~ 然后你就可以使用注册的组件了: ~~~php \Lying::$maker->cache('template'); //或者 \Lying::$maker->template; ~~~ > 注意,已经注册过的组件不会受到影响!!!也不会改变任何配置!!!