Create Codeigniter Helper -> undefined method - codeigniter

I created a helper "session_helper.php" in application/helpers/ folder
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('is_login'))
{
function is_login()
{
$CI =& get_instance();
$is_logged_in = $CI->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in != TRUE) {
redirect('login');
}
}
}
And "Configuracion" Controller:
class Configuracion extends CI_Controller {
function __construct()
{
parent::__construct();
$this->is_logged_in();
}
function is_logged_in()
{
$this->load->helper('session');
$this->is_login();
}
}
The problem is when I call the controller "http://localhost/proyect/configuracion" I get the following error:
Fatal error: Call to undefined method Configuracion::is_login() in C:...\application\controllers\configuracion.php on line 15
I read the manual and apparently everything is correct ... what is wrong?

"is_login" is a function, not a method. Just replace $this->is_login(); with is_login();.

Helpers are not methods, they are just simple function calls.
Take a look at helpers in the User Guide: http://codeigniter.com/user_guide/general/helpers.html
Loading a helper:
$this->load->helper('url');
using its helper functions:
<?php echo anchor('blog/comments', 'Click Here');?>
where anchor() is the function that is part of the loaded helper.
Also I would urge you to stay way from calling a helper 'session' make it something more descriptive, as it might become confusing later on. Just a suggestion, its entirely up to you.

Related

CodeIgniter accessing models from helper

I'm writing a CodeIgniter helper for e-mail functions, and need currently in each function I'm writing:
$CI = &get_instance();
$CI
->email
//etc
It's not a big problem, but I was wondering if there was a way of loading the CI instance once for all functions? Something like a constructor method?
If you are absolutely set on defining your functions inside of a helper rather than extending CodeIgniter's email library then you can do this:
email_helper.php
<?php
// Assuming $CI has not been set before this point
$CI = &get_instance();
function some_email_function()
{
$GLOBALS['CI']->email;
}
unset($CI);
You can actually do this.
Here's an example
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Example {
public function __construct()
{
$this->CI =& get_instance();
}
public function functionExample(){
$this->CI->load->module('fullpage');
if($this->CI->fullpage->my_function() ){
echo 'It works!';
}
}
}

Redirect loop while extend controller Codeigniter

I have a problem in login application in codeigniter , the problem is like this:
I create MY_Controller, like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
}
}
Then I extended it on my library that i created, this is my "Petugas_Controller" library:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas_Controller extends MY_Controller {
public function __construct(){
parent::__construct();
$this->load->model('petugas_m');
} // end contructor.
public function check(){
if ($this->uri->uri_string() !== 'index' && ! $this->petugas_m->is_logged_in() == TRUE){
redirect('admin/petugas/index');
}
}
}
I'm using that library to be used in my "Petugas" controller, like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas extends MY_Controller {
public function __construct(){
parent::__construct();
$this->load->library('petugas_controller');
$CI = &get_instance();
$CI->check();
}
public function index(){
echo "You have to login";
}
public function dashboard(){
echo "Welcome to dashboard";
}
}
In "Petugas_Controller" library, I loaded model that is called "Petugas_m", here is the model :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Petugas_m extends CI_Model {
public function __construct(){
parent::__construct();
}
public function is_logged_in(){
return FALSE;
}
}
That's all what i was trying, and my problem is I want to check if username is logged or not, by doing $this->check() in "Petugas_library" that loads "Petugas_m" model, i could see an dashboard echo if the "check" method in "Petugas_m" is set to TRUE but it's always redirect me a loop if it FALSE.
I was searching for tutorials and still have this crazy problem :( ,
Thank in advance, and sorry for my bad english.
As long as your petugas_controller library is located within the libraries folder, you can access the check method via
$this->petugas_controller->check();
Also try changing the if statement in your check to something more like this
if ($this->uri->uri_string() !== 'index' && $this->petugas_m->is_logged_in() !== TRUE){
lol, I've just realized my mistake here, I've just tried to make another controller, it's called login, then in my "petugas_controller" library I redirect the user if not login to this "login" controller, it's totally worked ...
Youy guys are inspiring me so much,Thanks :)

How to load module in my own created library codeigniter

class Mylib
{
function show_lib()
{
$obj=& get_instance();
$obj->load->module(‘login_check’);
$var=$obj->login_check->get_all_table_data();
print_r($var);
}
}
ERROR:-
Fatal error: Call to undefined method CI_Loader::module()
I Hope This Will Work --> check this code
class Mylib
{
function show_lib()
{
protected $ci;
$this->ci = &get_instance();
$this->ci->load->library(‘login_check’);
$var=ci->load->login_check->get_all_table_data();
return $var;
}
}
Not module, it's a library
use:
$this->load->library();
Working with modules in CI is called HMVC - Hierarchical Model View Controller.
There is a beautiful modular extension for CI to work with - Modular Extensions - HMVC
By using this extension you can create and work with modules in CI.
After setting up HMVC Extension you can call modules from your controller.
$controller = $this->load->module('module_name/controller_name');
echo $controller->method();
Few tutorials to get started:
http://net.tutsplus.com/tutorials/php/hvmc-an-introduction-and-application/
http://www.extradrm.com/blog/?p=744
If you are using codeigniter 3 you need to use Codeigniter Object to load modules,helpers..etc. Assign Codeigniter object to a variable and use it. $CI =& get_instance() Sample code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class CustomLibrary{
public $random_number;
public function __construct(){
$CI =& get_instance();
$CI->load->helper('string');
}
}
For more information about this visit http://www.webnsyntax.com/

Codeigniter HMVC Modular Error

I'm new to CI + MX and I tried the Modules::run(); style but I can't seem to let it work.
Here's my directory structure:
/application
-/modules
--/main
---/controllers
----main.php
---/models
---/views
--/connections
---/controllers
----connections.php
---/models
----/group_model.php
---/views
----connection_view.php
main.php controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends MX_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
}
function index(){
echo modules::run('connections/load_connections');
}
}
?>
connections.php controller:
<?php
class Connections extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('connections/group_model');
}
function load_connections(){
$user_id = 2;
$data['tabs'] = $this->group_model->get_groups($user_id);
$this->load->view('connection_view', $data);
}
}
?>
group_model.php model:
class Group_model extends CI_Model{
function __construct(){
parent::__construct();
}
/**
* Get all groups in db
**/
function get_groups($user_id){
$this->db->select('g.group_name');
$this->db->from('groups AS g');
$this->db->join('members AS m', 'g.group_id = m.group_id');
$this->db->where('m.user_id', $user_id);
return $this->db->get()->result_array();
}
}
My connection_view.php view contains a div and some php codes to display the $data['tabs'] passed as array in load_connections function.
The problem is, I'm getting an error that says:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Connections::$group_model
Filename: controllers/connections.php
Line Number: 14
and
Fatal error: Call to a member function get_groups() on a non-object in C:\xampp\htdocs\hmvc\application\modules\connections\controllers\connections.php on line 14
I have clearly followed all the instructions provided on MX wiki at https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home
and set up everything as needed.
My database.php under /application/config is already configured.
routes.php is also configured pointing the default controller to main.php
I wonder what I have missed or have done wrong that I can't get it to work.
Codeigniter version: 2.1.3
MX version: 5.4
almost missed it, extend connections with MX_Controller since you are calling modules::run(). Whenever you want a controller to be called with modules::run(), you extend it with MX_Controller instead of CI_Controller
Your second error is caused because of your first error.
Your first error is most likely caused because it cant open the group_model.
try this
$this->load->model('group_model');

CI not loading library

Here's my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('user_model');
}
public function index() {
redirect('user/login');
}
public function login() {
$this->load->library('form_validation', '', 'fv');
var_dump($this);
$this->fv->set_rules('nick_name', 'Nick Name', 'trim|required|max_length[12]|min_length[4]');
$this->fv->set_rules('password', 'Password', 'trim|required|md5');
}
}
?>
I was basically writing a login controller. In the login function, after this I load the view, etc. But up till here, I am getting the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: User::$fv
Filename: controllers/user.php
Line Number: 23
I have commented out everything else in the controller effectively to what I have pasted above. I don't get where the problem is. Thanks.
[EDIT]
On var dumping the $this object, I found an even more unusual thing. The object "fv" was not defined inside the $this object but rather under the $this->user_model object. So isset($this->fv) was returning false and isset($this->user_model->fv) was returning true. And yes I tried $this->load->library('form_validation') instead of naming it "fv", results remained the same.
Your controller name should named as user.php & it should located inside the controllers folder.
Try CI recommended coding styles, such as,
$this->load->library('form_validation');
$this->form_validation->set_rules(); etc..

Resources