CodeIgniter Forums
Suggestion for extension of controller - 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: Suggestion for extension of controller (/showthread.php?tid=78103)



Suggestion for extension of controller - Matleyx - 11-30-2020

Hello guys.

I need some advice on CI4. In ci3 I extended the controller with the MY_controller like this:
PHP Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


 
class 
MY_Controller extends CI_Controller
{
  function __construct()
  {
    parent::__construct();

  }
}

class 
Guest_Controller extends MY_Controller        //Controller for Not Logged (free)
{
  function __construct()
  {
    parent::__construct();

  }
}

class 
Logged_Controller extends MY_Controller        //Controller for ALL Logged
{
  function __construct()
  {
    parent::__construct();
    if (!$this->ion_auth->logged_in())
        {
            
            redirect
('auth/login');
        }
  }
}

class 
Admin_Controller extends MY_Controller        //Controller for Admin
{
  function __construct()
  {
    parent::__construct();
    if (!$this->ion_auth->logged_in())
        {
            
            redirect
('auth/login');
        }
        else
            {
            if(!$this->ion_auth->is_admin())
                    {
                        $this->session->set_flashdata('flashError''This page is only for ADMIN');
                        redirect('example_logged');
                    }
            }
    }
  

so in each controller that i created, I extended the class I needed (adminController, LoggedController etc)

How can I get the same result with the BaseController? What class do I extend in my controllers, depending on whether they are admin, logged etc?

Thanks to all


RE: Suggestion for extension of controller - InsiteFX - 11-30-2020

Just think of the BaseController as the MY_Controller same principle.

Only thing you will need to change is your checking for logged in etc.;
You will need to create a Filter for that. Also the Controllers do not use
a __contrust they use a initController meyhod instead.

A good example is Myth/Auth, I suggest that you take a look at it.

Myth:Auth


RE: Suggestion for extension of controller - Matleyx - 12-02-2020

Hi Insite...thanks for the answer.
I look at the Filter official documentation, but if i correctly undestand, i must declare EACH my controller in the route file with the right filter. Is correct?
In CI3, in that mode, was very simple: if i wnted chech logged, i extended the logged Controller, if admin, i extended the admin controller ecc.
An other thing: in that mode was very simple to manteinance.

Is it possible recreated the same solution with ci 4?

Thanks for the answer...


RE: Suggestion for extension of controller - InsiteFX - 12-02-2020

It should be, but I have not had the time to test it.


RE: Suggestion for extension of controller - Matleyx - 12-02-2020

(12-02-2020, 10:13 AM)InsiteFX Wrote: It should be, but I have not had the time to test it.
Smile Smile Smile Smile
Ahahahahah....
No no Insite, i don't asking you to make this work for me!!!!!
But if you Have any suggestion for me, is very good....
I'm tring CI4, and i want to create a simple starter pack for my abitually use. 

Thanks a lot!


RE: Suggestion for extension of controller - InsiteFX - 12-02-2020

I would add it to the your controllers that extend the BaseController.

IT should still work the same as you did it before.