Welcome Guest, Not a member yet? Register   Sign In
Helper - Call to undefined function
#1

(This post was last modified: 10-16-2020, 03:10 AM by sjender.)

Hi All,

I'm new to CI (just started this week).

I am trying to get my own custom helper to work, but I keep getting 'Call to undefined function' errors.

Here's my code:

app/Helpers/build_page.php
PHP Code:
<?php

function tester(){
    return "test";


app/Controllers/News.php (snippet)
PHP Code:
public function index()
    {
        helper('build_page');
        echo tester(); 

As read in the manual I guess this should work?
But I keep getting:

Call to undefined function App\Controllers\tester()

What am I missing here.
Reply
#2

It's not very clear in the documentation, but when you create a new helper, the filename must have the '_helper' suffix: https://codeigniter4.github.io/userguide...g-a-helper

So rename your file to build_page_helper.php and it should work. To load the file, you don't need the suffix, so keep this line as is: 
PHP Code:
helper('build_page'); 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#3

As @ includebeer said, all helper files need to be like this name_helper.php
you always have to save the file with the _helper.php extension.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

Hello dear CI4 experts

I'm in a similar situation

I have
/App/Helpers/isactive_helper.php

the content of isactive_helper.php is

PHP Code:
<?php 

if ( ! function_exists('setActive')){

    function setActive($uriCheck)
    {
        $uri = new \CodeIgniter\HTTP\URI();
        
        $uriSegment 
$uri->getPath();

        if ( $uriSegment == $uriCheck ) {

            echo "active";

        }
    }


I then use this helper inside a view (which is the main layout view of my app)

Code:
        24        <ul class="navbar-nav mr-auto">
        25            <li class="nav-item <?php setActive("tasks"); ?>">
        26                <a class="nav-link" href="<?= site_url("tasks") ?>">Tasks</a>
        27            </li>

but as I open the app URL, I get

Call to undefined function setActive()

and it is highlighted exactly line 25 of the view

Can you kindly hint with what's wrong?

Thank you
Reply
#5

You need to load the file in your controller before you can use its functions in a view.

PHP Code:
helper(“isactive”); 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#6

(This post was last modified: 02-28-2021, 01:14 PM by Corsari.)

(02-28-2021, 12:38 PM)includebeer Wrote: You need to load the file in your controller before you can use its functions in a view

the "Call to undefined function" problem is solved

but ...

for the above code,

Code:
$uriSegment = $uri->getPath();

used inside the helper returns always "" , an empty string ( tested with dd() )

while used inside the view, it actually returns the path at right of the site_url
Reply
#7

The URI class needs an actual URI to work with. So instead of creating one directly, you can get the current URI from the current request:


PHP Code:
// $uri = new \CodeIgniter\HTTP\URI();

$request = \Config\Services::request();

$uri $request->uri
Reply
#8

(This post was last modified: 03-02-2021, 12:39 AM by Corsari.)

(03-01-2021, 02:28 AM)daveontheciforum Wrote: The URI class needs an actual URI to work with. So instead of creating one directly, you can get the current URI from the current request:


PHP Code:
// $uri = new \CodeIgniter\HTTP\URI();

$request = \Config\Services::request();

$uri $request->uri

Thank you Dave

it works! , I conceived and created my first helper !

Though as seen in the layout HTML above, I went for

PHP Code:
        $request = \Config\Services::request();
        
        $uriSegment 
$request->uri->getPath(); 

since in the navigation menu items, anybody and in any situation will use the same strings

Thank you
Reply




Theme © iAndrew 2016 - Forum software by © MyBB