Using CodeIgniter, I have a few custom controllers:
MY_Controller - this extends the CI_Controller
Front_Controller - extednds MY_Controller, used for any "user" facing pages
Admin_Controller - extends MY_Controller, used for "admin" pages
Now, when I am on the admin "index" page, the controller of which uses Admin_Controller, I am getting logged errors coming from the Front_Controller.
I put a var_dump() into the Front_Controller and I cannot see it on the admin page, so I'm confident it is not being called by my code, but somehow, it does seem to be getting called!
Is there any explanation for this?
Some code:
Admin_Controller
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Admin_Controller extends MY_Controller
{
public function __construct() {
parent::__construct();
if(!$this->ion_auth->logged_in()) {
redirect('login', 'location');
} elseif($this->ion_auth->logged_in() AND !$this->ion_auth->in_group(array('admin', 'mod'))) {
redirect('account/dashboard', 'location');
} else {
$this->user = $this->ion_auth->user()->row();
}
$this->data = array(
'head' => 'inc/head',
'foot' => 'inc/foot',
'nav' => 'admin/inc/nav',
'flash' => 'inc/flash_messages',
'user' => $this->user
);
}
}
Admin index:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Index extends Admin_Controller {
public function __construct() {
parent::__construct();
}
public function index()
{
$this->load->vars($this->data);
$this->load->view('admin/index');
}
}
MY_Controller
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->data = array(
'head' => 'inc/head',
'foot' => 'inc/foot',
'nav' => 'inc/nav',
'flash' => 'inc/flash_messages',
);
$this->load->model('new/resellers/reseller_model');
$this->load->model('new/white_labels/white_label_model');
// find matching domain and show logo
$domain = $_SERVER['SERVER_NAME'];
$reseller = $this->reseller_model->get_by('domain', $domain);
if($reseller):
$resellerWhiteLabel = $this->white_label_model->get($reseller->white_label_id);
if($resellerWhiteLabel):
$this->data['portal_reseller'] = $reseller;
$this->data['portal_reseller_white_label'] = $resellerWhiteLabel;
endif;
endif;
$this->load->library('lib_log');
$this->load->library('ion_auth');
$this->load->helper('language');
$this->lang->load('auth');
$this->lang->load('site');
}
}
As far as I can remember CodeIgniter is a singleton, so it loads everything on each request.
This means that the Front_Controller will get loaded even if not called. So if there are errors in it they will out.
Take some time, fix the errors and all should be good.
Related
My Project Structure is
application
controllers/site
core/
views/site
I want when i entered http://mywebsite admin has to be loaded and when i entered http://mywebsite/site frontend has to be loaded
I went through some tutorials and i have done changes below
In config/routes.php
$route['default_controller'] = 'admin';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['site'] = 'site/home';
In core/My_Controller.php
defined('BASEPATH') or exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
}
class Admin_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
}
}
class Site_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
}
}
In controller/site/Home.php
defined('BASEPATH') or exit('No direct script access allowed');
class Home extends Site_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view("site/index.php");
}
}
But I am getting 404 when I opened http://mywebsite/site
Please help me
$route['site'] = 'site/home';
What that line does is that it defines a route that looks for a method called home inside the controller called Site viz. Site_Controller.
So you are getting the 404 error because your Site_Controller does not have the home method.
Try changing the Site_Controller like so...
class Site_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
}
# added this method
public function home()
{
$this->load->view("site/index.php");
}
}
I have this on my MY_Controller that was located on my core folder.
<?php
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function is_logged_in($data){
$session = $this->session->userdata();
if($session['isloggedin']['username'] == ''){
return isset($session);
}else{
return FALSE;}
}
}
?>
I'm pretty sure i copy pasted the above code from some tutorial and i haven't gotten to editing it based on my needs.
Any case i have questions.
So i have a pages controller that will be responsible for giving access to some views depending on the account_type of the logged in user.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class pages extends MY_Controller {
}?>
this is my session whenever a user logs in.
$new_session = array( 'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'type' => $this->input->post('type'),
'logged_in' => TRUE);
$this->session->set_userdata($new_session);
How do i call the MY_controller function is_logged_in() from the pages controller or is the 'extends MY_Controller' automatically also calls the function is_logged_in() or do i have to basically just put it in a __construct so it automatically calls the function?
Also, how do i go about checking if a user is logged in and seeing their details?
Do i pass session_data from my controller to MY_Controller? if so, how?
Or should i just put a $this->session->userdata(); line inside the is_logged_in() function?
P.S. i have tried using Authentication Libraries but they include far too much to what i need, i just need a basic authentication. Any suggestions? that is still maintained right now.
you can directly call is_logged_in() function from your pages controller. just like this:
Pages.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends MY_Controller {
function __construct() {
parent::__construct(); // this will trigger the __construct() of MY_Controller
}
}
MY_Controller.php
<?php
class MY_Controller extends CI_Controller{
public function __construct() {
parent::__construct();
if( $this->is_logged_in() ) {
// do something if user is allowed to access.
}
else {
// do something if user is not allowed to access
}
}
}
I'm using Codeigniter 3 and I need some data available to all methods. I will query the data from the database and then I need to display it on every page.
I have created a MY_Controller extending CI_Controller and saved it in /application/core/
But I am getting this error:
Fatal error: Call to undefined method Area_model::get_user_locations()
MY_Controller:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public $location_data;
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('area_model');
$org_id = $this->session->userdata('org_id');
$this->location_data = $this->area_model->get_user_locations($org_id);
}
}
Can i access models and the database from within MY_controller?
Area_model.php
// get all areas for an organisation
public function get_user_locations($org_id) {
$areas = $this->db->get_where('areas', array('org_id' => $org_id));
$area_array = $areas->result_array();
return $area_array;
}
MY_Controller
not used to direct any function to __construct()
__construct to only load to any model
write other function to MY_Controller look like this
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public $location_data;
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('area_model');
$org_id = $this->session->userdata('org_id');
}
}
function functionname()
{
$this->location_data = $this->area_model->get_user_locations($org_id);
}
Area_model.php
public function get_user_locations($org_id)
{
$areas = $this->db->get_where('areas', array('org_id' => $org_id));
$area_array = $areas->result_array();
return $area_array;
}
I've recently switched from Windows 7 to Ubuntu.. Now I'm having this strange problem with codeigniter.. My code works prefect on Widnows 7 Xampp server, but when I try to access it on ubuntu having apache2, I cannot load any model, libraries etc.
Here is my code for model
<?php
class Usermodel extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->user_per_page = 8;
}
function getUser($id)
{
$query = $this->db->query("SELECT * FROM user WHERE id = $id");
if(intval($query->num_rows()) > 0)
{
$data = $query->result();
return $data[0];
}
else
return null;
}
}
Here is the code of my controller where i am loading model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller
{
public function index()
{
$this->load->model("Usermodel");
$fb_config = array(
'appId' => 'xxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxx'
);
$this->load->library('facebook', $fb_config);
$this->load->helper('security');
}
}
I've already tried the following
1. Changed usermodel.php to Usermodel.php
2. Changed $this-load->model("Usermodel") to $this-load->model("usermodel")
but none seem to work
I get this fatal error when i call $this->Usermodel->getUser(1) in index() function of my controller
PHP Fatal error: Call to a member function getUser() on a non-object in /var/www/voicebuds/application/controllers/register.php on line 19, referer: mysite
UPDATE
If i put the Usermodel in config/autoload.php, it works fine.. So I must say there is some problem with loader function.
check your model file name for model
<?php
// file name usermodel.php
class Usermodel extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->user_per_page = 8;
}
function getUser($id)
{
$query = $this->db->query("SELECT * FROM user WHERE id = $id");
if(intval($query->num_rows()) > 0)
{
$data = $query->result();
return $data[0];
}
else
return null;
}
}
in your controller load appropriate model using model file name
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller
{
public function index()
{
$this->load->model("usermodel");//your file name of model
$fb_config = array(
'appId' => '454005957970086',
'secret' => '7b8e9664cb0439f88da00007996278c7'
);
$this->load->library('facebook', $fb_config);
$this->load->helper('security');
}
}
Model classes are stored in your
application/models/
folder. They can be nested within sub-folders if you want this type of organization.
The basic prototype for a model class is this:
class Model_name extends CI_Model {
function __construct()
{
parent::__construct();
}
}
Where Model_name is the name of your class. Class names must have the first letter capitalized with the rest of the name lowercase. Make sure your class extends the base Model class.
The file name will be a lower case version of your class name. For example, if your class is this:
class User_model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
Your file will be this:
application/models/user_model.php
So please check, is your model in any sub-folder or not.
It is said to be a best practice to load models, libraries in __construct (constructor)
class User extends CI_Controller
{
public function __construct()
{
parent:: __construct();
$this->load->model('usermodel');
}
}
I found in your controller there is not construct. try to add constructor and load usermodel there
I've ended up using __construct() in controller class.. I now first initialize model in constructor and then call in in function and its working now.!! But still I'm puzzled why load-model() didn't work for me in index() function
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller
{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->model("Usermodel");
$fb_config = array(
'appId' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXX'
);
$this->load->library('facebook', $fb_config);
$this->load->helper('security');
}
public function index()
{
$data = $this->Usermodel->getUser(1);
print_r($data);die;
}
}
I have been working on a session validation for my login to make sure that a user is logged in to view pages. I keep getting this error:
Fatal error: Class 'MY_Staffcontroller' not found in /usr/local/var/www/CodeTest
/ci/application/controllers/staff_c.php on line 3
My staff_c page looks like so :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Staff_c extends MY_Staffcontroller {
function homepage()
{
$data['main_content'] = 'homepage_view';
$this->load->view('includes/template', $data);
}
}
I have been reading same questions all over the place and they say the same thing pretty much...
Is your controller located in application/core?
Well yes it is. I can't seem to get passed this hump!
This is the code within My_Staffcontroller.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_staffcontroller extends CI_Controller {
function __construct()
{
parent::__construct();
$loggedin = $this->session->userdata('loggedin');
if(!isset($loggedin) || $loggedin != TRUE);
{
die($this->load->view('denied'));
}
}
}
I know this is user error as this is only my second day with CodeIgniter but I can't seem to find proper workaround for this?
I have tried this tutorial and still nothing and also this
Even following this video has me stuck on the session part.
And I just can not get this to work.
Remember Linux is case-sensative whereas Windows is case-insensative.
place you're MY_Staffcontroller inside application/core/MY_Controller.php file
Your MY_Controller.php file should look like this (minus all you're other functions, this is a minimal example)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
}
class MY_Staffcontroller extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function sayHello()
{
echo "Hello, I am a function within MY_Staffcontroller.php";
}
}
Example
This will be located in /application/controllers directory
Basically any protected and public functions located in either MY_Controller OR MY_Staffcontroller will be accessible from derived controllers that extend the extended controller. In this case it would be MY_Staffcontroller
class Public_Staff_Controller extends MY_Staffcontroller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->sayHello();
}
}
/* end of file /application/core/MY_Controller.php */