How to Define and Use Global Constants in Laravel 5.1 on Linux
This article explains two ways to define and use global constants in Laravel 5.1, including creating a constants.php file in the config folder and modifying bootstrap/autoload.php, and notes the required file permissions for Linux servers.
Note: This guide assumes Laravel version 5.1.
Method 1 (works locally, may fail on server)
Create a constants.php file in the config directory with the following content:
<code><?php
return [
'URI' => 'baidu.com',
];
</code>Access the constant using:
<code>echo Config::get('constants.URI');</code>Method 2 (requires file permission change)
Add a require statement at the end of bootstrap/autoload.php :
<code>require __DIR__.'/constants.php';</code>Create constants.php under the bootstrap directory with:
<code><?php
define('URI', 'xxx.com.cn');
</code>Use the constant with:
<code>echo URI;</code>On Linux servers, ensure the constants.php file has permission 755 for it to work.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.