CodeIgniter Forums
CI3 to CI4 Migrations (__construct) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: CI3 to CI4 Migrations (__construct) (/thread-73367.html)



CI3 to CI4 Migrations (__construct) - bartMommens - 04-16-2019

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


RE: CI3 to CI4 Migrations (__construct) - kilishan - 04-16-2019

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.


RE: CI3 to CI4 Migrations (__construct) - bartMommens - 04-16-2019

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.


RE: CI3 to CI4 Migrations (__construct) - bartMommens - 05-07-2019

(04-16-2019, 06:42 AM)kilishan Wrote: 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.

Hi Kilishan,

I finally dived into codeigniter 4 's Controller filters and must say: i'm amazed how powerful this really is. Compared to Codeigniter 3 this is a serious code reduction for some projects.

Thank you pointing me into the right direction.


RE: CI3 to CI4 Migrations (__construct) - kilishan - 05-07-2019

(05-07-2019, 06:27 AM)bartMommens Wrote:
(04-16-2019, 06:42 AM)kilishan Wrote: 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.

Hi Kilishan,

I finally dived into codeigniter 4 's Controller filters and must say: i'm amazed how powerful this really is. Compared to Codeigniter 3 this is a serious code reduction for some projects.

Thank you pointing me into the right direction.

Hey bartMommens, glad you think so! It will hopefully proved to be really powerful and flexible for many needs, so looking forward to seeing how it gets used!


RE: CI3 to CI4 Migrations (__construct) - Avega Soft - 05-07-2019

(04-16-2019, 06:42 AM)kilishan Wrote: 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.

If I am right understanding the Controller Filters starting working before apps controllers and check url's or form action type as hooks in CI3 ?


RE: CI3 to CI4 Migrations (__construct) - kilishan - 05-08-2019

(05-07-2019, 09:02 PM)Avega Soft Wrote:
(04-16-2019, 06:42 AM)kilishan Wrote: 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.

If I am right understanding the Controller Filters starting working before apps controllers and check url's or form action type as hooks in CI3 ?

Controller filters will be ran either just before running the controller method, or just after. There are still "hooks" in CI4 similar to what you're used to in CI3, but they've been renamed to "Events" now and made more flexible.

https://codeigniter4.github.io/CodeIgniter4/extending/events.html


RE: CI3 to CI4 Migrations (__construct) - Avega Soft - 05-10-2019

(05-08-2019, 07:05 AM)kilishan Wrote:
(05-07-2019, 09:02 PM)Avega Soft Wrote:
(04-16-2019, 06:42 AM)kilishan Wrote: 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.

If I am right understanding the Controller Filters starting working before apps controllers and check url's or form action type as hooks in CI3 ?

Controller filters will be ran either just before running the controller method, or just after. There are still "hooks" in CI4 similar to what you're used to in CI3, but they've been renamed to "Events" now and made more flexible.

https://codeigniter4.github.io/CodeIgniter4/extending/events.html

Thank you for the answer, you helped me a lot.