How to get a migration config item in CodeIgniter? - php

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');

Related

How to autoload a custom library in codeigniter

I have created a custom library in my codeigniter application and I need that to be autoloaded.
Is it possible to do that for custom Libraries?
I tried adding the same in autoload.php, but getting error.
Error : Unable to load the requested class: Database,Sessions
Sessions.php is my custom library placed inside applications/library package.
Code : $autoload['libraries'] = array('database,Sessions');
You made a mistake in array elements of auto load.
It should be $autoload['libraries'] = array('database','Sessions');
It should be like below
$autoload['libraries'] = array('database', 'sessions');
Put your file in, application/libraries folder and then add your file name in auto-loader libraries array element, application/config
$autoload['libraries'] = array('yourfilname');

How to access language file inside config?

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.

Loading configuration items from another config file in CI

I've been trying to load a configuration item into another configuration file for my CodeIgniter app.
The reason for doing this is that I don't want to go through all of the files to change same parameters when I, for example, change the server or want to change the site title.
I've tried to get the items I need from the main config.php file by using $this->config['site_title'], by loading the config file using $this->config->load('config') and by loading individual config item using $this->config->item('site_title') but all of these methods return errors that the configuration items could not be loaded.
Am I missing something?
you should load the config files from the CI instance within a Controller
$ci = & get_instance();
$item_name = $ci->config->item('item_name');
simply create your config file in your ci config folder
myconfig.php
in it
$config['my_site_title'] = 'TechNew.In - www.technew.in';
and load your config in the controller
$this->load->config('myconfig');
then get your value
$my_site_title = $this->config->item('my_site_title');

Get to parent folder in codeigniter

I'm putting an DOMpdf creator in my Codeigniter application, but now i need to get an require_once to the parent folder. Somehow Codeigniter doesn't allow me to do this the "normal" way.
This is my require_once
require_once("../dompdf_config.inc.php");
How can I make this work in Codeigniter?
Try something like:
//APPPATH gives you path till application/ folder
require_once(APPPATH.'your_DOMpdf_file_path');
Hope it helps
To load a config file in CodeIgniter, you can call the $this->config->load() method.
To load one of your custom config files you will use the following
function within the controller that needs it:
$this->config->load('filename');
Where filename is the name of your config file, without the .php file
extension.
If you need to load multiple config files normally they will be merged
into one master config array. Name collisions can occur, however, if
you have identically named array indexes in different config files. To
avoid collisions you can set the second parameter to TRUE and each
config file will be stored in an array index corresponding to the name
of the config file. Example:
// Stored in an array with this prototype: $this->config['blog_settings'] = $config
$this->config->load('blog_settings', TRUE);
Please see the section entitled Fetching Config Items below to learn
how to retrieve config items set this way.
The third parameter allows you to suppress errors in the event that a
config file does not exist:
$this->config->load('blog_settings', FALSE, TRUE);
http://codeigniter.com/user_guide/libraries/config.html

Codeigniter routing question

I have a CI application with organized folders.
I have several folders on views, controllers and models.
ie.:
/controllers/frontend/, controllers/backend
/views/frontend, views/backend
... etc ...
So if i want to access the function 'login' on the frontend controller i have to go to: http://localhost/frontend/login/index
What i want is to get rid of the need of typing 'frontend', so if i type http://localhost/login/index, it would be the same as http://localhost/frontend/login/index.
Of course i dont want to add them manually to the routes file, i want it to be recognized automatically.
Thanks.
Try changing line 17 in your config.php file in application\config.
$config['base_url'] = 'http://localhost/';
Change above to below:
$config['base_url'] = 'http://localhost/frontend/';
I know you don't want to change your routing for each controller, but if this is the only one you need to do a specific routing for, you can change the default controller in the Router file inside your application/config folder to:
$route['default_controller'] = 'frontend';
$route['(:any)'] = "frontend/some_secondary_variable";
The second line is only if you need to pass variables to the controller, otherwise, omit it.

Categories