search
HomeBackend DevelopmentPHP TutorialReading configuration in thinkPHP and parsing C method
Reading configuration in thinkPHP and parsing C methodJun 09, 2018 pm 02:33 PM
thinkphpreadConfiguration

This article mainly introduces the reading and C methods of configuration in thinkPHP, and analyzes the function, location, grouping and reading method of thinkPHP configuration file in the form of examples. Friends in need can refer to it

The examples in this article describe the reading and C methods of configuration in thinkPHP. Share it with everyone for your reference, the details are as follows:

1. Project public configuration

Conf/config.php

The content is as follows

<?php
/**
 *项目公共配置
 *@package
 *@author
 **/
return array(
 &#39;LOAD_EXT_CONFIG&#39;   => &#39;db,info,email,safe,upfile,cache,route,app,alipay,sms,platform,store,pay&#39;,  
 &#39;APP_AUTOLOAD_PATH&#39;  => &#39;@.ORG&#39;,
 &#39;OUTPUT_ENCODE&#39;   => true,    //页面压缩输出
 &#39;PAGE_NUM&#39;    => 15,
 /*Cookie配置*/
 &#39;COOKIE_PATH&#39;   => &#39;/&#39;,    // Cookie路径
 &#39;COOKIE_PREFIX&#39;   => &#39;&#39;,    // Cookie前缀 避免冲突
 /*定义模版标签*/
 &#39;TMPL_L_DELIM&#39;   =>&#39;{sh:&#39;,   //模板引擎普通标签开始标记
 &#39;TMPL_R_DELIM&#39;   =>&#39;}&#39;,    //模板引擎普通标签结束标记
 &#39;TMPL_CACHE_ON&#39;   => false,   //关闭模板缓存
 &#39;DEFAULT_GROUP&#39;   => &#39;Home&#39;,   //默认访问分组,设置默认入口
 &#39;APP_GROUP_LIST&#39;  => &#39;Agent,Home,System,User,Store,Wap,Mall,Opener&#39;,  // 项目分组设定,多个组之间用逗号分隔,例如&#39;Home,Admin&#39;
 &#39;PUBLIC_RESOURSE&#39;  => &#39;./Public/&#39;,
 &#39;URL_404_REDIRECT&#39;  => &#39;./Tpl/404.html&#39;,
);
?>

'LOAD_EXT_CONFIG' => 'db,info,email,safe,upfile,cache,route,app,alipay,sms,platform,store,pay' Confirm loading Additional configurations, these configurations can be read through the C() method and are globally valid.

2. If module grouping is enabled, you can define a configuration file for each group separately. The grouping configuration file is located at:

Project configuration directory/group name/ config.php

&#39;APP_GROUP_LIST&#39; => &#39;Home,Admin&#39;, //项目分组设定
&#39;DEFAULT_GROUP&#39; => &#39;Home&#39;, //默认分组

Now that two groups, Home and Admin, are defined, we can define the group configuration file as follows:

Conf/Home/config.php
Conf/Admin/config.php

The configuration file of each group is only valid in the current group. The definition format of the group configuration is the same as the project configuration.

Note: The group name is case-sensitive and must be consistent with the defined group name.

3. Read the configuration

After defining the configuration file, you can use the C method provided by the system (if you feel weird, you can use the Config word To help memory) to read the existing configuration

C(&#39;参数名称&#39;)//获取已经设置的参数值

For example, C('APP_STATUS') can read the setting value of the system's debug mode, Again, since configuration parameters are not case-sensitive, C('app_status') is equivalent, but uppercase convention is recommended.

If APP_STATUS does not yet exist, return NULL.

C method can also be used to read two-dimensional configuration

C(&#39;USER_CONFIG.USER_TYPE&#39;)//获取用户配置中的用户类型设置

C method reads global configuration and the configuration of the current module.

If there is no parameter, all valid configurations will be read.

If the same configuration name exists, the previous value will be overwritten.

For example:

&#39;HTML_CACHE_TIME&#39; => 60, //静态缓存有效期(秒)
&#39;HTML_CACHE_TIME&#39; => 80,

The final result obtained is 80.

The loading sequence is based on the parameter LOAD_EXT_CONFIG

&#39;LOAD_EXT_CONFIG&#39; => &#39;db,info,email,safe,upfile,cache,route,app,alipay,sms,platform,store,pay&#39;

For example, there is a parameter HTML_CACHE_TIME in info that is 60, but there is no parameter in other configurations, then this parameter is read It's 60 when it comes out.

If there is HTML_CACHE_TIME 50 in the db, the value is still 60. Because info is read later, the HTML_CACHE_TIME in the db is overwritten.

Attached is the C method source code

/**
 * 获取和设置配置参数 支持批量定义
 * @param string|array $name 配置变量
 * @param mixed $value 配置值
 * @return mixed
 */
function C($name=null, $value=null) {
 static $_config = array();
 // 无参数时获取所有
 if (empty($name)) {
  if(!empty($value) && $array = cache(&#39;c_&#39;.$value)) {
   $_config = array_merge($_config, array_change_key_case($array));
  }
  return $_config;
 }
 // 优先执行设置获取或赋值
 if (is_string($name)) {
  if (!strpos($name, &#39;.&#39;)) {
   $name = strtolower($name);
   if (is_null($value))
    return isset($_config[$name]) ? $_config[$name] : null;
   $_config[$name] = $value;
   return;
  }
  // 二维数组设置和获取支持
  $name = explode(&#39;.&#39;, $name);
  $name[0] = strtolower($name[0]);
  if (is_null($value))
   return isset($_config[$name[0]][$name[1]]) ? $_config[$name[0]][$name[1]] : null;
  $_config[$name[0]][$name[1]] = $value;
  return;
 }
 // 批量设置
 if (is_array($name)){
  $_config = array_merge($_config, array_change_key_case($name));
  if(!empty($value)) {// 保存配置值
   cache(&#39;c_&#39;.$value,$_config);
  }
  return;
 }
 return null; // 避免非法参数
}

##The above is the entire content of this article, I hope it will be useful for everyone’s learning Help, please pay attention to the PHP Chinese website for more related content!

Related recommendations:

Analysis of N method of ThinkPHP

The above is the detailed content of Reading configuration in thinkPHP and parsing C method. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
thinkphp是不是国产框架thinkphp是不是国产框架Sep 26, 2022 pm 05:11 PM

thinkphp是国产框架。ThinkPHP是一个快速、兼容而且简单的轻量级国产PHP开发框架,是为了简化企业级应用开发和敏捷WEB应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。

一起聊聊thinkphp6使用think-queue实现普通队列和延迟队列一起聊聊thinkphp6使用think-queue实现普通队列和延迟队列Apr 20, 2022 pm 01:07 PM

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了关于使用think-queue来实现普通队列和延迟队列的相关内容,think-queue是thinkphp官方提供的一个消息队列服务,下面一起来看一下,希望对大家有帮助。

thinkphp的mvc分别指什么thinkphp的mvc分别指什么Jun 21, 2022 am 11:11 AM

thinkphp基于的mvc分别是指:1、m是model的缩写,表示模型,用于数据处理;2、v是view的缩写,表示视图,由View类和模板文件组成;3、c是controller的缩写,表示控制器,用于逻辑处理。mvc设计模式是一种编程思想,是一种将应用程序的逻辑层和表现层进行分离的方法。

实例详解thinkphp6使用jwt认证实例详解thinkphp6使用jwt认证Jun 24, 2022 pm 12:57 PM

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了使用jwt认证的问题,下面一起来看一下,希望对大家有帮助。

thinkphp扩展插件有哪些thinkphp扩展插件有哪些Jun 13, 2022 pm 05:45 PM

thinkphp扩展有:1、think-migration,是一种数据库迁移工具;2、think-orm,是一种ORM类库扩展;3、think-oracle,是一种Oracle驱动扩展;4、think-mongo,一种MongoDb扩展;5、think-soar,一种SQL语句优化扩展;6、porter,一种数据库管理工具;7、tp-jwt-auth,一个jwt身份验证扩展包。

一文教你ThinkPHP使用think-queue实现redis消息队列一文教你ThinkPHP使用think-queue实现redis消息队列Jun 28, 2022 pm 03:33 PM

本篇文章给大家带来了关于ThinkPHP的相关知识,其中主要整理了使用think-queue实现redis消息队列的相关问题,下面一起来看一下,希望对大家有帮助。

thinkphp 怎么查询库是否存在thinkphp 怎么查询库是否存在Dec 05, 2022 am 09:40 AM

thinkphp查询库是否存在的方法:1、打开相应的tp文件;2、通过“ $isTable=db()->query('SHOW TABLES LIKE '."'".$data['table_name']."'");if($isTable){...}else{...}”方式验证表是否存在即可。

thinkphp3.2怎么关闭调试模式thinkphp3.2怎么关闭调试模式Apr 25, 2022 am 10:13 AM

在thinkphp3.2中,可以利用define关闭调试模式,该标签用于变量和常量的定义,将入口文件中定义调试模式设为FALSE即可,语法为“define('APP_DEBUG', false);”;开启调试模式将参数值设置为true即可。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft