Laravel 5 中使用define() 的常量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31799459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:06:47  来源:igfitidea点击:

Constants using define() in Laravel 5

phplaravellaravel-5bladelaravel-5.1

提问by user3089840

I have several large files of constants using the PHP define() function that I had working well in Laravel 4, but now I am upgrading an app to Laravel 5 and I'm not sure how to best carry these files over.

我有几个使用 PHP define() 函数的大常量文件,我在 Laravel 4 中运行良好,但现在我正在将应用程序升级到 Laravel 5,我不确定如何最好地携带这些文件。

The reason why I have more than one file for constants is because they are based on past, present, and future years (and they are large files with many constants). Here's an example from one of the files of constants:

我有多个常量文件的原因是因为它们基于过去、现在和未来的年份(并且它们是包含许多常量的大文件)。这是常量文件之一的示例:

<?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');
    }
}

I've tried doing the following: Adding the following block of code to my composer.json file:

我尝试执行以下操作:将以下代码块添加到我的 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"
    ]
  },

However, this didn't seem to work, probably since I have PHP conditional statements in my constants files.

但是,这似乎不起作用,可能是因为我的常量文件中有 PHP 条件语句。

I have also tried putting this into my base blade files for all of my views:

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

@include('constants.constants2013andearlier')
@include('constants.constants20142015')
@include('constants.constants20152016')

However, this didn't work either, as the files seem to not get read (I got errors saying "ErrorException in Course.php line 752: Use of undefined constant MY_CONSTANT - assumed 'MY_CONSTANT'" where Course.php is one of my Models.

但是,这也不起作用,因为文件似乎没有被读取(我收到错误消息“Course.php 第 752 行中的 ErrorException:使用未定义的常量 MY_CONSTANT - 假设为 'MY_CONSTANT'”,其中 Course.php 是我的一个楷模。

In my old Laravel 4 version of the project, I was defining these constants in the global.php file with the below code:

在项目的旧 Laravel 4 版本中,我使用以下代码在 global.php 文件中定义这些常量:

require app_path().'/constants2013andearlier.php';
require app_path().'/constants20142015.php';
require app_path().'/constants20152016.php';

Any suggestions? Thank you.

有什么建议?谢谢你。

采纳答案by user3089840

I'd still be interested in hearing any other ideas or potential issues with the solution I discovered below:

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

  1. I put my all of my constants files in a folder (named "constants") in the app directory.

  2. From the command line, I ran: php artisan make:middleware AddConstantsFilesToAllRoutes

  3. In the AddConstantsFilesToAllRoutes file, I added all of my constants files like this:

    require_once app_path('constants') . '/constants2013andearlier.php'; require_once app_path('constants') . '/constants20142015.php'; require_once app_path('constants') . '/constants20152016.php';

  4. In Kernal.php, I added my new middleware file above to the $protected middleware = []array which adds it to all possible routes:

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

  5. Be careful NOTto add the above code to the protected $routeMiddleware = []array or it will not add the constants files to all routes.

  6. Of course, if you just wanted to add your constants files to selective routes, then that would be an excellent solution to do that; however, you would need to manually add those routes to the constructor of each controller where you want those routes to have access to those constants files.

  7. Also, make sure that all of your controllers extend from the BaseController in order for the middleware to run on the routes for that controller.

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

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

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

    require_once app_path('constants') 。'/constants2013andearlier.php'; require_once app_path('constants') 。'/constants20142015.php'; require_once app_path('constants') 。'/constants20152016.php';

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

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

  5. 注意不要将上面的代码添加到protected $routeMiddleware = []数组中,否则它不会将常量文件添加到所有路由中。

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

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