【问题标题】:Constants using define() in Laravel 5在 Laravel 5 中使用 define() 的常量
【发布时间】:2015-10-26 06:40:49
【问题描述】:

我有几个大型常量文件,使用我在 Laravel 4 中运行良好的 PHP define() 函数,但现在我正在将应用程序升级到 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 条件语句。

我还尝试将其放入我的所有视图的基本刀片文件中:

@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';

有什么建议吗?谢谢。

【问题讨论】:

  • 您定义的常量的真实示例是什么?这里肯定有一点代码味道
  • 我举了一个常数的例子,因为我有很多相似的常数,重复更多相同的没有意义。它们都看起来像这样:define('MY_CONSTANT', 'This is an example of a constant');不用说,我的常量文件本身没有任何问题,因为它们在我的旧 Laravel 4 版本的应用程序中完美运行。
  • 我更感兴趣的是你在常量中存储了什么样的值。如果它是简单的文本字符串,您可能会更好地使用 laravel 中的 Lang 构造
  • 只有字符串、整数和小数存储为常量。

标签: php laravel laravel-5 blade laravel-5.1


【解决方案1】:

我仍然有兴趣了解我在下面发现的解决方案的任何其他想法或潜在问题:

  1. 我将我所有的常量文件放在了 app 目录中的一个文件夹(名为“constants”)中。

  2. 我从命令行运行:php artisan make:middleware AddConstantsFilesToAllRoutes

  3. 在 AddConstantsFilesToAllRoutes 文件中,我添加了所有常量文件,如下所示:

    require_once app_path('constants') 。 '/constants2013andearlier.php'; 需要一次 app_path('constants') 。 '/constants20142015.php'; 需要一次 app_path('constants') 。 '/constants20152016.php';

  4. 在 Kernal.php 中,我将上面的新中间件文件添加到 $protected middleware = [] 数组中,该数组将其添加到所有可能的路由中:

    \App\Http\Middleware\AddConstantsFilesToAllRoutes::class,

  5. 小心不要将上述代码添加到protected $routeMiddleware = [] 数组,否则不会将常量文件添加到所有路由。

  6. 当然,如果您只是想将常量文件添加到选择性路由,那么这将是一个很好的解决方案;但是,您需要手动将这些路由添加到您希望这些路由可以访问这些常量文件的每个控制器的构造函数中。

  7. 另外,请确保您的所有控制器都从 BaseController 扩展,以便中间件在该控制器的路由上运行。

【讨论】:

  • 您应该将此移植到服务提供商。您可以将其包含在AppServiceProvider 的引导方法中,或者将其包含在新的ConstantServiceProvider 中。将包含集中在服务提供商内部是一个好主意。
  • 您介意扩展您的答案吗?如果我只是将以下代码放在 AppServiceProvider 的引导方法中,我只会再次获得未定义的常量: require_once '/constants/constants2013andearlier.php'; require_once '/constants/constants20142015.php'; require_once '/constants/constants20152016.php';
  • 应该可以了,我在AppServiceProvider的引导方法中包含了一些带有常量的文件,并且常量显示正确。
  • 我在AppServiceProvider的引导方法中只有require app_path('Library') .'/constants1.php'
  • 是的,Laravel 5.1。 constants1.php 有一个 if/else 代表 Auth::check 并且 define 与您的相同。
猜你喜欢
  • 2012-03-28
  • 1970-01-01
  • 2018-10-19
  • 2013-10-23
  • 2013-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多