Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ionAuth as CI4 service? (question) #1567

Open
dgvirtual opened this issue Jul 13, 2022 · 2 comments
Open

ionAuth as CI4 service? (question) #1567

dgvirtual opened this issue Jul 13, 2022 · 2 comments

Comments

@dgvirtual
Copy link
Contributor

Hi, I am using ionAuth in codeigniter filters and am wondering if calling in each filter (and then in the baseController)

$ionAuth = new \IonAuth\Libraries\IonAuth();

is perhaps not optimal in terms of performance... Does it hit the database each time I do it? How do I get the shared instance of the class in the filters?

I see calling CI libraries as services returns a shared class, so is there any value in defining IonAuth also as a service? If so, maybe someone has it done already and could share?

Sorry if these questions sound silly, I am not very good at OOP...

@benedmunds
Copy link
Owner

Yes that's definitely inefficient.

I would define an auth class property in your base controller and then use that in the filters. Here's an example of doing that in CI4: https://stackoverflow.com/questions/58900176/codeigniter-4-autoload-library

@dgvirtual
Copy link
Contributor Author

dgvirtual commented Sep 16, 2022

Unless I messed something up in my tests, the filters' method before() seems to run before the BaseController method initController, so I cannot define auth class property in the BaseController.

But I have implemented ionAuth as a service this way:

added a service method in the app/Config/Services.php class:

    public static function ionAuth($getShared = true)
    {
        if ($getShared) {
            return static::getSharedInstance('ionAuth');
        }
        return new \IonAuth\Libraries\IonAuth();
    }

And then in both BaseController's initController method and the filters I can run

$this->ionAuth = \Config\Services::ionAuth();

without worrying which of them runs first...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants