Welcome Guest, Not a member yet? Register   Sign In
Suggestion for extension of controller
#1

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
Reply
#2

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
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

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...
Reply
#4

It should be, but I have not had the time to test it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(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!
Reply
#6

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

IT should still work the same as you did it before.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB