How to access language file inside config? - php

How we can use language file inside config.php php codeigniter?
I searched to find solution just from out side the config we can set item in config to use language file for setting config item value, but how we can set config item using language file inside config file?
for example i want to do as bellow:
$config['item'] = array(
'1' => $ci->lang->line('my_name')
);

You use wrong way. You try make view function in config file. You can make this in template or may be in model.
You should change your application architecture.

you can't access ci object in config file because this is load before CI_Loader class.
but you can update config.php field later on run time, using $this->config->set_item('key', 'value'); function in controller/model/library
sample code in controller __construct(){} method
public function __construct(){
$this->config->set_item('item', array('1' => $this->lang->line('my_name')));
}
Another way use hooks to update item globally, more detail available on CI documentation.

Related

Extend blade engine in Laravel to use different views directory

I am working on a Laravel project and I am very new to it. For now, I want to use blade templates to render views but I want it to search for views in different directories like <custom_dir>\views instead of default resources/views.
The <custom_dir> will be dynamic (it can be a variable).
Any ideas? I was thinking of a custom service provider and then extend the default function which renders views in Laravel inside it. But not sure how to implement it.
Edit:
I have user this link to extend the default functionality of include function in blade template engine. But this overrides the include functionality. I want to change the path and then call the default blade functionality
You could probably append the path to the configuration:
1) Statically, by modifying file config/view.php
'paths' => [
realpath(base_path('resources/views')),
//more paths here
],
2) Dynamically at runtime:
$paths = config('view.paths');
$paths[] = $newPathToAdd;
config(["view.paths" => $paths ]);
I suggest you use this in moderation otherwise you will just end up with a mess of directories with no real specified purpose.
You can create custom directories in resources\views directory and use them with something like this:
return view($customDirectory.'.index');
Where index is a template inside custom directory.

How to get a migration config item in CodeIgniter?

As usual, to access CodeIgniter's configuration item from a $config array, I would use this function $this->config->item('some_item');. That's fine, but how can I access a config items from this file app/config/migration.php ? where $config['migration_enabled'] is located
I'm doing a $this->config->item('migration_enabled') or $this->config->item('migration_version'), but it returns me always FALSE, anyone know why ? And how can I work it out to make it accessible within the controller ?
You need to load the migration config before you access it. You can either load it on demand in your controller via:
$this->config->load('migration');
or you can autoload it in your config/autoload.php file by adding it to the $autoload['config'] array:
$autoload['config'] = array('migration');

Creating Config File for Custom Library in CodeIgniter

I've looked through all of the CI documentation, and done some Googling of it, but I still can't quite seem to figure out how to create a configuration file for a custom library in codeigniter. If somebody could even just point me in the direction of where in the docs I could find my answer it would be greatly appreciated.
I am creating a library in CI that makes use of several database columns that can vary in name between applications, so I would like the names to be stored in a custom config file. Then I would like to be able to load these values in the construct of the library.
So my two questions are:
1.) How do I name the config file, and how do I name variables within that file so they don't overwrite any other config vars?
2.) How do I get the values from within my library?
When i have questions like this i like to look at other projects that already do this. We utilized Tank_auth in almost all of our ci projects. This is a popular authentication library, which has its own custome config files
It just creates its own config file in application/config directory.
You could prefix your config items with your app name to ensure that they are unique
it then just loads it in the constructor:
$this->ci->load->config('tank_auth', TRUE);
If there is a config/libraryname.php file, it will be automatically loaded, just before library instanciation.
(so, beware of name conflicts with CI's config files)
Side note: this autoloading is disabled if you pass an array as the 2nd argument:
$this->load->library('thelibrary', array('param1' => 'value1'));
in your config /config/your_conf.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config = Array(
'your_conf1' => 'your_conf_val1',
'your_conf2' => 'your_conf_val2',
'your_conf3' => 'your_conf_val3'
);
in your controller:
$this->config->load('your_conf');
var_dump((array)$this->config); //show all the configs including those in the your_conf.php

change configure variable dynamically cakephp

Let's say I have this in my bootstrap.php file in a cakePHP application:
Configure::write('Config.language', 'eng');
How can I dynamically change the value of that configuration variable based on user action in a controller of my application? I tried to do the same thing that I did above here in my controller, but that didn't work
Any help?
Try Configure::write('Config.language', 'dut'); for e.g.
This answer from the question suggested by #Ryan Pendleton shows a somewhat correct way to use this directive.
It should be used in the AppController because it gets loaded first - as the parent of all other controllers in the application itself.
I used "somewhat correct" because it is best to validate the language codes ('eng', 'fre', 'dut') in the app/config/routes.php file - go here for more information.
Also do check out this: Internationalization-Localization explanation.

where to place Yii::app()->language = $_SESSION['lang '] to affect ALL Yii translations?

If placed in protected/views/layouts/main.php ,
Yii::app()->language = $_SESSION['lang'];
will not affect modules (because most of the contents is generated earlier).
For instance: Yii-user module.
What is the best file to place the Yii::app()->language = $lang; in the Yii flow of includes, to affect all the modules and their language settings ?
You can set up a "base" controller to extend your controllers (if you don't already have such a set-up) and put your language settings there as described here: http://www.yiiframework.com/wiki/26/setting-and-maintaining-the-language-in-application-i18n/
However, if you do have the value in a regular PHP session and want to set a global default, you should also be able to set it in your main config file with something like:
return array(
'language' => $_SESSION['lang'],
'name' => 'My Web Application',
... rest of your config settings...
I've modified protected/components/Controller.php, adding
function init()
{
parent::init();
Yii::app()->language = $_SESSION['lang']; //or some more code;
}
It works very well.
I've also tried making MyController.php in the same directory and extending the Controller.php, and it also works, but it slows down the whole application (most probably the loader tries hard until it loads the class, or because the operation was performed every single time the class was called ;) ).
When reading $_SESSION in the config/main.php we have to remember, that if Yii handles sessions, the second session_start() will result in a PHP E_Notice.
Yii can handle sessions :
link,
and it's supposed to have them turned on by default, but when adding $_SESSION code to the config.php I needed to add a 'manual' session_start() myself.

Categories