codeigniter - use custom config value inside CI config file - codeigniter

I got custom.php (inside config folder) with code like below
$config['myurl'] = 'somesite.com';
I got config.php ( standard CI ), I want to set the base_url using the value from custom.php, like this
$config['base_url'] = $this->config->item('myurl');
Doing that I got the error
Using $this when not in object context in /Volumes/HD 2/work/vnl/app/config/config.php on line 18
Whats the right code for this purpose?

This is not going to work there. The $this that you would want to use there is a controller instance and that is not created yet when the standard application/config/config.php loads.
You can try to add a pre_system hook (that's loaded right before the config object) and include in some helper function that can be called and return the desired value in the application/config/config.php. The usual constants like APPPATH will be available then.
If you can make this config variable a constant that can work too (just put the define() inside application/config/constants.php). Since the config file is just a regular php source file, you can have conditionals here too if you must.

You can just work with the arrays.
In your custom config file
$config['myurl'] = 'myurl.com';
$config['base_url'] = &$config['myurl'];
This will change the default base_url once the custom config file is loaded, you can also just reset the base_url element in your custom config, however in my opinion it is not recommended to reset the base url in another config file as later in the project you could lose track of where it is set, why not set it (and maybe use conditions) in the main config file?

Related

RuboCop Error - Define a global variable in Cucumber RUBY files

I need to define a global variable in my Cucumber env.rb file which can be accessed throughout the framework in all step methods. Currently i am defining as this in my env.rb file:
$global_var ||= false
And i need to access this var into the Before hook as well After hook and few step methods where i am re-initializing this. It is working perfectly as i want. But the problem is, rubocop doesn't like this and throwing error as "do not use global variable". How can i resolve this ???
FYI, I tried using singleton to define this var as accessor and not quite sure where i am missing.
Change the config file for rubocop. use the link:
Example to Change
Look for the passage starting with When we look in the .rubocop_todo.yml file we see something like this: and also Configure Rubocop to be your style guide
Link to List of Configuration Changes possible:
Link to List of Styles
change .rubocop.yml file:
Style To Change:
GlobalVars: Enabled: false
Example File : Example file - how it looks like
How to Configure Style: Style/Inheritance Guide

how to get CI config set variable value?

how to get CI config set variable value?
I am working in CI. I have require base url in Controller file.
I am use base_url(); function but show error.
You can use constants for base url for eg.define('BASE_URL','http://domain.com') in constant.php
have you load the helper of url??
Go to (your project)->Source Files->application->config->autoload.php
then find
$autoload['helper'] = array();
and load url like this:
$autoload['helper'] = array('url');

How to properly use the CI-Merchant library's config file

I am trying to add items to merchant.php (the CI-Merchant library's config file, which I believe is auto-loaded by CodeIgniter). I want to be able to set the settings for the payment gateway (driver) I am choosing in the settings so that I don't have to write it out in every controller that is calling the library/driver and I do not want to hardcode the settings in the driver.
These are the settings I am trying to save, but I could have others with different gateways:
$config['authorize_net']['api_login_id'] = '***';
$config['authorize_net']['transaction_key'] = '***';
$config['authorize_net']['test_mode'] = TRUE;
$config['authorize_net']['developer_mode'] = TRUE;
However this is causing 2 warnings. The first:
A PHP Error was encountered
Severity: Warning
Message: stripos() expects parameter 1 to be string, array given
Filename: libraries/merchant.php
Line Number: 97
And the second:
A PHP Error was encountered
Severity: Warning
Message: strtolower() expects parameter 1 to be string, array given
Filename: libraries/merchant.php
Line Number: 103
So it seems to me that CodeIgniter is automatically passing the merchant.php config file to the library but it was not expecting it (which also confuses me, because in the CI-Merchant download it comes with the config file).
My worst case scenario would be to change the "default settings" in merchant_authorize_net.php the but I really want to avoid this, below is what that default settings function looks like.
public function default_settings()
{
return array(
'api_login_id' => '',
'transaction_key' => '',
'test_mode' => FALSE,
'developer_mode' => FALSE,
);
}
My questions are:
Is there a proper way to use the CI-Merchant's config file?
If not, is there a way to have driver-specific config files in CodeIgniter?
The config file is actually just there as boilerplate code which should probably be removed. CI-merchant itself doesn't have any logic to automatically read the config file for you.
Generally the recommended approach is to store the settings in your own config file (or environment variables), and then use those settings in your controller to initialize the library. You are correct that it's best to try and avoid editing anything inside the library folder.
If you are starting a new project I also recommend you check out Omnipay, the successor to CI-Merchant, as CI-Merchant will not be receiving any further development.

zend framework 2 Set TextDomain in onBootstrap

I followed the instructions of this link successfully, now my web is multilanguage without requiring put "locale" in the "traslate()" calls.
But I have to put the TextDomain each time that I call it.
$this->traslate("Hello", __NAMESPACE__) //where __NAMESPACE__ is the text domain.
I would like set TextDomain in the onBootstrap method instead of put it in each call of the the "traslate()" helper.
I have tried with setTextDomain method, but it doesn't exist.
Somebody know how do it?
The onBootStrap Code is following:
.....//Code for define $locale.
$sm = $e->getApplication()->getServiceManager();
$translator = $sm->get('translator');
$translator->setLocale($locale);
$traslator->SetTextDomain($textdomain); //This line not work!!!!!
Didn't see this right the first time. Going by DASPRIDS Presentation about ZF2 I18N the correct function to call is:
$this->plugin('translate')->setTranslatorTextDomain('module-b');
Though if i see this correctly, that's from within the view Scripts. Getting the Translator from ServiceManager however - i haven't tested this - but try the following:
$translator->getPluginManager()->get('translate')->setTranslatorTextDomain('foo');
Okey. We have advanced one step.
The first solution works ok (the view solution), now my web page traduce texts only using this helper parameters, being Locale and TextDomain defined by the config:
$this->translate('HELLO');
But the second solution not works. I don't understand because the same plugin is accepted in the view and not in the onBootstrap when the name is the same.
I rewrite my onBootstrap code bellow:
$translator = $e->getApplication()->getServiceManager()->get('translator');
$pm = $translator->getPluginManager(); //until here works ok.
$pm->get('translate'); //this throws an error message how if 'translate' not found.

Variable autoescape in Smarty templates

I have recently found out that Smarty, differently from Django template engine, does not escape variables automatically and I need to put |escape next to most of the variables in my templates.
Following the docs, http://www.smarty.net/docsv2/en/variable.default.modifiers.tpl I need to set default modifiers, needn't I?
So, here's my code:
$smarty = new Smarty();
$smarty->default_modifiers = array('escape:"htmlall"');
... and still variables ARE NOT escaped until I add |escape next to them.
What am I doing wrong?
If you are on Smarty 3, try this:
$smarty = new Smarty();
$smarty->loadFilter(Smarty::FILTER_VARIABLE, "htmlentities");
Tadà!
Update: Smarty::FILTER_VARIABLE is undocumented as of 28/11/2014. Use $smarty->escape_html = true if you want to stick to offical docs.
It appears that this feature was removed from Smarty v3, and docs are outdated. See:
http://www.smarty.net/forums/viewtopic.php?p=62207
I'd recommend a workaround - which is template level. Either create a new style v3 function to take care of filtration, or, do a simple include.
Include method
Put this in a clean.tpl file:
{$text|escape:htmlall}
Then invoke as {include file=clean.tpl text=$myvariabletofilter}
Function method
The new functions in Smarty could also take care of that:
{function clean}
{$text|escape:htmlall}
{/function}
And invoke as {clean text=$myvariabletofilter}
As always, make sure that these things get trimmed right and don't insert unncessary spaces.

Resources