Where is the right place and way |
Hi,
i want to include a kind of library to check login informations, to handle some basic functions etc. So in the library i need db-support too. This library should be avivable on all pages and automatically also in models. As a library i need to add this code in every file where i want to use it (i think) "use App\Libraries\MyLib;" and create a new instance by "$mylib = new MyLib();" But i want to have access to the library without adding some code in every file. If i create it as a "helper", i need to add a new db-connection in every function, that i dont wont too. It should be a central place. It should be the best place for perfomance (so i dont wont to create x-instances etc.) So is it possible as a library, or is a "service" or "helper" the better/right way to do it?
1) Add it to the Autoload or classmap then you should be able to access it.
2) Create a service for it. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Do you mean i can create a service from my library?
I found only smal tutorial and docs about creating a own service. Is it possible to make the library himself globally accessable (like in my post)?
https://codeigniter.com/user_guide/conce...l#services
This is pretty thorough…
thanks,
but i'm so sorry,i dont understand somethings around the services: - Where i can load or put my classes from the library into the service The file wich extends the BaseService isnt it, or? - How i can access the classes from a service and put my parameters to the them? -Where i need to put the part to get the service and make it avivable into the global space of the project (in all controller, helper, models, views etc) without getting it new. I mean this part "$ myservice = service('myservice');" I cant find a simple example to make this thinks clear for me. In the meantime i find something, what i also dont understand, whats about Code: Factories::libraries('MyLibrary); Is it possible, in my tries it failed? Also i trie to setup my simple service as the following steps. 1) create my service-file: "Test.php" in /Config Code: namespace App\Config; 2)go to basecontroller (note i want to include the service in a central place to make it globally avivable) use \Config\Test; Code: ... in no one is the result of the test-function from the file Test.php - the expectation would have been that "hello what else..." would be displayed there What i do wrong?
app/ThirdParty
---- GoogleTranslate.php app/Config/Services PHP Code: <?php app/Helpers ---- language_helper.php PHP Code: /** What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
thanks a lot, for me it will more helpful if i understand what i doing wrong in my simple example instead to look into another example. May its possible to give me a feedback to my own example?
If i look at your example i have the following questions. - Is it a must to use the sample-file (Config/Services.php) and call the class-name who extends the BaseService as "Services"? - Is it possible to load my library in the same way like the third party file into a service - in my case by "use App\Libraries\MyLibname;"? |
Welcome Guest, Not a member yet? Register Sign In |