CodeIgniter Forums
Helper - Call to undefined function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Helper - Call to undefined function (/showthread.php?tid=77770)



Helper - Call to undefined function - sjender - 10-16-2020

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.


RE: Helper - Call to undefined function - includebeer - 10-16-2020

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/general/helpers.html?highlight=_helper#loading-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'); 



RE: Helper - Call to undefined function - InsiteFX - 10-16-2020

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.


RE: Helper - Call to undefined function - Corsari - 02-28-2021

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


RE: Helper - Call to undefined function - includebeer - 02-28-2021

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

PHP Code:
helper(“isactive”); 



RE: Helper - Call to undefined function - Corsari - 02-28-2021

(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


RE: Helper - Call to undefined function - daveontheciforum - 03-01-2021

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



RE: Helper - Call to undefined function - Corsari - 03-01-2021

(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