Helper - Call to undefined function |
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 app/Controllers/News.php (snippet) PHP Code: public function index() 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.
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
/*** NO support in private message - Use the forum! ***/
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 )
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 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"> 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
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
/*** NO support in private message - Use the forum! ***/ (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
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(); (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: 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(); since in the navigation menu items, anybody and in any situation will use the same strings Thank you |
Welcome Guest, Not a member yet? Register Sign In |