Welcome Guest, Not a member yet? Register   Sign In
how i can create my library in codeigniter 4
#1

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

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);

Attached Files
.php   Recaptcha.php (Size: 4.05 KB / Downloads: 20)
Reply
#3

(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();
 }

Reply
#4

thanks a lot!
Codeigniter always the best and
Reply
#5

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...ed-classes
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply




Theme © iAndrew 2016 - Forum software by © MyBB