CodeIgniter Forums
One library depends on another - what's the solution? - 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: One library depends on another - what's the solution? (/showthread.php?tid=82601)



One library depends on another - what's the solution? - leafface - 07-28-2022

Hello,

To make sure I use the proper practice, I have to fully explain what's going on.

I have a library called 'Ui', responsible for displaying the front end. (It displays a main view that has other views inside it, variables for each view, and so on.)
The object for this library is created in BaseController::initController(). So all controllers have access to the $this->ui object. I suppose this is fine so far.

I have another library however, called 'Auth', which is responsible for handling the user logged in. Both classes are autoloaded like this:
PHP Code:
public $classmap = [
  'Ui' => APPPATH.'/Libraries/Ui.php',
  'Auth' => APPPATH.'/Libraries/Auth.php',
]; 

The constructor of Auth has to set a variable for Ui, which means Ui depends on Auth*. The two libraries have no access to one another though.
* EDIT: Auth depends on Ui

This same method worked fine in CI 2-3, where Auth simply executed a $this->ui->... method.

Is there a simple solution for this, or is the whole concept wrong? Should I use libraries for both these things in the first place?

Thanks for your time.


RE: One library depends on another - what's the solution? - iRedds - 07-28-2022

Use services for dependency injection.
https://codeigniter.com/user_guide/concepts/services.html


RE: One library depends on another - what's the solution? - kenjis - 07-28-2022

(07-28-2022, 12:06 PM)leafface Wrote: The constructor of Auth has to set a variable for Ui, which means Ui depends on Auth. The two libraries have no access to one another though.

This same method worked fine in CI 2-3, where Auth simply executed a $this->ui->... method.

It seems your explanation is something wrong.

If the constructor of Auth has to set a variable for Ui, that is Auth depends on Ui.
I think something like this:
PHP Code:
class Auth {
    private $ui;
    public function __construct($ui) {
        $this->ui $ui;
    }


If the two libraries really have no access to one another though, you don't need to set a variable for Ui in the Auth constructor. But you say "Auth simply executed a $this->ui->... method". That is Auth has access to Ui.


RE: One library depends on another - what's the solution? - leafface - 07-29-2022

(07-28-2022, 07:47 PM)iRedds Wrote: Use services for dependency injection.
https://codeigniter.com/user_guide/concepts/services.html

The thing why I decided against solving this with services is that the user guide says: "It is recommended to only create services within controllers."
My Ui library is used outside of controllers as well, e.g. in the Auth library.


RE: One library depends on another - what's the solution? - leafface - 07-29-2022

Your solution works! Many thanks!

BTW: Yes, sorry, Auth depends on Ui, not the other way around.