hari prasath
hari prasath

Reputation: 13

Creating Helper function is not working in codeigniter 4

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 App\Controllers;


/**
 * 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

Upvotes: 0

Views: 88

Answers (2)

b126
b126

Reputation: 1254

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 \Helpers\general_helper.php

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

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

Upvotes: 2

mickmackusa
mickmackusa

Reputation: 47844

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:

Upvotes: 0

Related Questions