简体   繁体   English

Laravel:在视图中使用config文件夹中的变量文件

[英]Laravel: Use variable file from config folder in views

I have a vars.php file in ./config/vars.php : 我在./config/vars.php有一个vars.php文件:

<?php
    return
    [
        'MINIMUM_PASSWORD_LENGTH' => 6
    ];
?>

I need to be able to access the variables in this file inside of other *.blade.php files and *.js files. 我需要能够访问该文件中其他*.blade.php文件和*.js文件中的变量。

I have looked around but haven't been able to find an answer to this. 我环顾四周,但无法找到答案。 I have tried min: Config::get('vars.MINIMUM_PASSWORD_LENGTH'), (Full code below) but this didn't work. 我已经尝试了min: Config::get('vars.MINIMUM_PASSWORD_LENGTH'), (下面的完整代码),但这没有用。 I think I have to do some kind of include inside my master layout file, then it will be able to be accessed in all my pages (Since they all come from the master layout base). 我认为我必须在主布局文件中进行某种包含,然后才能在我的所有页面中访问它(因为它们全部来自主布局库)。

register.js, used for my register modal: register.js,用于我的注册模式:

register_password: {
    validators: {
        notEmpty: {
            message: 'Password is required'
        },
        stringLength: {
            min: MINIMUM_PASSWORD_LENGTH,
            message: 'Must be at least 6 characters'
        },
        identical: {
            field: 'register_confirm',
        }
    }
},

Try config('vars.MINIMUM_PASSWORD_LENGTH') - it works for me. 试试config('vars.MINIMUM_PASSWORD_LENGTH') -它对我config('vars.MINIMUM_PASSWORD_LENGTH')

If you want to use it in .js scripts, I guess the best way would be use cookies: 如果您想在.js脚本中使用它,我想最好的方法是使用cookie:

Setting cookie with Laravel 使用Laravel设置cookie

Reading cookie with JavaScript 使用JavaScript读取Cookie

You could also generate .js scripts dynamically, but that's awful practice. 您还可以动态生成.js脚本,但这是很糟糕的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM