Welcome Guest, Not a member yet? Register   Sign In
Can't use helper function in controller
#1

[eluser]zsela[/eluser]
Hello all!

I would like to ask your help regarding my problem: created a helper (common_functions_helper) with a function in it:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function entitle ($action) {
  
$entitled = 0;
$rights = $this->session->userdata('rights');
for ($i = 0; $i < count($rights); $i++){
  if ($rights[$i] == $action) $entitled = 1;
}

return $entitled;
  
}

?&gt;

I tried to you this entitle function in one of my controllers, but it ruins the whole caller function. I loaded the helper in the constructor:

Code:
$this->load->helper('common_functions');

Then, I tried to use the function this way:

Code:
if ($this->entitle(1005)) {
do some stuff here...
}

The result is a blank page. The strange thing is when I copy the function into the controller and remove the load for the helper it works perfectly fine.

I think I used the helper correctly according to CI user guide.
Please give me some suggestions, what can be the problem? Thanks in advance!
#2

[eluser]Noobigniter[/eluser]
Try without "$this->"
Code:
$this->load->helper('common_functions');

$entitle = entitle(1005);

if($entitle){
    do some stuff here...
}
#3

[eluser]zsela[/eluser]
Thanks for your reply, but it doesn't work either.
#4

[eluser]Noobigniter[/eluser]
Yet this is how I'm doing ...
ex:

i have this in "xy_functions_helpers" :

Code:
/*
** Super var_dump ^^
*/
function superdump()
{
  echo '<pre>';

  $num_args = func_num_args(); // compte et retourne le nombre d'argument sous forme de tableau

  for ($i = 0; $i < $num_args; ++$i)
  {
   print_r(func_get_arg($i)); // on affiche chaque argument
   echo "\n\n";
  }

  echo '</pre>';
  exit;
}

i put this in autoload :
Code:
$autoload['helper'] = array('form', 'url', 'html', 'cookie', 'xy_functions', 'language');

i use this ( everywhere ):

Code:
superdump($this->stuff);

and it's works.
#5

[eluser]CroNiX[/eluser]
Helpers are standalone procedural functions that know nothing about CI unless you bring the CI instance into the helper function. There is no "this" in a standalone procedural function as it's not a class.
Code:
$CI =& get_instance();  //get CI instance
$rights = $CI->session->userdata('rights');  //get something from the CI session...
It doesn't mention this in the helpers section of the users guide, but it does in Creating Libraries in the "Utilizing CodeIgniter Resources within Your Library" section. Be sure to read the whole guide!
#6

[eluser]zsela[/eluser]
Thanks for your answer. You are right, I should go through the whole User Guide again more deeply.

However, it is still not working as you suggested:
Helper:

Code:
function entitle ($action) {

$entitled = 0;
$CI =& get_instance();  //get CI instance
$rights = $CI->session->userdata('rights');
for ($i = 0; $i < count($rights); $i++){
  if ($rights[$i] == $action) $entitled = 1;
}

return $entitled;
  
}

Controller:

Code:
$this->load->helper('common_functions');
Code:
if (entitle(1002)) {
    
    $this->load->view('admin/admin_index');
    
   }

This code still has a result of an empty page.
#7

[eluser]CroNiX[/eluser]
What happens if you do a dump of $rights right after you grab the session and then die()?
#8

[eluser]zsela[/eluser]
This is pretty strange. I tried to use this code, to dump $rights, and some other text, but nothing happens:

Code:
function entitle ($action) {

$entitled = 0;
$CI =& get_instance();  //get CI instance
$rights = $CI->session->userdata('rights');
echo 'Some message';
print_r($rights);
die();
for ($i = 0; $i < count($rights); $i++){
  if ($rights[$i] == $action) $entitled = 1;
}

return $entitled;
  
}

In fact, when I load the helper in the constructor every function results a blank page, even those, which don't even use the entitle function or any other functions from the helper.
#9

[eluser]CroNiX[/eluser]
Strange, but I know it works or a lot of my sites wouldn't. Do you have error reporting turned on? Errors in logs? Blank pages usually indicate an error.
#10

[eluser]zsela[/eluser]
Unfortunately I don't know how to turn on error reporting. Can you help me with that?
When I saw a blank page before it was always caused by a CI error, so probably this is the case again.




Theme © iAndrew 2016 - Forum software by © MyBB