How to autoload a custom library in codeigniter - php

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

Related

HMVC Integration in Codeigniter view paths

I am trying to intregrate HMVC to codeigniter. I have installed the MX files to thrid_party and uploaded the MY_Loader , MY_Loader and MY_Model to the application/core folder. it is working fine
I have two issues
1) How to add the module routes that overide the application routes
I am accessing module through the link localhost/domain/admin/portfolio
I have tried adding routes.php to the modules config application/modules/portfolio/config/routes.php with below details
$route['admin/portfolio'] = 'portfolio/admin/portfolio';
$route['admin/portfolio/add'] = 'portfolio/admin/portfolio/edit';
$route['admin/portfolio/edit/(:num)'] = 'portfolio/admin/portfolio/edit/$1';
On my root application config already added a routes
$route['admin'] = 'admin/login';
Because of this route 'admin/login' in the application/config/routes.php it is showing page not found. To fix this I have currently added the module/portfolio/config/routes`` above the 'admin/login'. Is there any other method instead of adding it to theapplication/config/routes`.
2) How to access the module view files
I have controller accessing the view files from application/controlles/admin/
$this->load->view('admin/view_header',$data);
$this->load->view('admin/view_portfolio',$data);
$this->load->view('admin/view_footer');
You have placed your Portfolio Controller under
application/modules/portfolio/controllers/admin
which is fine.
Your route (which will hit the index by default) should be
$route['admin/portfolio'] = 'portfolio/admin/portfolio';
Aside: other naming considerations
What I tend to do is to create a controller with admin in the name...
So I would have PortfolioAdmin.php or something along those lines, so I know by the file name, it's admin "Stuff", when I am playing with it in my Editor/IDE.
UPDATE:
In regards to your
Nor this works Modules::run('admin/portfolio', $data);
So you would then use the full controller name... Do not use routes, they are for URLs. Any module you want to call from another module you always use the full name.
Modules::run('portfolio/admin/portfolio', $data);

Undefined variable on userdata

I'm using codeIgniter to developing a website.
This website has a view sidebar and i call on view header with
<?php $this->load->view('sidebar');,
then in another view i call header by using <?php $this->load->view('header');
On sidebar i'm loading the session variables by
$papel = $this->session->userdata('papel');
$logado = $this->session->userdata('logado');
On sidebar it fits perfectly but in other pages like it's doesn't working anymore.
I'm already tried recalling the session variables and got the same results.
ps.: It's alerady autoloaded
Load session library in autoload:
Open autoload.php file located in application/config/autoload.php
Update $autoload['libraries'] variable
$autoload['libraries'] = array('database', 'email', 'session');
Load session in autoload.php found in config folder.
$autoload['libraries'] = array('database', 'session');
Plus to make a session you have to set it:
$this->session->set_userdata('session_name', 'value of the session');
Then calling it:
$this->session->userdata('session_name');

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

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

Categories