当前位置: 首页 > news >正文

网站建设 移动端 和 PC端万和城网站

网站建设 移动端 和 PC端,万和城网站,淄博网站建设0533cl,漳州手工外发加工网介绍 Laravel 框架的所有配置文件都保存在 config 目录中。每个选项都有说明,你可随时查看这些文件并熟悉都有哪些配置选项可供你使用。 使用 您可以在应用程序的任何位置使用全局 config 辅助函数轻松访问配置值。 可以使用“点”语法访问配置值,其中…

介绍

Laravel 框架的所有配置文件都保存在 config 目录中。每个选项都有说明,你可随时查看这些文件并熟悉都有哪些配置选项可供你使用。

使用

您可以在应用程序的任何位置使用全局 config 辅助函数轻松访问配置值。 可以使用“点”语法访问配置值,其中包括您希望访问的文件名和选项。 也可以指定默认值,如果配置选项不存在,将返回:

$value = config('app.timezone');// 如果配置值不存在,则检索默认值...
$value = config('app.timezone', 'Asia/Seoul');

使用分析

我们可以先看一下助手函数 config

if (! function_exists('config')) {/*** Get / set the specified configuration value.** If an array is passed as the key, we will assume you want to set an array of values.** @param  array|string|null  $key* @param  mixed  $default* @return mixed*/function config($key = null, $default = null){if (is_null($key)) {return app('config');}if (is_array($key)) {return app('config')->set($key);}return app('config')->get($key, $default);}
}

分析

app(‘config’) 是注册到服务容器的实例,这里我们追踪到config 的实例是

Illuminate\Contracts\Config\Repository

Repository 的 set & get 方法代码如下

    /*** Set a given configuration value.** @param  array|string  $key* @param  mixed  $value* @return void*/public function set($key, $value = null){$keys = is_array($key) ? $key : [$key => $value];foreach ($keys as $key => $value) {Arr::set($this->items, $key, $value);}}/*** Get the specified configuration value.** @param  array|string  $key* @param  mixed  $default* @return mixed*/public function get($key, $default = null){if (is_array($key)) {return $this->getMany($key);}return Arr::get($this->items, $key, $default);}

这时我们就可以清晰的知道,助手函数config($key = null, $default = null)的含义:

  • 如果$key 不传或者传null,返回的是注册到服务容器中Repository实例
  • 如果$key 为数组,那么修改Repository 的 items的值
  • 其他情况 返回 Repository 的 items[$key] ?? $default

  • 此时我们就可能有疑问 Repositiry 中 items 值是什么时候进行赋值的呢?赋的值又是那些呢?接下来我们一点点的分析

    绑定服务容器

    在laravel运行中,并没有直接绑定 Repositiry 到容器中,而是第一次构建config实例的时候进行了绑定,app(‘config’) 或者 app()->make(‘config’) 时检测是否进行绑定,如果没有的话进行绑定,下面时make函数:

        /*** Resolve the given type from the container.** @param  string  $abstract* @param  array  $parameters* @return mixed*/public function make($abstract, array $parameters = []){$abstract = $this->getAlias($abstract);if (! $this->bound($abstract) &&array_key_exists($abstract, $this->availableBindings) &&! array_key_exists($this->availableBindings[$abstract], $this->ranServiceBinders)) {$this->{$method = $this->availableBindings[$abstract]}();$this->ranServiceBinders[$method] = true;}return parent::make($abstract, $parameters);}
    

    根据代码可知

    $this->availableBindings['config'] = registerConfigBindings
    

    registerConfigBindings函数即为绑定到容器方法

        /*** Register container bindings for the application.** @return void*/protected function registerConfigBindings(){$this->singleton('config', function () {return new ConfigRepository;});}
    

    因此在调用make(‘config’) 时若没有绑定,自动调用registerConfigBindings方法进行绑定

    配置文件赋值

    在bootstrap/app.php中,进行对配置文件进行了赋值,譬如:

    $app->configure('auth');
    $app->configure('permission');
    $app->configure('excel');
    $app->configure('database');
    

    configure函数为:

        /*** Load a configuration file into the application.** @param  string  $name* @return void*/public function configure($name){if (isset($this->loadedConfigurations[$name])) {return;}$this->loadedConfigurations[$name] = true;$path = $this->getConfigurationPath($name);if ($path) {$this->make('config')->set($name, require $path);}}
    

    这里的操作就是将conf/$name.conf 的内容写到 Repositiry 中 items 值中

http://www.yayakq.cn/news/71043/

相关文章:

  • 招聘网站开发源代码电子商务网站建设考纲
  • 流量统计是可以查询到网站来路的关键字里出现了不相关的关键词网站建设要注意些什么
  • 网站seo优化技术入门做网站郑州汉狮
  • 河南省建设厅职称网站网站高防服务器租用
  • 免费pc网站建设界面漂亮的网站
  • 三合一网站建站网页设计的方法
  • 简述网站规划的一般步骤创建电子商务网站
  • 遵义市做网站公司中国十大含金量证书
  • 企业网站管理系统cms坪地网站建设怎么样
  • 同泰公司网站公司查询公司网站建站要多少钱
  • vs2008做网站专业汽车网站
  • 河南第一火电建设公司网站电子商务网站建设的认识的心得
  • 淘宝客网站建站教程做零食网站怎么样
  • 网站域名去哪里备案福州网站设计哪里好
  • 恩施建设厅网站百度建站云南服务中心
  • 建建设网站公司石家庄建设网站公司简介
  • 推广型网站免费建设安卓小程序开发入门
  • 微信团购群网站怎样做网站百度权重查询
  • 漳州做网站优化餐饮网站建设
  • 漫画驿站网页设计图纸尺寸图建网站报价明细表
  • 湛江制作公司网站只做男士衬衫的网站
  • 建设网站合同网站后台编辑技巧
  • 网站 前端 后端ss网站代码
  • 淘客网站推广免备案wordpress使用模板
  • 汽车门户网站开发网站数据库分离怎么做
  • 网站备案名称规则iis搭建网站
  • 网站首页设计风格淘宝店网站论坛怎么做
  • 树莓派wordpress建站苏州网信信息科技股份有限公司
  • 网站优化可以做哪些优化网址导航怎么更换
  • 网站建设与制作石家庄wordpress自动采集翻译