PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 22, 2022

[FIXED] How can I define some constants for the whole of project?

 January 22, 2022     composer-php, configuration, constants, laravel, php     No comments   

Issue

Here is my directory (the screen-shot in this link). Also I have one more file named config.php which contains some variables, like this:

<?php

    // db
    $dbname = 'mydb';
    $db_username = 'root';
    $db_password = '';

    // login
    $l_email = 'some@one.com';
    $l_password = 'mypas'

    // details
    $sleep_on_error = 1; // per sec

    // and etc 
    // .
    // .
    // .

?>

As you see, I've some variables that are stored into config.php file .. I may need each of those variable in every part of my project. So I need to make config.php file accessible in the whole of project. So I can add config.php file into composer to make it accessible in the whole of project. like this:

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/classes/config.php"
    ]
},

All I'm trying to do is implementing a easy-configuration-system for my project. Can please tell me am I doing that correctly? Or should I define a class for doing that? or all I'm doing is wrong and I should do something else?

In short, what's the best structure to make a configuration for the project?


Solution

The Laravel way is using config files. Create my.php and put it in /config directory and then access constant values globally:

config('my.variable')

Here's an example of my.php:

<?php

return [
    'variable' => 15,
    'some-array' => [1, 2, 5],
];


Answered By - Alexey Mezenin
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing