CodeIgniter Forums
Issue with BaseController and inheritance - 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: Issue with BaseController and inheritance (/showthread.php?tid=74138)



Issue with BaseController and inheritance - SirTom - 07-29-2019

I have a problem with the BaseController and inheritance .

I am using Sub-Folders to organize my controllers. The controllers inherit from the BaseController is:


PHP Code:
<?php namespace App\Controllers;
use 
CodeIgniter\Controller;
class 
BaseController extends Controller
{ ... } 

<?
php namespace App\Controllers\Subfolder;
use 
App\Controllers\BaseController;
class 
MyController extends BaseController 
{ ... } 


The controller MyController has accesses to a protected function of the BaseController with:

PHP Code:
$this->baseControllerFunction(); 

In this function I use get_filenames from the filesystem-helper:

PHP Code:
$myCss get_filenames(FCPATH .'assets/css/'); 


So far this works. But the project is very complex and consists of several modules. So I want to add BaseControllers to each Sub-Folder, which inherit from the "global" BaseController.


PHP Code:
<?php namespace App\Controllers;
use 
CodeIgniter\Controller;
class 
BaseController extends Controller
{ ... }

<?
php namespace App\Controllers\Subfolder;
use 
App\Controllers\BaseController;
class 
SubfolderBaseController extends BaseController
{ ... }

<?
php namespace App\Controllers\Subfolder;
class 
MyController extends SubfolderBaseController 
{ ... } 


Now I get an error with the call:

PHP Code:
$this->baseControllerFunction(); 


Error-Message: Call to undefined function App\Controllers\get_filenames()

Why is the get_filenames function no longer available? The BaseController loads the helper for the filesystem with:


PHP Code:
protected $helpers = ['filesystem']; 


By inheritance the helper should be available in all subclases. So it is known to me from other object-oriented programming languages.

Thank you for your Support and Help!
Tom


RE: Issue with BaseController and inheritance - titounnes - 07-29-2019

add this script
helper('filesystem'):
before $myCSs\ ...


RE: Issue with BaseController and inheritance - SirTom - 07-29-2019

In the BaseController, there is alreay helper('filesystem'):


PHP Code:
protected $helpers = ['filesystem']; 


Everything works perfectly with one BaseController.

The error occurs when another BaseController from a Sub-Folder inherits of the global BaseController.


RE: Issue with BaseController and inheritance - MGatner - 07-29-2019

Could you share more of your code? That error message doesn't look right:
Error-Message: Call to undefined function App\Controllers\get_filenames()

There should be a class name in there, like "App\Controllers\SubfolderBaseController::get_filenames()".


RE: Issue with BaseController and inheritance - SirTom - 07-29-2019

PHP Code:
<?php namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
BaseController extends Controller
{
 
  protected $helpers = ['url''filesystem'];

 
  public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
 
  {
 
     parent::initController($request$response$logger);
 
  }

 
  protected function getCss()
 
  {
 
     $myCss get_filenames(FCPATH .'assets/css/');
 
     return $myCss;
 
  }



PHP Code:
<?php namespace App\Controllers\Subfolder;

use 
App\Controllers\BaseController;

class 
SubfolderController extends BaseController
{
 
  public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
 
  {
 
     parent::initController($request$response$logger);
 
  }

 
  // later more functions ...




PHP Code:
<?php namespace App\Controllers\Subfolder;

class 
MyFirstController extends SubfolderController 
{
 
  public function index() 
 
  {
 
     $data['myCss'] = $this->getCss();
 
     echo view('myView'$data);
 
  }



Error-Message: Call to undefined function App\Controllers\get_filenames()

   


RE: Issue with BaseController and inheritance - InsiteFX - 07-29-2019

You do not have to load the url helper anymore CI 4 loads it for you now.

Are you using a constructor in your controllers?


RE: Issue with BaseController and inheritance - SirTom - 07-30-2019

(07-29-2019, 04:04 PM)InsiteFX Wrote: You do not have to load the url helper anymore CI 4 loads it for you now.

Are you using a constructor in your controllers?

No, I do no use a constructor in my controllers. In the documentation it says, you should realize this via the BaseController now.

With only one BaseController all is fine. With two inherited BaseControllers the error occurs.


RE: Issue with BaseController and inheritance - haroldentwist - 07-22-2021

(07-29-2019, 03:35 AM)SirTom Wrote: I have a problem with the BaseController and inheritance .

I am using Sub-Folders to organize my controllers. The controllers inherit from the BaseController is:


PHP Code:
<?php namespace App\Controllers;
use 
CodeIgniter\Controller;
class 
BaseController extends Controller
{ ... } 

<?
php namespace App\Controllers\Subfolder;
use 
App\Controllers\BaseController;
class 
MyController extends BaseController 
{ ... } 


The controller MyController has accesses to a protected function of the BaseController with:

PHP Code:
$this->baseControllerFunction(); 

In this function I use get_filenames from the filesystem-helper:

PHP Code:
$myCss get_filenames(FCPATH .'assets/css/'); 


So far this works. But the project is very complex and consists of several modules. So I want to add BaseControllers to each Sub-Folder, which inherit from the "global" BaseController.


PHP Code:
<?php namespace App\Controllers;
use 
CodeIgniter\Controller;
class 
BaseController extends Controller
{ ... }

<?
php namespace App\Controllers\Subfolder;
use 
App\Controllers\BaseController;
class 
SubfolderBaseController extends BaseController
{ ... }

<?
php namespace App\Controllers\Subfolder;
class 
MyController extends SubfolderBaseController 
{ ... } 


Now I get an error with the call:

PHP Code:
$this->baseControllerFunction(); 


Error-Message: Call to undefined function App\Controllers\get_filenames()

Why is the get_filenames function no longer available? The BaseController loads the helper for the filesystem with:


PHP Code:
protected $helpers = ['filesystem']; 


By inheritance the helper should be available in all subclases. So it is known to me from other object-oriented programming languages.

Thank you for your Support and Help!
Tom

You have a scripting problem. More specifically, you have a script conflict. An error was made during creation, which now prevents you from completing the project.