在 Laravel 5 中使用 define() 的常量
Constants using define() in Laravel 5
我有几个大的常量文件使用 PHP define() 函数,我在 Laravel 4 中运行良好,但现在我正在将应用程序升级到 Laravel 5,并且我我不确定如何最好地携带这些文件。
我有多个常量文件的原因是因为它们基于过去、现在和未来年份(而且它们是包含许多常量的大文件)。这是来自常量文件之一的示例:
<?php
//Check to see if the user is logged in.
// If no user, we can't know what catalog we should use
if (Auth::check()) {
//The most recent catalog year will serve as the default for anyone
//who signs up for a catalog year that is newer than the most recent
//catalog year.
if (Auth::user()->strg_4 == "2015 - 2016" ||
intval(substr(Auth::user()->strg_4, 0, 4)) > date("Y")) {
//Constants for Create course/Edit course pages
define('MY_CONSTANT', 'This is an example of a constant');
}
}
我已尝试执行以下操作:将以下代码块添加到我的 composer.json 文件中:
"autoload": {
"classmap": [
"database",
"app/Http/Controllers",
"app/Models",
"app/myclasses"
],
"psr-4": {
"App\": "app/"
},
"files": [
"app/constants2013andearlier.php",
"app/constants20142015.php",
"app/constants20152016.php"
]
},
然而,这似乎不起作用,可能是因为我的常量文件中有 PHP 个条件语句。
我也曾尝试将其放入我的基础 blade 文件中以用于我的所有观点:
@include('constants.constants2013andearlier')
@include('constants.constants20142015')
@include('constants.constants20152016')
然而,这也没有用,因为文件似乎没有被读取(我在 Course.php 行 752 中收到错误消息“ErrorException:
使用未定义常量 MY_CONSTANT - 假定 'MY_CONSTANT'" 其中 Course.php 是我的模型之一。
在我的项目的旧 Laravel 4 版本中,我使用以下代码在 global.php 文件中定义这些常量:
require app_path().'/constants2013andearlier.php';
require app_path().'/constants20142015.php';
require app_path().'/constants20152016.php';
有什么建议吗?谢谢。
我仍然有兴趣听听我在下面发现的解决方案的任何其他想法或潜在问题:
我将我所有的常量文件放在应用程序目录中的一个文件夹(名为 "constants")中。
从命令行,我运行:php artisan make:middleware AddConstantsFilesToAllRoutes
在 AddConstantsFilesToAllRoutes 文件中,我添加了所有常量文件,如下所示:
require_once app_path('constants') 。 '/constants2013andearlier.php';
require_once app_path('constants') 。 '/constants20142015.php';
require_once app_path('constants') 。 '/constants20152016.php';
在 Kernal.php 中,我将上面的新中间件文件添加到 $protected middleware = []
数组中,该数组将其添加到所有可能的路由中:
\App\Http\Middleware\AddConstantsFilesToAllRoutes::class,
注意不要将以上代码添加到protected $routeMiddleware = []
数组,否则它不会将常量文件添加到所有路由。
当然,如果您只是想将常量文件添加到选择性路由,那将是一个很好的解决方案;但是,您需要手动将这些路由添加到每个控制器的构造函数中,您希望这些路由可以访问这些常量文件。
此外,请确保所有控制器都从 BaseController 扩展,以便中间件在该控制器的路由上 运行。
我有几个大的常量文件使用 PHP define() 函数,我在 Laravel 4 中运行良好,但现在我正在将应用程序升级到 Laravel 5,并且我我不确定如何最好地携带这些文件。
我有多个常量文件的原因是因为它们基于过去、现在和未来年份(而且它们是包含许多常量的大文件)。这是来自常量文件之一的示例:
<?php
//Check to see if the user is logged in.
// If no user, we can't know what catalog we should use
if (Auth::check()) {
//The most recent catalog year will serve as the default for anyone
//who signs up for a catalog year that is newer than the most recent
//catalog year.
if (Auth::user()->strg_4 == "2015 - 2016" ||
intval(substr(Auth::user()->strg_4, 0, 4)) > date("Y")) {
//Constants for Create course/Edit course pages
define('MY_CONSTANT', 'This is an example of a constant');
}
}
我已尝试执行以下操作:将以下代码块添加到我的 composer.json 文件中:
"autoload": {
"classmap": [
"database",
"app/Http/Controllers",
"app/Models",
"app/myclasses"
],
"psr-4": {
"App\": "app/"
},
"files": [
"app/constants2013andearlier.php",
"app/constants20142015.php",
"app/constants20152016.php"
]
},
然而,这似乎不起作用,可能是因为我的常量文件中有 PHP 个条件语句。
我也曾尝试将其放入我的基础 blade 文件中以用于我的所有观点:
@include('constants.constants2013andearlier')
@include('constants.constants20142015')
@include('constants.constants20152016')
然而,这也没有用,因为文件似乎没有被读取(我在 Course.php 行 752 中收到错误消息“ErrorException: 使用未定义常量 MY_CONSTANT - 假定 'MY_CONSTANT'" 其中 Course.php 是我的模型之一。
在我的项目的旧 Laravel 4 版本中,我使用以下代码在 global.php 文件中定义这些常量:
require app_path().'/constants2013andearlier.php';
require app_path().'/constants20142015.php';
require app_path().'/constants20152016.php';
有什么建议吗?谢谢。
我仍然有兴趣听听我在下面发现的解决方案的任何其他想法或潜在问题:
我将我所有的常量文件放在应用程序目录中的一个文件夹(名为 "constants")中。
从命令行,我运行:
php artisan make:middleware AddConstantsFilesToAllRoutes
在 AddConstantsFilesToAllRoutes 文件中,我添加了所有常量文件,如下所示:
require_once app_path('constants') 。 '/constants2013andearlier.php'; require_once app_path('constants') 。 '/constants20142015.php'; require_once app_path('constants') 。 '/constants20152016.php';
在 Kernal.php 中,我将上面的新中间件文件添加到
$protected middleware = []
数组中,该数组将其添加到所有可能的路由中:\App\Http\Middleware\AddConstantsFilesToAllRoutes::class,
注意不要将以上代码添加到
protected $routeMiddleware = []
数组,否则它不会将常量文件添加到所有路由。当然,如果您只是想将常量文件添加到选择性路由,那将是一个很好的解决方案;但是,您需要手动将这些路由添加到每个控制器的构造函数中,您希望这些路由可以访问这些常量文件。
此外,请确保所有控制器都从 BaseController 扩展,以便中间件在该控制器的路由上 运行。