CodeIgniter Forums
Function inside the constructors are not working Any Help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Function inside the constructors are not working Any Help (/showthread.php?tid=77358)



Function inside the constructors are not working Any Help - shintojj - 08-21-2020

For example my constructor is given below and the following is not working if it is in the constructor . But it works outside the constructor

PHP Code:
public function _construct()
{
    
$this->session = \Config\Services::session();
    
$menu = new MenuModel();  
    
$data['menu_list'] = $menu->get_menu_list(session()->role_id);   
    echo 
view('common/menu',$data);




RE: Function inside the constructors are not working Any Help - jreklund - 08-21-2020

Hi, you need to put it inside a initController instead.

PHP Code:
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
{
    
// Do Not Edit This Line
    
parent::initController($request$response$logger);

    
$this->session = \Config\Services::session();
    
$menu = new MenuModel();  
    
$data['menu_list'] = $menu->get_menu_list(session()->role_id);   
    echo 
view('common/menu',$data);