Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 4: Get value from BaseController to custom helper
#1

Seems get_instance() is no longer working in Codeigniter 4. But I need to get some value from BaseController to my custom_helper. Here is my code snippet:
BaseController.php
Code:
<?php namespace App\Controllers;
class BaseController extends Controller
{
protected $helpers = ['custom'];
public function initController(...) {
  parent::initController(...);

  $myconfig = config('MyConfig');
  $this->languages = $myconfig->languages;
  $this->selected_lang = $myconfig->site_lang;
  $lang_segment = $this->request->uri->getSegment(1);
  foreach ($this->languages as $lang) {
    if ($lang_segment == $lang->short_form) {
      $this->selected_lang = $lang;
      $this->lang_base_url = base_url() . $lang->short_form . "/";
    }
  }
}
}
// Here I need to pass ($this->selected_lang) value to my custom_helper.

custom_helper.php
Code:
<?php
if (!function_exists('trans')) {
    function trans($string)
    {
        $language_translations = get_translation_array($this->selected_lang->id);
        // --> Here I want to get ($this->selected_lang->id) value from BaseController.
       
        if (!empty($language_translations[$string])) {
            return $language_translations[$string];
        }
        return "";
    }
}
function get_translation_array($land_id)
{
    .......
}

I'm not sure is it possible or not! I'm newbie in CI4. Please suggest if is there any solutions. Thanks in advance.
Reply
#2

Pass it from the BaseController to your helper using sessions.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB