My codeigniter site autoloads sessions. I have an XML API page that I created but I'm getting a session error because of that autoload. I would prefer not to load sessions on this controller but I don't want to have to load sessions manually on all of my other controllers. Can that be done?
Use a base controller to load the session class rather than autoload.php and have your controllers extend it. More information here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html
// application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
}
// you may add additional base controller classes here
You must extend this controller with the ones that you want to have access to the session class, so unfortunately you will have to make some edits to your existing controllers:
class UserController extends MY_Controller {
public function index()
{
// session class is loaded
}
}
Other controllers can continue to extend CI_Controller and the session class won't be loaded.
I use this method for all my CI projects and rarely use the autoloader.php, it allows much more flexibility.
Related
I have created the session in login function. Now I want to use the created session on all other functions too if a user login then the created session should be applied to all other functions.
Thanks
I think you need to understand session first.
How do Sessions work?
Sessions will typically run globally with each page load, so the Session class should either be initialized in your controller constructors, or it can be auto-loaded by the system. For the most part the session class will run unattended in the background, so simply initializing the class will cause it to read, create, and update sessions when necessary.
To initialize the Session class manually in your controller constructor, use the $this->load->library() method:
$this->load->library('session');
Once loaded, the Sessions library object will be available using:
$this->session
Session data is simply an array associated with a particular session ID (cookie).
Visit CI Documentation of Session For More detail
See it live here: Session
In your login section
$this->db->where('email',$email);
$this->db->where('password',$pass);
$query = $this->db->get('admin');
$data= $query->result_array();
if($data){
$this->session->set_userdata('sessionVariable', $data);
redirect('controller_name');
}
Open autoload.php from application/config/autoload.php
$autoload['libraries'] = array('session');
OR
load session libraries in __construct() of your controller
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
To get session data
$sessionData = $this->session->userdata('sessionVariable');
You can autoload the sessions in config.php
$autoload['libraries'] = array('database','Session','email');
OR
You can create a Base Controller in Core folder and extend all your other controller to that base controller.
Like this
<?php
class MY_Controller extends CI_Controller {
public $data = array();
function __construct() {
parent::__construct();
$this->data['errors'] = array();
$this->data['site_name'] = config_item('site_name');
$this->load->library('session');
}
}
Now All Your other Controller should be extended to your Base Controller instead of CI_Controller
In Your Controller
Controller 1:
class Login extends MY_Controller
{
function __construct() {
parent::__construct();
}
}
Controller 2:
class Dashboard extends MY_Controller
{
function __construct() {
parent::__construct();
}
}
So You will get just need to load your library is your base controller and get all the goodness of base controller in the child controllers. This will give you better hierarchy, code management and security
I have a controller "MY_Controller.php" in "application/core" that extends "CI_Controller":
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
$this->load->helper('security');
$this->load->helper('language');
// Load language file
$this->lang->load('en_admin', 'english');
}
}
I created another controller called "Auth.php" in "application/controllers" that extends "MY_Controller":
class Auth extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('ion_auth');
if ($this->ion_auth->logged_in() === FALSE) {
redirect('user/login');
}
}
}
I created a third controller "Dashboard.php" that should extend "Auth", but throws an error:
class Dashboard extends Auth
{
public function index()
{
echo 'Hello from the dashboard';
}
}
Fatal error: Class 'Auth' not found in /home/user/www/forum/application/controllers/Dashboard.php on line 5
Would appreciate your advice to solve this problem.
The link in my comment has multiple ways to solve your problem. I prefer to use the #3 method - "Using an autoload function with hooks". Here's what works for me.
application/config/config.php
$config['enable_hooks'] = TRUE;
application/config/hooks.php
$hook['pre_system'][] = array(
'class' => '',
'function' => 'register_autoloader',
'filename' => 'Auto_load.php',
'filepath' => 'hooks'
);
application/hooks/Auto_load.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
function register_autoloader()
{
spl_autoload_register('site_autoloader');
}
/*
* Custom autoloader.
* This piece of code will allow controllers and other classes
* that do not start with "CI_" to be loaded when
* extending controllers, models, and libraries.
*/
function site_autoloader($class)
{
if(strpos($class, 'CI_') !== 0)
{
if(file_exists($file = APPPATH.'core/'.$class.'.php'))
{
require_once $file;
}
elseif(file_exists($file = APPPATH.'libraries/'.$class.'.php'))
{
require_once $file;
}
elseif(file_exists($file = APPPATH.'models/'.$class.'.php'))
{
require_once $file;
}
}
}
Your first extend of CI_Controller - class MY_Controller extends CI_Controller - should probably be in application/core/MY_Controller.php. Any classes that extend MY_Controller should be in application/libraries/.
The linked page labels the "hook" method as "the slow and right way" but the profiling I did comparing it to "2. Using an autoload function (the fast and dirty way)" showed a time difference of slightly over a microsecond. That's not worth worrying about.
I used the "fast and dirty way" for many years but kept forgetting about it when upgrading CI and I'd overwrite the config file resulting in a lost autoloader routine. Don't have that problem using the hook method other than remembering to set $config['enable_hooks'] to TRUE.
This is the regular behavior. Controllers do not extend each other. They may only extend MY_Controller (or CI_Controller). This is true for all MVC frameworks.
The way to use ion_auth, is not to extend Auth controller. You have already loaded ion_auth library, so you may directly use its methods in all controllers, the same way you use them in Auth controller.
Documentation for ion_auth here.
Well I've never used MY_Controller. I specify my class names.
I create all my classes under application/libraries and my structure is
Admin extends Common_Controller
Common_Controller handles the auth stuff for admin and members login/auth etc
Admin is a Controller up in application/modules/admin/controllers/Admin.php
Common_Controller extends Base_Controller
Base_Controller handles all the stuff common to all controllers like the templating etc
Base_Controller extends MX_Controller
MX_Controller is the wiredesignz HMVC Controller
MX_Controller extends CI_Controller
and we are done.
This is of course a HMVC setup but the principle is the same. Just don't use MY_Controller, give it a real name and use it normally.
And of course HMVC uses MY_Loader and other classes MY_xxx it uses to hook itself into CI and they live under application/core and it all plays nicely.
So as far as I can tell, there isn't a limit on how many classes deep you can go when you are extending them.
I need to set additional configs defined in the database. My idea is to extend CI config in pre_controller hook.
The hook is not a problem, but how to extend CI config in the right way?
Can someone explain it to me with some example if possible?
Besides that, if I hook it in pre_controller, will it check configs and values from the database on every request with caching, because I need to check the table on every request?
dont need to use hooks necessarily
in application/core/ create your base application controller ex: AppController
class AppController extends CI_Controller
{
public function __construct()
{
parent::__construct();
$config = $this->db->get('config')->result();
foreach ($config as $config_item)
{
$this->config->set_item($config_item->name, $config_item->value);
}
}
}
and all your application controllers extends from AppController
class Main extends AppController
{
// other functions
}
I have a table in my database called settings, with the following structure {id, name, value}.
I want a variable to be available on all pages like $setting[name] => value....
I tried putting it on the helper and autoload it, but the helper can't access the db. Any other ideas? Thanks
http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
Create a class called MY_Controller which extends CI_Controller and save it in the core folder under application. Then have all your other classes extend MY_Controller instead of CI_Controller.
Then you can set your values in the constructor of MY_Controller
<?PHP
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$settings = $this->db->get('settings');
}
This would make $settings available to all your controllers just like you wanted. Just remember the controllers you need those settings in have to extend MY_Controller!
class A_new_controller extends MY_Controller {
All I'm trying to do is something fairly simple :
Create a class (let's say brandNewClass - NOT MY_Controller) which extends CI_Controller
Create other controllers which extend brandNewClass
E.g.
class brandNewClass extends CI_Controller {
public function index()
{
}
public function info()
{
}
}
used like (in a file under /controllers) :
<?php
class newController extends brandNewClass
{
}
?>
The thing is, although it works when I'm copying the file under /application/core and naming it as MY_Controller, when I change the name to something more... self-explanatory, it doesn't.
Fatal error: Class 'brandNewClass' not found in .... on line ..
I've even tried using the __autoload function mentioned here, but without any luck.
Any ideas?
Have a look at this excellent tutorial - I hope it helps
http://codeigniter.tv/a-10/Extending-the-core-MY_Controller-and-beyond
The autoloader doesn't automaticly include other controllers. you will have to include it manually like this:
if (!defined('BASEPATH'))exit('No direct script access allowed');
include_once(APPPATH . 'controllers/brandnewclass.php');
If you want to create a custom base controller and make other controllers extend there, you can do it in following ways:
Create MY_Controller extending CI_Controller in application/core/ folder and make other controllers extend MY_Controller as MY_Controller will be autoloaded from core (This I guess you already know but want other alternatives.
Create MY_Controller in application/core/. Then create other level of Controllers that can primarily be Admin_Controller and Frontend_Controller. Now, one of these controllers will make base for your actual used controllers.
e.g. in application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
}
}
Then Admin and Frontend controllers will be created in application/libraries/ and will extend MY_Controller like
class Admin_Controller extends MY_Controller {
public function __construct(){
parent::__construct();
}
}
Now, Any controller can extend one of these 2 controllers but for doing that you will have to autoload them. For autoloading in this case there can be a confusion because setting autoload['libraries'] in config/autoload.php will not work . That autoload works inside a controller but here we need to autoload before that i.e. in the class declaration. Will need to set this code in config/config.php
function __autoload($class)
{
$path = array('libraries');
if(strpos($class, 'CI_') !== 0) {
foreach($path as $dir) {
$file = APPPATH.$dir.'/'.strtolower($class).'.php';
if (file_exists($file) && is_file($file))
#include_once($file);
}
}
}
Now you can create your own controller
class newController extends Admin_Controller
{
}
This is the most suggested method making your structure quite clean and effective. May take some effort in understanding for the first time but is definitely worth it.
Third method is just a tweak of the second one, just based on the condition you mentioned of not using MY_Controller
You can make Admin_Controller or Frontend_Controller extend CI_Controller directly and not extend MY_Controller
That may just lead to some duplicity of code in both these controllers if that may be the case
http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-base-Classes-Keeping-it-DRY
I suspect you're trying something similar?
There's an autoload function that you can add to the config file so that you needen't require_once() the class all the time.
You should declare the class as abstract, since it shouldn't be instantiated directly.
You'll need to modify the CodeIgniter autoloader configuration file and add your class to it, or change the actual autoloader.
You really should consider not using CodeIgniter :)