How can i declare a global array in constants.php?
I want use an array of this form in a helper file.
$process['up_position']['Title']='Up position';
$process['up_position']['icon']='icon-up';
$process['up_position']['description']='Up a position';
$process['down_position']['Title']='Down position';
$process['down_position']['icon']='icon-down';
$process['down_position']['description']='Down a position';
What is the better way?
Thank you!!
I see constants.php but all constants are simple values
Constants are simple values in general. These are values that never change.
My own opinion is
Option 1 : to have these in the Config file if they are system wide settings.
Option 2 : to have them in a table if you ever want to change them.
Option 3 : to have them in the super class of your Controller or Model so you have them inherited in every file you create, you don't need to assign them anymore but just use them as they are.
Option 4 : to have them in a library.
It all depends where you read / use them from. I see you have "title", "icon" and "description" so these seem to be objects used in a view, I'd assume. Then you can create a controller super class where objects like that are created, you just assign the values to them once you call the view and the view will have the values.
Related
I have an application developed with Laravel. My software has settings that are used globally and should be available in all controllers (such as default information). I take this information from the database in the main controller every time a request is sent and save it in a variable.
namespace App\Http\Controllers;
class Controller extends BaseController
{
protected $config;
public function __construct()
{
$this->config= DB::table('config')->get();
}
}
Is there a way to save and use this information without the intervention of a database? I don't want to use sessions.
It is better if a solution is introduced using laravel packages.
Thanks
Assuming that you collection doesn't hold a lot of data, you can always put it inside your custom config. Create a php file inside your app/config directory, where you can put all your values like this:
<?php
return [
'key1' => value1,
'key2' => value2,
];
You can create any data structure here that you might need. Now, when you need to read single key from this data, you can use Laravel's config() helper:
$config = config('config_name.key');
If you want to get whole collection of the data, you can do it with the same config() helper, like this:
$config = config('app.config_name');
Hope that I understood your question right, and that this can lead you in right direction. You can read more about config on official documentation.
I have yii language localization turned ON, so I use in config 'language'=>'en',
and write things like Yii::t('layouts_main','Home') and store translations of Home in php file.
But apart of this, I have tables, and there are data inside in different languages.
For example I have a list of countries which must be stored in mysql table in different languages.
Table structure is very simple: id, name_en,name_de,name_es etc...
I did it that way so if language change those 2 letters must be controlling from which sell to read the name.
In my controller I get my data from table to array
$tableCountry = Country::model()->findAll();
Then I'm making such variable which will containt "name+" language variable which is 'en' in config
$name_lang ="name_".Yii::app()->Language;
So now I made variable $name_lang which contains name_en
Then I choose the right sells using $name_lang variable
$list=CHtml::listData($modelCountry,'id',$name_lang);
Using $name_lang only name_en data will go to List. So later I can switch settings of language in config file to "de" and only name_de data will fgo to List.
So everything is fine here.
But what If I have complex table which contains country_name_en, city_name_en,region_name_en etc...
To put this data into list I have to make 3 or more variables
$country_name_lang ="country_name_".Yii::app()->Language;
$city_name_lang ="city_name_".Yii::app()->Language;
$region_name_lang ="region_name_".Yii::app()->Language;
So I wonder is there any other better way to do such things ?
Option 1: You can define in the model another variable called country_name (without any suffix). You can do this just in the model, you do not have to create a field in the db too. No need for validation or anything for this variable.
Then in your after find method for the model you can do
public function afterFind()
{
$this->country_name = $this->{"country_name_".Yii::app()->Language};
}
Feel free to add everything here.
Now everywhere you should be able to use $model->country_name without any care what the language is.
Option 2 would be to use magic methods. you can read more from them here: http://www.php.net/manual/en/language.oop5.magic.php
Define for your model such a method:
public function __call($name, $arguments)
{
if(isset($this->{$name. "_".Yii::app()->Language}))
return $this->{$name. "_".Yii::app()->Language}
elseif(isset($this->$name))
return $this->{$name}
else
throw new Exception(xxx, 'We couldn\'t find the variable');
}
Now you should be able to use $model->country_name() and you would again get the variable in the proper language. You can use also $model->id() and you would get the proper id too as it has a fallback if there is no id_en field.
I am new to codeigniter , In my program i want a variable need to be accessed by multiple controllers,
It's not a constant variable, value of variable changes ,
Sorry , My mistake
I want to store a JSON object to be precise
Pls help me to figure this out.
Thanks in advance.
You can create a base controller with an attribute for your variable, then have your controllers extend that base controller.
Option 1
Since you are using CodeIgniter and sessions then something like this could work out for you:
set it
$someJSONobject = 'JSON';
$this->session->set_userdata('GLOBAL_JSON', $someJSONobject);
retrieve it
$someJSONobject = $this->session->userdata('GLOBAL_JSON');
echo $someJSONobject->subitem;
Make sure you are storing sessions in a DB if you go with this option because Cookie space is VERY limited
Option 2
Even if you are not using CodeIgniters' session implementation then you can do something quite similar in native PHP:
$someJSONobject = 'JSON';
$_SESSION['GLOBAL_JSON'] = $someJSONobject;
Appending on Rooneyl's solution you may want to save that value on session which is easier to access from all end
Session docs
I have just started playing with laravel haveing come from codeigniter and I am trying to work out the best way to define a series of constants.
CI uses a constants folder under app/config and I was largely happy with this approach for most things but wanted advice as to the best way to do this in Laravel.
My constants fall into 3 categories and I would like advice if possible on how best to store and retrieve each (baring in mind I'm completely new to Laravel.
Type 1:
Constants which need to be loaded everytime a controller is called:
e.g. I would like to start by defining a constant to tell me if the user is requesting content via ajax, this is what I used to do in CI constants file:
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
Type 2:
Constants which can be changed by the user:
Are these best stored in a database or would writing to a config file be a better option? Again looking for advice on how to store and retrieve
Type 3:
Constants which are only needed in specific controllers:
Ideally I would like to be able to group constants into arrays or separate files and pull them out in groups as required.
e.g. I could just retrieve my upload settings for my upload controller.
Thanks for any help/advice in advance - examples would be greatly appreciated
Type 1
You can add any constant in Bundle (or application) start.php file.
Type 2 and 3
I would suggest using Config for both these requirement.
My solution is create a file inside app/config folder called "constants.php"
app/config/constants.php
The code inside is in array way.
"some value",
'c2'=>"some value"
);
?>
then inside each controller you can retrieve using
Config::get('constants.c1');
I think is an easy way to do it, it's more scalable.
<?php
//file : app/config/constants.php
return [
'IS_AJAX' => isset($_SERVER['HTTP_X_REQUESTED_WITH']
];
in anywhere:
echo Config::get('constants.IS_AJAX');
I do one request and my URL has a parameter like this .../index.php?customer=abc
In index.php's class $_GET['customer'] is available.
There are multiple other classes being created then.
Finally in somefile.php containing some different class someClass, $_GET['customer'] is no more available.
I am forced to use a framework that uses a form that eval()s PHP code on button click.
new TDynButton($body, "login", ... , "\$this->win->doLogin();");
IndoLogin() there is no $_GET['customer']. Cannot understand why. Is it possible if this framework uses action=GET in the background that I am losing my $_GET? Im totally lost.
Thanks.
My approach would be to give the information in $_GET['customer'] to the instatiated object by passing it in the constructor and store it in a private member. This way you have the information needed and no direct access to $_GET is nessessary. This is anyway a better design I think.
$_GET is a global variable that will be available throughout the script you use it in. You have to pass it to the script, though - such as somefile.php?customer=peter
Yes, $_GET is a superglobal variable that is available in all PHP scripts.
And yes, generally, framework convert/sanitaze the GET/POST arrays and clears them.