applications->library folder create File
example Acb.php make 1st word capital or in some hostings your library will not found
then make class like this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class abc {
functions xyz(){}
}
you can make functions inside the class
in your controller load the library and call the function
-
ciadmin
Cat Herder
-
Posts: 1,319
Threads: 21
Joined: Jan 2014
Reputation:
71
Er, The above advice is more for CI3.
For CI4, create libraries as classes inside some app folder, eg app/Libraries, and make sure that they are properly namepaced.
Eg:
<?php namespace App\Libraries\MyStuff;
class MyStuff {
}
and then load/reference it with
$mystuff = new App\Libraries\MyStuff();
and away you go.
-
MatheusCastro
Junior Member
-
Posts: 28
Threads: 16
Joined: Dec 2019
Reputation:
0
12-08-2019, 04:27 AM
(This post was last modified: 12-08-2019, 06:40 AM by MatheusCastro.)
(12-07-2019, 09:24 PM)ciadmin Wrote: Er, The above advice is more for CI3.
For CI4, create libraries as classes inside some app folder, eg app/Libraries, and make sure that they are properly namepaced.
Eg:
<?php namespace App\Libraries\MyStuff;
class MyStuff {
}
and then load/reference it with
$mystuff = new App\Libraries\MyStuff();
and away you go.
Perfect, it works! Exemple:
PHP Code: <?php
namespace App\Libraries\Payment;
class Payment {
public function configName($name): string { return $name; } }
Another thing, it is recommended to use
PHP Code: new \App\Libraries\Payment();
Or:
PHP Code: //This use App\Libraries\Payment;
class Home extends Controller { public function index() { //This $payment = new Payment();
{... }
-
Martin_Salas
Junior Member
-
Posts: 16
Threads: 4
Joined: Jan 2019
Reputation:
0
(12-07-2019, 09:24 PM)ciadmin Wrote: Er, The above advice is more for CI3.
For CI4, create libraries as classes inside some app folder, eg app/Libraries, and make sure that they are properly namepaced.
Eg:
<?php namespace App\Libraries\MyStuff;
class MyStuff {
}
and then load/reference it with
$mystuff = new App\Libraries\MyStuff();
and away you go.
Sorry I do not understand. When trying the example, I get the following error: Class 'App\Controllers\App\Libraries\MyStuff' not found
What do I still have to do, to make it work correctly?
-
InsiteFX
Super Moderator
-
Posts: 6,502
Threads: 323
Joined: Oct 2014
Reputation:
239
Example CI 4 Library
PHP Code: <?php namespace App\Libraries;
/** * Class ExampleLibrary * * @package App\Libraries */ class ExampleLibrary { /** * Class properties go here. * ------------------------------------------------------------------- * public, private, protected, static and const. */
// -------------------------------------------------------------------
/** * __construct () * ------------------------------------------------------------------- * * Class Constructor * * NOTE: Not needed if not setting values or extending a Class. * */ public function __construct() {
}
// -------------------------------------------------------------------
} // End of Name Library Class.
/** * ----------------------------------------------------------------------- * Filename: ExampleLibrary.php * Location: ./app/Libraries/ExampleLibrary.php * ----------------------------------------------------------------------- */
Example Controller loading the Example Library:
PHP Code: <?php namespace App\Controllers;
use App\Libraries\ExampleLibrary;
/** * Class ExampleController * * @package App\Controllers */ class ExampleController extends BaseController { /** * Class properties go here. * ------------------------------------------------------------------- * public, private, protected, static and const. */
// -------------------------------------------------------------------
/** * __construct () * ------------------------------------------------------------------- * * Class Constructor * * NOTE: Not needed if not setting values or extending a Class. * */ public function __construct() { parent::__construct();
}
// -------------------------------------------------------------------
/** * index () * ------------------------------------------------------------------- * */ public function index() { $newExampleLibrary = new ExampleLibrary(); } // --------------------------------------------------------------------
} // End of ExampleController Controller Class.
/** * ----------------------------------------------------------------------- * Filename: ExampleController.php * Location: ./app/Controllers/ExampleController.php * ----------------------------------------------------------------------- */
Try that.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
-
Martin_Salas
Junior Member
-
Posts: 16
Threads: 4
Joined: Jan 2019
Reputation:
0
(12-15-2019, 09:33 AM)InsiteFX Wrote: Example CI 4 Library
PHP Code: <?php namespace App\Libraries;
/** * Class ExampleLibrary * * @package App\Libraries */ class ExampleLibrary { /** * Class properties go here. * ------------------------------------------------------------------- * public, private, protected, static and const. */
// -------------------------------------------------------------------
/** * __construct () * ------------------------------------------------------------------- * * Class Constructor * * NOTE: Not needed if not setting values or extending a Class. * */ public function __construct() {
}
// -------------------------------------------------------------------
} // End of Name Library Class.
/** * ----------------------------------------------------------------------- * Filename: ExampleLibrary.php * Location: ./app/Libraries/ExampleLibrary.php * ----------------------------------------------------------------------- */
Example Controller loading the Example Library:
PHP Code: <?php namespace App\Controllers;
use App\Libraries\ExampleLibrary;
/** * Class ExampleController * * @package App\Controllers */ class ExampleController extends BaseController { /** * Class properties go here. * ------------------------------------------------------------------- * public, private, protected, static and const. */
// -------------------------------------------------------------------
/** * __construct () * ------------------------------------------------------------------- * * Class Constructor * * NOTE: Not needed if not setting values or extending a Class. * */ public function __construct() { parent::__construct();
}
// -------------------------------------------------------------------
/** * index () * ------------------------------------------------------------------- * */ public function index() { $newExampleLibrary = new ExampleLibrary(); } // --------------------------------------------------------------------
} // End of ExampleController Controller Class.
/** * ----------------------------------------------------------------------- * Filename: ExampleController.php * Location: ./app/Controllers/ExampleController.php * ----------------------------------------------------------------------- */
Try that. WORKS! Thank you very much
-
miqueiaspenha
Newbie
-
Posts: 1
Threads: 0
Joined: Oct 2015
Reputation:
0
(12-15-2019, 09:33 AM)InsiteFX Wrote: Example CI 4 Library
PHP Code: <?php namespace App\Libraries;
/** * Class ExampleLibrary * * @package App\Libraries */ class ExampleLibrary { /** * Class properties go here. * ------------------------------------------------------------------- * public, private, protected, static and const. */
// -------------------------------------------------------------------
/** * __construct () * ------------------------------------------------------------------- * * Class Constructor * * NOTE: Not needed if not setting values or extending a Class. * */ public function __construct() {
}
// -------------------------------------------------------------------
} // End of Name Library Class.
/** * ----------------------------------------------------------------------- * Filename: ExampleLibrary.php * Location: ./app/Libraries/ExampleLibrary.php * ----------------------------------------------------------------------- */
Example Controller loading the Example Library:
PHP Code: <?php namespace App\Controllers;
use App\Libraries\ExampleLibrary;
/** * Class ExampleController * * @package App\Controllers */ class ExampleController extends BaseController { /** * Class properties go here. * ------------------------------------------------------------------- * public, private, protected, static and const. */
// -------------------------------------------------------------------
/** * __construct () * ------------------------------------------------------------------- * * Class Constructor * * NOTE: Not needed if not setting values or extending a Class. * */ public function __construct() { parent::__construct();
}
// -------------------------------------------------------------------
/** * index () * ------------------------------------------------------------------- * */ public function index() { $newExampleLibrary = new ExampleLibrary(); } // --------------------------------------------------------------------
} // End of ExampleController Controller Class.
/** * ----------------------------------------------------------------------- * Filename: ExampleController.php * Location: ./app/Controllers/ExampleController.php * ----------------------------------------------------------------------- */
Try that.
Thanks!
|