Home > Software design >  How To Call Helper In extends ResourceController Codeigniter 4
How To Call Helper In extends ResourceController Codeigniter 4

Time:03-03

I need help here. This is my RestFul API And This is my source code :

ApiMover.php

<?php
 
 namespace App\Controllers\ApiData;
 use App\Controllers\BaseController;
 use CodeIgniter\RESTful\ResourceController;
 use App\Models\Dennis_medoo_model;


class ApiMover extends ResourceController
{    

    function __construct()
    {       
        
    }
    

    // equal to get    
    public function index()
    {          
       
        
    }
    
        
    // equal to post
    public function create() {
            

    }

    // equal to get
    public function show($com = null) {                
        
        
        if ($com != "topvalue" && $com != "topgainer") {

            $response = [
                'status'   => 103,
                'error'    => 'Command Error',
                'messages' => 'Command Must Be : topvalue Or topgainer',
                'data' => null                
            ];
    
            return json_encode($response);  
        }

        if ($com == "topvalue") {
            include 'Mover/topvalue.php';
        }


        if ($com == "topgainer") {
            include 'Mover/topgainer.php';
        }
        
    }

    // equal to put    
    public function update($id = null) {
        
    }
    

    // equal to delete
    public function delete($id = null) {        
            
    }

}

Inside my class ApiMover in function show() I have a custom helper function name aasort(). The helper name is "Dennis_utility_helper". I already load it automatically. But when I use class that extends ResourceController which it is a RestFul API the helper function aasort() cannot be found.

Is there a way to call my custom helper in class ApiMover extends ResourceController ?

Thank You

CodePudding user response:

I found the solution. Just load manual the helper in the RestFul API example :

helper(Dennis_utility)

CodePudding user response:

The ResourceController extend BaseResource which extend Controller so, in my opinion, you can simply use autoload helper functionality in the same way as in BaseController...

class MyApiController extends ResourceController
{
    protected $helpers = ['email', 'cookie'];
    ...
  • Related