Welcome Guest, Not a member yet? Register   Sign In
Custom library available like CI3
#2

Here's one example. There are probably other ways and v4 development team members might be able to suggest more appropriate approaches. But this works.

This example is done using a fresh install of v4.
The only default value changed is that $baseURL in /application/Config/App.php is set with the testing server's URL.
No items are removed or added to the file /application/Config/Routes.php

Here's a very simple "library"

/application/Libraries/Test.php
PHP Code:
<?php namespace App\Libraries;

class 
Test
{
    protected 
$message;

    public function 
__construct()
    {
        
$this->message 'Greetings from the class <em>Test</em>.';
    }

    public function 
getMessage()
    {
        return 
$this->message;
    }



We will create an instance of this class and use the class method getMessge() in the controller Base

/application/Controllers/Base.php
PHP Code:
<?php namespace App\Controllers;

class 
Base extends \CodeIgniter\Controller
{
    
/**
     * @var \App\Libraries\Test instance of Test class
     */
    
protected $testLib;

    public function 
__construct(...$params)
    {
        
parent::__construct(...$params);
        
//create instance of "Test" class assign it to the controller's property
        
$this->testLib = new \App\Libraries\Test();
    }

    public function 
index()
    {
        
$data['title'] = 'Testing 1,2,3'//Browser tab text
        // Use the "Test" class
        
$data['message'] = $this->testLib->getMessage();
        
//show the view file
        
echo view('test_message'$data);
    }



The above controller can be used to extend other controllers and the property $testLib can be accessed in child classes using
$this->testLib.

The "view" is a hacked... er, revised version of welcome_message.php that comes with a clean install.

/applications/Views/test_message.php
Code:
<!doctype html>
<html>
    <head>
        <title><?= $title; ?></title>

        <link rel="shortcut icon" type="image/png" href="/favicon.ico"/>
    </head>
    <body>

        <style {csp-style-nonce}>
            div.logo {
                height: 200px;
                width: 155px;
                display: inline-block;
                opacity: 0.08;
                position: absolute;
                top: 2rem;
                left: 50%;
                margin-left: -73px;
            }
            body {
                height: 100%;
                background: #fafafa;
                font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
                color: #777;
                font-weight: 300;
            }
            h1 {
                font-weight: lighter;
                letter-spacing: 0.8;
                font-size: 3rem;
                margin-top: 145px;
                margin-bottom: 0;
                color: #222;
            }
            .wrap {
                max-width: 1024px;
                margin: 5rem auto;
                padding: 2rem;
                background: #fff;
                text-align: center;
                border: 1px solid #efefef;
                border-radius: 0.5rem;
                position: relative;
            }
            .version {
                margin-top: 0;
                color: #999;
            }
            .guide {
                margin-top: 3rem;
                text-align: left;
            }
            .msg{
                font-weight: bold;
            }
            pre {
                white-space: normal;
                margin-top: 1.5rem;
            }
            code {
                background: #fafafa;
                border: 1px solid #efefef;
                padding: 0.5rem 1rem;
                border-radius: 5px;
                display: block;
            }
            p {
                margin-top: 1.5rem;
            }
            .footer {
                margin-top: 2rem;
                border-top: 1px solid #efefef;
                padding: 1em 2em 0 2em;
                font-size: 85%;
                color: #999;
            }
            a:active,
            a:link,
            a:visited {
                color: #dd4814;
            }
        </style>

        <div class="wrap">

            <h1>Welcome to CodeIgniter</h1>

            <p class="version">version <?= CodeIgniter\CodeIgniter::CI_VERSION ?></p>

            <div class="logo">
                <svg version="1.0" xmlns="http://www.w3.org/2000/svg"
                     width="155.000000px" height="200.000000px" viewBox="0 0 155.000000 200.000000"
                     preserveAspectRatio="xMidYMid meet">
                <g transform="translate(0.000000,200.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
                <path d="M737 1963 c22 -79 -7 -185 -78 -290 -18 -26 -107 -122 -197 -213
                      -239 -240 -336 -371 -403 -544 -79 -206 -78 -408 5 -582 64 -134 212 -264 361
                      -314 l60 -20 -30 22 c-210 152 -229 387 -48 588 25 27 48 50 51 50 4 0 7 -27
                      7 -61 0 -57 2 -62 37 -95 30 -27 46 -34 78 -34 56 0 99 24 116 65 29 69 16
                      120 -50 205 -105 134 -117 233 -43 347 l31 48 7 -47 c13 -82 58 -129 250 -258
                      209 -141 306 -261 328 -405 11 -72 -1 -161 -31 -218 -27 -53 -112 -143 -165
                      -174 -24 -14 -43 -26 -43 -28 0 -2 24 4 53 14 241 83 427 271 482 486 19 76
                      19 202 -1 285 -35 152 -146 305 -299 412 l-70 49 -6 -33 c-8 -48 -26 -76 -59
                      -93 -45 -23 -103 -19 -138 10 -67 57 -78 146 -37 305 30 116 32 206 5 291 -27
                      89 -104 206 -162 247 -17 13 -18 12 -11 -15z"/>
                </g>
                </svg>
            </div>

            <div class="guide">
                <p>This example demonstrates one way to utilize a custom class
                    (a.k.a. library) in a CodeIgniter version 4.0-dev controller.</p>
                <p>The following message was provided by the custom class:
                    <span class="msg"><?= $message; ?></span></p>

                <p>The custom class used is found at:</p>
                <pre><code>application/Libraries/Test.php</code></pre>
                
                <p>If you would like to edit the view file you'll find it located at:</p>
                <pre><code>application/Views/test_message.php</code></pre>

                <p>The controller file for this page is found at:</p>
                <pre><code>application/Controllers/Base.php</code></pre>

            </div>

            <div class="footer">
                Page rendered in {elapsed_time} seconds. Environment: <?= ENVIRONMENT ?>
            </div>

        </div>
    </body>
</html>

Direct your browser to http://yoursite.com/base to see if all the pieces fit together and produce the expected output.
Reply


Messages In This Thread
Custom library available like CI3 - by Przem4S - 05-23-2018, 12:46 AM
RE: Custom library available like CI3 - by dave friend - 05-24-2018, 11:47 AM



Theme © iAndrew 2016 - Forum software by © MyBB