CodeIgniter Forums
How to create a new library and use it with controller? - 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: How to create a new library and use it with controller? (/showthread.php?tid=79101)



How to create a new library and use it with controller? - sr13579 - 04-19-2021

I am writing a very big application and I think it would be great if i could put that in a library and use it in my controller. How can I easily build up a simple library and use that in my Controller?
Care to give me an example?


RE: How to create a new library and use it with controller? - InsiteFX - 04-19-2021

Example:

PHP Code:
<?php

namespace App\Libraries;

/**
 * Class ExampleLibrary
 *
 * @package App\Libraries
 *          
 * in your controller or where you load the library
 *          
 * use App\Libraries\Example;
 */
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 ExampleLibrary Library Class.

/**
 * -----------------------------------------------------------------------
 * Filename: ExampleLibrary.php
 * Location: ./app/Libraries/ExampleLibrary.php
 * -----------------------------------------------------------------------
 */ 



RE: How to create a new library and use it with controller? - rich8374 - 12-23-2021

In your controller, use
Code:
use App\Libraries\ExampleLibrary;
above the class
and then you can instantiate your library class in a method with
Code:
$exampleLibrary = new ExampleLibrary();