• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI3 to CI4 Migrations (__construct)

#1
Hello,

i'm currently migrating a CI3 project over to CI4, my old project relied heavily on __constructor() so that each extended controller would require this.
PHP Code:
public function __construct($permission_to_check)
 
   {
 
       parent::__construct();
 
       $this->lang->load('2fa/2fa_error_messages'$this->user_lang);
 
       $this->permission_to_check $permission_to_check;
 
       if(!$this->check_permission($this->permission_to_check)){
 
           redirect('home');
 
       }
 
   

So each extended controller checks a specific permission on construct and redirects if the permission is not set.

PHP Code:
parent::__construct('has_perm_024D'); 


In CI4 i've noticed that __construct() is no longer required i wonder if it's still a "good practice"  to use the __construct() function or is there a way to achieve this with the initController function.

It might be a silly question but i want to get it right from the start Big Grin

Thanks for the tips and / or info.

Best regards,
Bart
Reply

#2
You can definitely use the constructor for almost anything you need to. It was purposefully left blank so framework code didn't get in the way of what you needed.

However, redirects no longer work from within constructors. Instead, you should look at controller filters for permission checks and the like.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#3
Hi kilishan,

Thanks for the information. I've just stumbled upon the filters myself, you suggestion confirms that i'm in the right place.
Looks like Before Filters can replace __construct() in my case , yet is still have to figure out how to pass the $permission_to_check arg to it.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.