I want to know a clean way of defining Application Constants in Codeigniter. I don't want to change any native file of codeigniter. Hence I don't want to define it in application/config/constants.php as when I need to migrate to newer version of code-igniter I will not be able to copy the native files of codeigniter directly.
I created a file application/config/my_constants.php and defined my constants there. 'define('APP_VERSION', '1.0.0');'
I loaded it using $this->load->config('my_constants');
But I am getting a error
Your application/config/dv_constants.php file does not appear to contain a valid configuration array.
Please suggest me a clean way of defining application level constants in code-igniter.
Not using application/config/constants.php is nonsense! That is the only place you should be putting your constants. Don't change the files in system if you are worried about upgrading.
just a complete answer. (None of the answers show how to use the constants that were declared)
The process is simple:
Defining a constant. Open config/constants.php and add the following line:
define('SITE_CREATOR', 'John Doe')
use this constant in another file using:
$myVar = 'This site was created by '.SITE_CREATOR.' Check out my GitHub Profile'
Instead of using define(), your my_constants.php file should look something like this:
$config['app_version'] = '1.0.0';
Be careful with naming the array key though, you don't want to conflict with anything.
If you need to use define(), I would suggest doing it in the main index.php file, though you will still need to use APP_VERSION to get the value.
config file (system/application/config/config.php) to set configuration related variables.
Or use
constant file (system/application/config/constants.php) to store site preference constants.
=======================
DEFINE WHAT YOU WANT
=======================
$config['index_page'] = 'home';
$config['BASEPATH'] = 'PATH TO YOUR HOST';
Please refer this:
http://ellislab.com/forums/viewthread/56981/
Define variable in to constants & add value on array
$ORDER_STATUS = array('0'=>'In Progress','1'=>'On Hold','2'
=>'Awaiting Review','3'=>'Completed','4'
=>'Refund Requested','5'=>'Refunded');
You can accomplish your goal by adding constants to your own config file, such as my_config.php.
You would save this file in the application/config folder, like this:
application/config/my_config.php.
It is very common to have a separate config file for each application you write, so this would be easy to maintain and be understood by other CI programmers.
You can instruct CI to autoload this file or you can load it manually, as needed. See the CI manual on "Config class".
Let me suggest that you use composer.json to autoload your own Constants.php file, like this:
Related
In laravel I need to create variable in env file. According to documentation and some relevant threads (got after googling) I need to do add variables in .env file
I have tried following
SANDBOX_PAYPAL_CLIENT_ID=7I3D9
SANDBOX_PAYPAL_CLIENT_SECRET=S2E4C5R6E7T
I am confused that after adding here variable I just need to call then into controller or I need to setup again in somewhere ./config/ directory? What is best practice, can someone guide me about that. I would like to appreciate. Thank you
You should put env variables in .env file, but make sure to only use env() calls in configuration files (the ones under /config) to retrieve their values.
Keep in mind that if you cache the configuration (php artisan config:cache) any env() will return null as Laravel will no longer load .env files.
That's also stated in to the documentation
You can get .env variables using the env() helper function.
Most packages get these values in their conf file to group the configurations.
You can then use the config() helper function to get the value.
However you implement this is up to you.
It depends on how you want to use them. Maybe create a file with name ConfigVariables.php declare all variables of env there.
1) You can have all of them at one place. Centralized control of all env variables at one place.
2) Take care of null checks etc, if config doesn't exit, pass some default value
3) This will help you to have one standard being used for all
4) If you use env directly in the code, you will have to check and take care of the null values (in case if there is no variable in the config)
Just to add a point here, Strings with spaces could be an issue if you do not use ""
You can test it:
Email_Sender_Name=Danyal Sandeelo
echo it as: echo env("Email_Sender_Name");
Email_Sender_Name="Danyal Sandeelo"
echo it as: echo env("Email_Sender_Name");
You can read this one: Laravel 5.2 not reading env file
I have created a new storage disk for the public uploads. I use it like this:
Storage::disk('uploads')->get(...);
But I am trying to figure out a way to get the path to the disk itself, I have spent some considerable time wondering between the framework files, but couldn't find what I am looking for. I want to use it in a scenario like so:
$icon = $request->file('icon')->store('icons', 'uploads');
Image::make(Storage::disk('uploads')->path($icon))->resize(...)->save();
So, how to get a storage disk's path ?
Update 2022
You can get the path from the config:
config('filesystems.disks.uploads.root')
Anyway, I keep the old answer, as it still works and is not wrong:
There is a function for this!
Storage::disk('uploads')->path('');
https://laravel.com/api/5.8/Illuminate/Filesystem/FilesystemAdapter.html#method_path
Looks like this has been there since 5.4! I never noticed...
After extra search I found that I can use the following:
$path = Storage::disk('uploads')->getAdapter()->getPathPrefix();
But still, isn't a "->path()" method is a given here or what!
We can also access the disk storage path from configuration helper directly:
config('filesystems.disks');
There are helper functions in the framework now that can get some of the commonly used paths in the app fairly easily.
https://laravel.com/docs/5.8/helpers
For example, if you wanted to access a file from the public folder you could do the below.
$image_file_path = public_path("images/my_image.jpg")
This will return you a local path for a file located in your app under the public/images folder.
Laravel Default Method
return Storage::disk('disk_name')->path('');
Get List of All Directories using disk name
Storage::disk('disk_name')->directories()
I am beginner for php and codeigniter.
I have a controller and other background php script file. From controller, i wish to set constants for username and password after user logged in. These will be stored in constant.php file. Now, as i said, i wish access these constants from background php script. This php script file is non-controller file which is stored in same directory in which controller is stored.
Or is there any way to store these username ans password somewhere and i can acess those from non-controller php files. This file contains the function definitions which i called them from controllers.
Store them in config/config.php e.g. $config['currency_symbol'] = "$"; then you can use in your application with $this->config->item('currency_symbol')
The answer of Nav Jav is the recommended way when you create global constants and functions. It is what MVC means.
Anyway, you can put your constant.php in your "application" folder. For example your constant.php is something like:
//File name constant.php, path is application\myfolder\constant.php
<?php
$test = 1;
function test($string){
return $string.' got';
}
?>
In the views or controllers or models where you want to use the constants and the functions, you can do:
<?php
require_once(APPPATH.'myfolder/constant.php');
echo test('string1'); //which will give you 'strint1 got'
echo $test; //which will give you 1
?>
Again, this is not recommended, you should follow Nav Jav's way.
I am new to this zend2, previously I worked with CAKEPHP & codeigniter. I want to write some constant values in a particular file & be able to access them any where in the project.
In cakephp it is like Configure::write('environment','dev'); we write this in a file in Config folder which will be at app/Config/file name
and we can access this like $env = Configure::read('environment'); any where..
Can we do in the same way in zend framework 2, like defining the constants in a file & can access them anywhere..?
Please give an example how to define & read it with the path of the file
No is the short answer. Cake, ZF1, CodeIgniter all made use of a design pattern, now widely discouraged, called the the Registry Pattern (which is really just a Singleton).
The very fact that this class is globally accessible, is one of the reasons why its use is not advised.
ZF2 has a completely different architecture and offers a flexible approach by merging configuration based on environment variables. An when it comes to 'using' the configuration, you should be injecting it into your services using the service manager and a service factory.
First define variable in config
Like:-
accountUser.admin = "admin"
Then initialize object like(in controller or model):-
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/config.ini', APPLICATION_ENV);
// APPLICATION_PATH is path to project
// APPLICATION_ENV will be your environment
Then use it like:-
$admin = $config->accountUser->admin;
What's the easiest way of storing a single number on a server so that any script can access it, using PHP? Currently, I have a number stored in a file, but this seems somewhat inelegant.
There's no right answer here, but most modern PHP systems for building web applications have some kind of Configuration object. This is often implemented as a singleton
//brain dead config object
class NamespaceConfiguration {
public static function getInstance() {
if (!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}
public static function set($key,$value){
//code to set $key/$value paid
}
public static function get($key){
//code to get a $value based on a $key
}
}
$config = NamespaceConfiguration::getInstance();
$config->set('myNumber',42);
....
function somewhereElse(){
$config = NamespaceConfiguration::getInstance();
$myNumber = $config->set('myNumber');
}
This class is loaded on every page requiest. This gives every developer a standard API to call when they want to get or set single configuration values, and allows a single developer to control the where of storage and the how of retrieval (which may be a flat file, XML file, memory cache, MySQL database, XML file stored in a MySQL Database, XML File stored in a MySQL Database that contains a node which points to a file that contains the value, etc.)
The where and how of retrieval is going to depend on your application environment, although by using a configuration object you can create some efficiencies up front (storing already retrieved values in a property cache, pre-fetching certain values on instantiation, etc.)
Since your application probably already have some sort of include-file on the top "header.php" or simular, you could just create a constant/variable in that file, and all the files that include the header will also have access to the constant.
This may help you to define the constant:
http://php.net/constant
That depends on the characteristics of the number. Is it updated/modified often? Is it the only such number? If it isn't changed it's probably better to do as Espo suggests and store it as a php constant that can be included when necessary. If you have other such numbers you can put them all in that file. If they are updated often it's probably better to put it in the database.
But. If it's a single number, that is subject to change, and you don't forsee any need for storing other numbers, why not use a file? Just remember to use flock() when updating it to avoid concurrency issues.
Your best bet would be to put it in a MySQL table for fetching later. That's probably the best way to store information in PHP.
If it is a variable that is more of a environmental nature you could always use set those in Apache
in your httpd.conf file you can set the following:
SetEnv myVar myValue
You can then use $_SERVER to fetch it
$_SERVER['myVar']
I usually set varibles on this if i am to setup same application on a few different virtual hosts but dont want them to have different parameters in the config file.
A config file will also meet your needs.
There is some easy to use classes in Zend framework that can help out
http://framework.zend.com/manual/en/zend.config.html
more specific section 7.3 and 7.4 describes how you can write config parameters in a plain text file or in XML.
if you prefer plain old php you have the function parse_ini_file that lets you read in config parameters from a text file
http://us.php.net/parse_ini_file
You can also use the Alternative PHP Cache or other caching options.
http://www.php.net/manual/en/function.apc-add.php
And you can use this in combination with the other solutions listed above, e.g saving a config to cache.