Codeigniter constructor - check if user is registered

I am trying to create a constructor for my controller, which refers to a function contained in the helper, which is automatically loaded.

The function checks if the user is logged in if he redirects them to the login page.

It looks like I did not set up the construct correctly, as I am getting the following error:

Fatal error: Call to undefined method Profile::is_logged_in() 

This is the controller:

 <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Profile extends CI_Controller { public function __construct() { parent::__construct(); //function inside autoloaded helper, check if user is logged in, if not redirects to login page $this->is_logged_in(); } public function index() { echo 'hello'; } } 

I only want the function inside the controller to be available if the user is logged in.

This is an auxiliary autoloader

 $autoload['helper'] = array('url','array','html','breadcrumb','form','function','accesscontrol'); 

(accesscontrol_helper.php):

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); function is_logged_in() { $is_logged_in = $this->session->userdata('is_logged_in'); if(!isset($is_logged_in) || $is_logged_in != true) { echo 'You don\'t have permission to access this page. <a href="../login">Login</a>'; die(); //$this->load->view('login_form'); } } 

Why am I unable to run this function? Does the helper code have a better method?

+4
source share
4 answers

As already mentioned, helpers are just a bunch of functions. Extension to them:

  • since they are loaded several times, you need to specify not to declare the function, if it is already present, all that you will cause an error.
  • In addition, you cannot call the CI class inside them without the first instance of the main CI object . This is a more suitable way to use your helper function:

     if(!function_exists('is_logged_in')) { function is_logged_in() { $CI =& get_instance(); $is_logged_in = $CI->session->userdata('is_logged_in'); if(!isset($is_logged_in) || $is_logged_in != true) { echo 'You don\'t have permission to access this page. <a href="../login">Login</a>'; die(); } } } 

I would also like to return it instead of echo and move die() to the controller, but this is another story.

+9
source

Helpers are functions that are just included, so you don’t need to access it with $this . Just call it as a normal function:

 is_logged_in(); 
+6
source

You do not call a helper function with $this . Just do is_logged_in();

 public function __construct() { parent::__construct(); //function inside autoloaded helper, check if user is logged in, if not redirects to login page is_logged_in(); } 
+3
source

accesscontrol_helper.php

  <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Accesscontrol_helper{ function is_logged_in() { //code } } 

in profile :

 class Profile extends CI_Controller { public function __construct() { parent::__construct(); Accesscontrol_helper::is_logged_in(); } } 
+1
source

Source: https://habr.com/ru/post/1381917/


All Articles