skip to Main Content

in autoload.php

 public $helpers = [
        'general'
    ];

helper is loaded and helper file defined 2 functions

<?php

function datetimeformat($date)
{
    return date("Y-m-d H:i:s", strtotime($date));
}

function randomnumber()
{
    return "111";
} 

and in controller i called the helper but is not working

<?php

namespace AppControllers;


/**
 * Rating Controller For Admin
 */
class Rating extends BaseController
{

    public function index()
    {
        // Load either general_helper or general
        if ( helper('general_helper') || helper('general')) {
echo "test";exit();
        }

    }
}

in this code helper is not loaded i cant find what is the issue please any one have idea

2

Answers


  1. The helper() function does not return a value, so don’t try to assign it to a variable.

    So your conditional check will never evaluate as true.

    After you’ve loaded your helpers, just check if the desired function exists using exit(function_exists('randomnumber') ? 'exists' : 'nope');.

    See also:

    Login or Signup to reply.
  2. This line is useless if ( helper('general_helper') || helper('general'))

    You do not have to check whether the helper has been loaded.
    It should be loaded, since you did it within the autoload.

    If it’s not loaded, then:

    1. Verify that the name of the helper general_helper corresponds to the name of the file. It must be Helpersgeneral_helper.php

    2. Call public $helpers = ['general_helper']; in the autoload.

    This is for CI4, as described in the official documentation.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search