search
HomeBackend DevelopmentPHP TutorialThe Right Way to Build Configurable PHP Applications_PHP Tutorial
The Right Way to Build Configurable PHP Applications_PHP TutorialJul 13, 2016 am 10:52 AM
phpseveral kindscreateCanappWaymethodConstructcorrectofConfiguration

This article illustrates several ways to create configurable PHP applications. The article also explores the ideal configuration points in an application and seeks a balance between an application being too configurable and being too closed.

If you plan to make your PHP application available to other people or companies, you need to make sure that the application is configurable. At a minimum, allow users to set up database logins and passwords in a secure manner so that the material within them is not made public.

This article demonstrates several techniques for storing configuration settings and editing these settings. In addition, the article also provides guidance on which elements need to be made configurable and how to avoid falling into the dilemma of over- or under-configuration.

​Configuration using INI file

PHP has built-in support for configuration files. This is accomplished through an initialization file (INI) mechanism such as a php.ini file, where constants such as database connection timeouts or how sessions are stored are defined. If you wish, you can customize the configuration for your application in this php.ini file. To illustrate, I added the following lines of code to the php.ini file.

​myapptempdir=foo

Then, I wrote a small PHP script to read this configuration item, as shown in Listing 1.

List 1. ini1.php

function get_template_directory()
{
​$v = get_cfg_var( "myapptempdir" );
return ( $v == null ) ? "tempdir" : $v;
}

echo( get_template_directory()."n" );
?>

​When running this code in the command line, the following results are obtained:

% php ini1.php
foo
%

marvelous. But why can't we use the standard INI function to get the value of the myapptempdir configuration item? I did some research and found that in most cases, custom configuration items cannot be obtained using these methods. However, it is accessible using the get_cfg_var function.

To make this method simpler, encapsulate access to the variable in a second function that takes the configuration key name and a default value as parameters, as shown below.

Listing 2. ini2.php

function get_ini_value( $n, $dv )
{
​$c = get_cfg_var( $n );
return ( $c == null ) ? $dv : $c;
}

function get_template_directory()
{
return get_ini_value( "myapptempdir", "tempdir" );
}

This is a good overview of how to access an INI file, so if you want to use a different mechanism or store the INI file somewhere else, you don't have to go through the trouble of changing a bunch of functions.

I don't recommend using INI files for application configuration, for two reasons. First, while this makes it easier to read the INI file, it makes it almost impossible to write the INI file safely. So this is only suitable for read-only configuration items. Second, the php.ini file is shared across all applications on the server, so I don't think application-specific configuration items should be written in that file.

What do you need to know about INI files? The most important thing is how to reset the include path to add configuration items as shown below.

Listing 3. ini3.php

echo( ini_get("include_path")."n" );
ini_set("include_path",
ini_get("include_path").":./mylib" );
echo( ini_get("include_path")."n" );
?>

In this example, I added my local mylib directory to the include path, so I can require PHP files from that directory without adding the path to the require statement.

Configuration in PHP

A common alternative to storing configuration entries in an INI file is to use a simple PHP script to persist the data. Below is an example.

Listing 4. config.php

# Specify the location of the temporary directory
#
$TEMPLATE_DIRECTORY = "tempdir";
?>

The code using this constant is as follows.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632442.htmlTechArticleThis article illustrates several methods of creating configurable PHP applications. The article also explores the ideal configuration points in an application and finds the balance between an application that is too configurable and one that is too closed...
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
VUE3入门教程:使用Webpack进行打包和构建VUE3入门教程:使用Webpack进行打包和构建Jun 15, 2023 pm 06:17 PM

Vue是一款优秀的JavaScript框架,它可以帮助我们快速构建交互性强、高效性好的Web应用程序。Vue3是Vue的最新版本,它引入了很多新的特性和功能。Webpack是目前最流行的JavaScript模块打包器和构建工具之一,它可以帮助我们管理项目中的各种资源。本文就为大家介绍如何使用Webpack打包和构建Vue3应用程序。1.安装Webpack

使用CMake构建Linux内核的配置指南使用CMake构建Linux内核的配置指南Jul 06, 2023 pm 02:46 PM

使用CMake构建Linux内核的配置指南概述在Linux开发中,构建和配置内核是一个重要的环节。对于大多数人来说,使用Kconfig和Makefile是最常见的配置方式。然而,使用CMake来构建和配置Linux内核也是一个灵活且强大的选择。本文将介绍如何使用CMake来构建和配置Linux内核,并附上一些代码示例。安装CMake首先,我们需要安装CMak

如何使用Golang构建Web应用程序如何使用Golang构建Web应用程序Jun 24, 2023 pm 02:46 PM

在当前的互联网时代,Web应用程序已成为了人们日常生活中不可或缺的一部分,而且在各种应用场景下都有广泛的应用。无论是电商网站、社交媒体、在线教育平台,还是各种SaaS应用程序,都离不开Web应用程序。随着技术的不断更新迭代,Golang越来越受到Web应用程序开发者的喜爱,下面我们就快速了解如何使用Golang构建Web应用程序。一、为什么使用Golang?

CakePHP中间件:快速构建可扩展的Web应用程序CakePHP中间件:快速构建可扩展的Web应用程序Jul 28, 2023 am 11:33 AM

CakePHP中间件:快速构建可扩展的Web应用程序概述:CakePHP是一个流行的PHP框架,被广泛应用于Web应用程序的开发。其提供了许多功能强大的工具和功能,其中包括中间件。中间件可以帮助我们快速构建和扩展Web应用程序,提高代码的可读性和可维护性。什么是中间件:中间件是在请求被派发给控制器之前或之后执行的一系列操作。它们可以完成许多任务,如身份验证、

如何使用PHP构建智能医疗系统如何使用PHP构建智能医疗系统Jun 11, 2023 pm 05:32 PM

在当今科技迅猛发展的时代,智慧医疗逐渐成为医疗行业的新趋势,而医疗健康的数据化和智能化,更是将如何使用PHP构建智能医疗系统变得尤为重要。本文将介绍PHP如何应用于医疗系统的开发,并结合实例详细探讨。一、智能医疗系统的功能特点首先了解智能医疗系统的主要功能特点,有助于我们更加清晰的构建医疗系统。智能医疗系统的主要特点包括:1、大数据分析预测功能:通过对医学数

使用JavaScript构建在线计算器使用JavaScript构建在线计算器Aug 09, 2023 pm 03:46 PM

使用JavaScript构建在线计算器随着互联网的发展,越来越多的工具和应用开始以在线形式出现。其中,计算器是一类被广泛使用的工具之一。本文将介绍如何使用JavaScript构建一个简单的在线计算器,并提供代码示例。在开始之前,我们需要了解一些基本的HTML和CSS知识。计算器的界面可以使用HTML的表格元素来构建,然后用CSS进行样式设计。以下是一个基本的

在PHP中构建物业管理系统在PHP中构建物业管理系统Jun 11, 2023 am 10:34 AM

随着城市化进程的不断加快和人民生活水平的不断提高,物业管理行业也逐渐成为一个重要的领域。目前,物业管理系统已经成为了物业公司必备的工具,它可以帮助物业公司提高管理效率,优化服务质量,提升客户满意度。本文将介绍在PHP中构建物业管理系统的相关知识。一、物业管理系统的基本功能1.物业收费管理物业收费管理是物业管理系统的核心功能之一,它涉及到物业管理公司对于物业费

基于Swoole构建实时股票交易系统基于Swoole构建实时股票交易系统Aug 08, 2023 am 09:01 AM

基于Swoole构建实时股票交易系统随着互联网技术的发展,股票交易成为了越来越多个人投资者和机构投资者的选择。为了更好地满足投资者的需求,提供更实时、高效的股票交易服务,我们可以借助Swoole这个高性能的PHP扩展来构建一个实时股票交易系统。Swoole是一个基于C语言扩展开发的PHP网络通信框架,它提供了异步、并发、高性能的网络编程特性。使用Swoole

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

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools