CodeIgniter Forums
public function __construct() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: public function __construct() (/showthread.php?tid=89148)



public function __construct() - Knutsford - 01-16-2024

PHP Code:
public function __construct()
{
     parent::__construct();
     $this->load->model('youth_user_model');
 
     $this->load->helper('url');
 
     $this->load->helper('html');
}


I am trying to convert a site  from version 3 to 4 of codeigniter

I am getting cannot call constructor on line 10
Line 10 is parent
::__construct();truct()();

What should that bit of code look like for version 4 please? Do I just remove that lineThanks 



RE: public function __construct() - InsiteFX - 01-16-2024

PHP Code:
All of these will load your helpers.

helper(['url''html']);

protected 
$helpers = ['url''htnl'];

Auto-loading Helpers
New in version 4.3.0.

If 
you find that you need a particular helper globally throughout your application
you can tell CodeIgniter to auto-load it during system initialization.
This is done by opening the app/Config/Autoload.php file and adding the helper to 
the $helpers property




RE: public function __construct() - adimancifi - 01-16-2024

I hope this work for you.

CodeIgniter v3.x
PHP Code:
class Home extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        
        $this
->load->model('youth_user_model');
        $this->load->helper('url');
        $this->load->helper('html');
    }
    
    
public function index()
    {
        return $this->load->view('home');
    }




CodeIgniter v4.4.4

PHP Code:
namespace App\Controllers;

use 
Config\Services;
use 
CodeIgniter\HTTP\CLIRequest;
use 
CodeIgniter\HTTP\IncomingRequest;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Psr\Log\LoggerInterface;

class 
Home extends BaseController 
{
    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        parent::initController($request$response$logger);
        
        $this
->youth_user_model model('youth_user_model');
        helper(['url','html']);
    }
    
    
public function index()
    {
        return view('home');
    }




RE: public function __construct() - kenjis - 01-16-2024

See https://codeigniter4.github.io/CodeIgniter4/installation/upgrade_4xx.html


RE: public function __construct() - Knutsford - 01-17-2024

Thanks the last one was what I was looking for