来自环境的价值不呼应

时间:2019-07-10 06:59:36

标签: php laravel

我已经在laravel 5.0到5.8上研究laravel了很长时间,我目前在一个项目中使用 laravel 5.7 ,我在我的env文件中声明了一些常量,但是我无法打印在我的控制器中,它们是我的环境文件中的常量

    APP_NAME=Laravel
APP_ENV=production
APP_KEY=base64:Qm/Xwz4V9oJLhlk8RiY4usy4cdruFHRTe8iYkj7suMs=
APP_DEBUG=true
APP_URL=http://devmarketplace.finpay.pk
LOG_CHANNEL=stack

DB_CONNECTION=mysql

#crm database
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=marketplacecrm
DB_USERNAME=root
DB_PASSWORD=

#marketplace database
LIVE_READ_DB_HOST=localhost
LIVE_WRITE_DB_HOST=localhost

#marketplace database
LIVE_DB_DATABASE=marketplace
LIVE_DB_USERNAME=root
LIVE_DB_PASSWORD=

#marketplace logs
LIVE_LOGS_DB_HOST=localhost
LIVE_LOGS_DB_DATABASE=marketplacelogs
LIVE_LOGS_DB_USERNAME=root
LIVE_LOGS_DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

PAYROLL_BANKS=http://devpayrollplus.finpay.pk/public/apiV2/
UNEEB=UNEEB

现在我可以像在我的控制器中一样访问它们

public function index(){
        echo env("UNEEB");
        exit();
        return view("stores.stores");
    }

在我的环境中添加价值之后,我已经执行了许多步骤

php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan config:cache

但是它仍然无法正常工作,有趣的是我在laravel 5.8上有另一个项目,并且在它们上运行良好,这是我的composer.json和package.json,以防万一它们出了问题。

composer.json

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^7.1.3",
        "awobaz/compoships": "^1.1",
        "aws/aws-sdk-php-laravel": "^3.3",
        "barryvdh/laravel-dompdf": "^0.8.3",
        "donatello-za/rake-php-plus": "^1.0",
        "fideloper/proxy": "^4.0",
        "genealabs/laravel-model-caching": "^0.3.6",
        "guzzlehttp/guzzle": "^6.3",
        "laravel/framework": "5.7.*",
        "laravel/telescope": "^1.0",
        "laravel/tinker": "^1.0",
        "maatwebsite/excel": "^3.1",
        "spatie/laravel-activitylog": "^3.1",
        "spatie/laravel-database-mail-templates": "^2.0",
        "spatie/laravel-fractal": "^5.4",
        "spatie/laravel-permission": "^2.29"
    },
    "require-dev": {
        "barryvdh/laravel-debugbar": "^3.2",
        "beyondcode/laravel-dump-server": "^1.0",
        "filp/whoops": "^2.0",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^2.0",
        "phpunit/phpunit": "^7.0"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "files": [
            "app/Helpers/GeneralHelper.php"
        ],
        "psr-4": {
            "App\\": "app/",
            "Model\\": "app/Models",
            "Report\\": "app/Reports",
            "Settlement\\": "app/Settlements",
            "Batch\\": "app/Batches"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}

Package.json

{
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "npm run development -- --watch",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    },
    "devDependencies": {
        "axios": "^0.18",
        "cross-env": "^5.1",
        "laravel-mix": "^4.0.7",
        "lodash": "^4.17.5",
        "resolve-url-loader": "^2.3.1",
        "sass": "^1.15.2",
        "sass-loader": "^7.1.0",
        "vue-template-compiler": "^2.5.21",
        "webpack-cli": "^3.3.5"
    },
    "dependencies": {
        "@fortawesome/fontawesome-free": "^5.6.3",
        "bootstrap": "^4.2.1",
        "bootstrap-daterangepicker": "^3.0.3",
        "bootstrap-vue": "^2.0.0-rc.11",
        "css-loader": "^2.1.0",
        "datatables.net": "^1.10.19",
        "datatables.net-autofill-bs4": "^2.3.2",
        "datatables.net-bs": "^1.10.19",
        "datatables.net-bs4": "^1.10.19",
        "datatables.net-buttons-bs4": "^1.5.4",
        "datatables.net-colreorder-bs4": "^1.5.1",
        "datatables.net-fixedcolumns-bs4": "^3.2.6",
        "datatables.net-fixedheader-bs4": "^3.1.5",
        "datatables.net-keytable-bs4": "^2.5.0",
        "datatables.net-responsive-bs4": "^2.2.3",
        "datatables.net-rowgroup-bs4": "^1.1.0",
        "datatables.net-rowreorder-bs4": "^1.2.5",
        "datatables.net-scroller-bs4": "^1.5.1",
        "datatables.net-select-bs4": "^1.2.7",
        "fs": "0.0.1-security",
        "jquery": "^3.3.1",
        "jshint": "^2.10.2",
        "mdbootstrap": "^4.7.0",
        "moment": "^2.24.0",
        "nprogress": "^0.2.0",
        "popper.js": "^1.14.6",
        "vue": "^2.5.22",
        "vue-fullscreen": "^2.1.3",
        "vue-sweetalert2": "^2.1.0",
        "vue2-daterange-picker": "^0.1.6",
        "vuejs-datepicker": "^1.5.4"
    }
}

4 个答案:

答案 0 :(得分:2)

遵循这些步骤

将常量添加到 .env 中,如
select accountCode,sum(allocationBudget) from T1 group by accountCode having SUBSTRING(accountCode,1,len(accountCode))=accountCode

将其添加到您的ADMIN_EMAIL=some@email.com
config/app.php

获取您的价值
'admin_email' => env('ADMIN_EMAIL', 'default@email.com'),

答案 1 :(得分:2)

如果您正在使用.env加载新更改,请确保在php artisan serve中进行新更改后已停止服务器一次。

由于默认设置,它将运行服务器启动时给定的配置。

因此,要从.env加载每个新更改,您必须终止该过程并重新运行php artisan serve

已编辑:

还要确认,如果.env中包含spaces or special charectors的任何变量,请确保将它们包装在double quotes中。

答案 2 :(得分:0)

奇怪,您执行的那些命令应该已经解决了问题……无论如何,Laravel对.env文件有点敏感,因此对于您的用法,我将更改常量的方法。

Laravel建议仅在配置文件中使用env()。尝试在代码中使用config()助手,而不要使用env()。将常量移到新文件中,然后可以在代码中调用config('blah-blah-name')

希望这会有所帮助。

LE:如果您使用php artisan serve,则可能是造成问题的原因。

答案 3 :(得分:0)

您正在呼叫

 echo env("UNEEB")

,同时在环境文件中用3个大写E声明。