CodeIgniter Forums
how i can create my library in codeigniter 4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: how i can create my library in codeigniter 4 (/thread-75969.html)



how i can create my library in codeigniter 4 - Hoodini - 04-02-2020

this is easy in codeigniter 3 but i cant find this in codeigniter 4 document


RE: how i can create my library in codeigniter 4 - FoLez - 04-02-2020

I update the Google Recaptcha Class from version 3 to 4, here is an example
[Attachment = 1748]
and in system/Controller.php in the initController function, I define it in a global variable for all controller's $this->g_re = new Recaptcha ($request, $response);


RE: how i can create my library in codeigniter 4 - dave friend - 04-02-2020

(04-02-2020, 01:03 PM)Hoodini Wrote: this is easy in codeigniter 3 but i cant find this in codeigniter 4 document

It is even easier than V3. The only tricky bit is learning to use namespace.

Here's a very simple library

app/Libraries/MyCustomClass.php

PHP Code:
<?php namespace App\Libraries;

class 
MyCustomClass
{
 public function 
sayHi()
 {
 return 
"Hello World!";
 }



Here's a simple example of using the custom library in a controller.

PHP Code:
<?php namespace App\Controllers;

use 
App\Libraries\MyCustomClass;  //so we can easily load the custom library

class Home extends BaseController
{
 public function 
index()
 {
 
$mine = new MyCustomClass(); // loads and creates instance
 
echo $mine->sayHi();
 }




RE: how i can create my library in codeigniter 4 - Hoodini - 04-02-2020

thanks a lot!
Codeigniter always the best and


RE: how i can create my library in codeigniter 4 - includebeer - 04-04-2020

If you need a single instance of a class, you can create a service that will return a shared instance of your library: https://codeigniter4.github.io/userguide/concepts/services.html#shared-classes