I have a controller in CodeIgniter that does not respond to my requests. When I add a simple function to it function test1 {echo 'test';}, it returns blank response. When I add this function to another controller, it returns 'test' as expected. The syntax for the invalid controller does not differ from a valid one:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class authorized extends CI_Controller
{
function index(){echo "test";}
function __construct()
{
parent::__construct();
//Load Neccessary Models
$this->load->model('users');
$this->load->model('manufacturers');
$this->load->model('suppliers');
$this->load->model('administrators');
$this->load->model('banks');
//End Load Neccessary Models
define("AUTHORIZENET_API_LOGIN_ID", "");
define("AUTHORIZENET_TRANSACTION_KEY", "");
define("AUTHORIZENET_MD5_SETTING", "");
$this->load->library('authorizenet');
}
function test1(){echo "test";}
}
What can be the reason for its not responding?
Alright, got it to work by ensuring there is the following order of functions within the controller: function __construct(), then function index(), then my required function test1().
Related
I am trying to call a model in controller in codeigniter but that give me error that to locate the model you have specified. Everything looks ok in my code but I did not get why it is not finding the model here is my model
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register_model extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->database("chandqki_mmusic");
}
public function insert_users_to_db($data)
{
return $this->db->insert('music_user_register', $data);
}
}
?>
and in my controller I am calling this model
class Home extends CI_Controller{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->helper('url');
$this->load->view('Home_view');
$this->load->model('register_model');
}
}
From my side its code is looking ok but May be I am missing somewhere can anybody help me regarding this. I have go through all the answers of stackoverflow and check all the whatever they say everything is I am following like that but still I am unable to solve it
Thanks
Your model file name must be ucfirst
Register_model.php
Replace
$this->load->model('register_model');
with
$this->load->model('Register_model');
R in register in caps.
I'm new to codeigniter and I have two controllers: utility.phpand welcome.php.
In utility.php, I have functions:
function getdata() {
//code here
}
function logdata() {
//code here
}
Inside welcome.php, I have this function:
function showpage() {
//some code here
//call functions here
}
What I want to do is inside my welcome.php, I want to call the functions from utility.php. How do I do this? Thanks.
Refrence from here
To extend controller please either follow this tutorial or see some code below.
differences between private/public/protected
make a file in folder /application/core/ named MY_Controller.php
Within that file have some code like
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
protected $data = Array(); //protected variables goes here its declaration
function __construct() {
parent::__construct();
$this->output->enable_profiler(FALSE); // I keep this here so I dont have to manualy edit each controller to see profiler or not
$this->load->model('some_model'); //this can be also done in autoload...
//load helpers and everything here like form_helper etc
}
protected function protectedOne() {
}
public function publicOne() {
}
private function _privateOne() {
}
protected function render($view_file) {
$this->load->view('header_view');
if ($this->_is_admin()) $this->load->view('admin_menu_view');
$this->load->view($view_file . '_view', $this->data); //note all my view files are named <name>_view.php
$this->load->view('footer_view');
}
private function _isAdmin() {
return TRUE;
}
}
and now in any of yours existing controllers just edit 1st or 2nd line where
class <controller_name> extends MY_Controller {
and you are done
also note that all your variables that are meant to be used in view are in this variable (array) $this->data
example of some controller that is extended by MY_Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class About extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['today'] = date('Y-m-d'); //in view it will be $today;
$this->render('page/about_us'); //calling common function declared in MY_Controller
}
}
You don't is the short answer.
The point of MVC is to have your code well organized.
What you can do though is create a library in the libraries folder where you put methods you need in more than one controller.
For example you can make a mylog library, where you can put all your log related stuff. In any controller you will then call:
$this->load->library('mylog');
$this->mylog->logdata();
Besides functions that deal with data models should reside in models. You can call any model from any controller in CI
That is out of concept, if you want to call code in different controllers do following:
create helper and call function whenever (even in view) you want.
extend CI_Controller so you can use one or more functions in any controller that is extended.
Lets start with helper:
create file in folder application/helpers/summation_helper.php
use following sample of code
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function sum($a, $b) {
//$CI =& get_instance();
// if you want to use $this variable you need to get instance of it so now instead of using $this->load... you may use $CI->load
$return = $a + $b;
return number_format($return, 3);
}
If you are going to use your helper in many controllers/views please load it in autoload, otherwise just load it manualy $this->load->helper('summation');
Extending Controller_CI: this is way better approach if you are using database. Please follow this tutorial, which explains it all.
*I have crafted an answer just before site went down posting this from cellphone.
I have one controller named home.php in which a function named podetails is there. I want to call this function in another controller user.php.
Is it possible to do so? I have read about HMVC in CI, but I want to know is it possible to do without using hmvc?
To extend controller please either follow this tutorial or see some code below.
differences between private/public/protected
make a file in folder /application/core/ named MY_Controller.php
Within that file have some code like
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
protected $data = Array(); //protected variables goes here its declaration
function __construct() {
parent::__construct();
$this->output->enable_profiler(FALSE); // I keep this here so I dont have to manualy edit each controller to see profiler or not
$this->load->model('some_model'); //this can be also done in autoload...
//load helpers and everything here like form_helper etc
}
protected function protectedOne() {
}
public function publicOne() {
}
private function _privateOne() {
}
protected function render($view_file) {
$this->load->view('header_view');
if ($this->_is_admin()) $this->load->view('admin_menu_view');
$this->load->view($view_file . '_view', $this->data); //note all my view files are named <name>_view.php
$this->load->view('footer_view');
}
private function _isAdmin() {
return TRUE;
}
}
and now in any of yours existing controllers just edit 1st or 2nd line where
class <controller_name> extends MY_Controller {
and you are done
also note that all your variables that are meant to be used in view are in this variable (array) $this->data
example of some controller that is extended by MY_Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class About extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['today'] = date('Y-m-d'); //in view it will be $today;
$this->render('page/about_us'); //calling common function declared in MY_Controller
}
}
write the podetails() as a function within a helper file.
then load that helper in both of the controllers.
in the controller you just call podetails()
Suppose:
--controller 1--
function podetails()
{
podetails(); // will call function in helper ;
}
--controller 2--
function podetails()
{
podetails(); // will call function in helper ;
}
Hello I'm using inherited controllers. These are my controllers:
-baseAdminController:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class _BaseAdminController extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
$calledFunction= $this->router->fetch_method();
if ($calledFunction!= 'loginView' && $calledFunction!= 'doLogin') {
$this->checkSession();
}
}
public function checkSession() {
if ($this->session->userdata('loggedIn') == false) {
$this->load->view('admin/loginView');
}
}
}
And my derived Admin Controllers:
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class AdminController extends _BaseAdminController {
public function indexView() {
$this->load->view('admin/indexView');
}
}
When i'm tring to login, CodeIgniter shows me both admin/loginView and admin/indexView. Because i'm checking session status at constructing my derived controller. How can i prevent to loading second view?
Thank in advance..
To answer your question, you could have done the following into your checkSession method:
public function checkSession() {
if ($this->session->userdata('loggedIn') == false) {
echo $this->load->view('admin/loginView', array(), TRUE);
exit;
}
}
Explanation: If you pass the third argument as TRUE, it will return the content of that file. Read ellislab.com for more info.
I hope this is related to your question:
I recently wanted to have a bunch of controllers extend a parent controller class (MY_Acl_Controller) that checked that the current logged-in user deserved access to each method (using a homegrown ACL library). The check was to be initiated in MY_Acl_Controller's constructor, so it would run on every request.
I wanted to set the Output class's output to the result of loading a view, then display the view and exit, but because this process was NOT executed in a routed controller method, I couldn't just call $this->load->view('errors/access_denied') and then return from the constructor function... CI would then carry on executing controller code.
So I created a MY_Output class, extending CI_Output, and added to it a public function display_with_exit():
public function display_with_exit()
{
$this->_display($this->final_output);
exit;
}
Then, in MY_Acl_Controller's constructor:
...
if(!$user_deserves_access)
{
$this->load->view('errors/access_denied');
$this->output->display_with_exit();
}
Maybe that might be useful to someone?
The advantage of this approach is that the Output class's _display() function sends all HTTP headers you'd like it to, as per any normal response.
Avoid exit; / function exit; or die; will terminate execution, its better practice in only debugging your code.
Try like below.
public function index(){
if($xx) return TRUE;
}
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 */