Reputation: 3323
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.
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:
"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.
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.
In my old Laravel 4 version of the project, I was defining these constants in the global.php file with the below code:
require app_path().'/constants2013andearlier.php';
require app_path().'/constants20142015.php';
require app_path().'/constants20152016.php';
Any suggestions? Thank you.
Upvotes: 2
Views: 7411
Reputation: 3323
I'd still be interested in hearing any other ideas or potential issues with the solution I discovered below:
I put my all of my constants files in a folder (named "constants") in the app directory.
From the command line, I ran: php artisan make:middleware AddConstantsFilesToAllRoutes
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';
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,
Be careful NOT to add the above code to the protected $routeMiddleware = []
array or it will not add the constants files to all routes.
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.
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.
Upvotes: 1