John_Betong
22:36:42

EXTEND CONTROLLER MY_LIB


Simple when you know how :)

I had to search because CI4 is different from previous versions.

file: ./application/Libraries/MY_lib.php
<php

declare(strict_types=1);

namespace App\Libraries;

use CodeIgniter\Controller;


//=====================================================================
class MY_Lib extends Controller 
{


//=====================================================================
function crunch($tmp, $ff=NULL)
{
  if($ff):
    $tmp = file_get_contents($tmp);
  endif;
    
  return str_replace(["\n", "\t", '   ','  '], ' ',  $tmp);
}//


}///  


Usage:

file: ./applicaiton/Controllers/Tests.php

Notice:
  • 1. class Tests extends MY_Lib
  • 2. 'logo' => $this->crunch(FCPATH .'assets/img/ci-logo.svg', true),
<php 
declare(strict_types=1);

namespace App\Controllers;

use App\Libraries\MY_Lib;

//===================================================================
class Tests extends MY_Lib 
{

//====================================================================
public function test( $test='home', $tNo=NULL)
{
  # DATA TO BE PASSED TO VIEW  
    $data =
    [
      'title'      => 'CI4 Playground by John_Betong', // ucfirst($test); // Capitalize the first letter
      'test'       => $test,
      'tNo'        => 5, // mt_rand(1,42);
      'menu'       => view('incs/menu', [], [false]),
      'adsense'    => LOCALHOST ? NULL : view('adsense/adsense-http', [], [false]),
      'ci4_logo'   => $this->crunch(FCPATH .'assets/img/ci-logo.svg', true),
      'bg-img'     => $this->crunch(FCPATH .'assets/img/bg-img.svg', true),
      'jb_gif'     => file_get_contents(FCPATH .'assets/img/jb_in_shorts.gif'),
      'style_ci4'  => $this->crunch(FCPATH .'assets/css/style_tla.css', true),
      'style_tla'  => $this->crunch(FCPATH .'assets/css/style_tla.css', true),
     ]; 
     $tmp  = $this->getData();
     $data = array_merge($data, $tmp);

  echo view('incs/_header', $data);
    echo view('tests/v_test', $data);
  echo view('incs/_footer', $data);  

}//--------------------------------------------------------------------

}///


 

Wonderful place for a footer Environment: production Page rendered in 0.0047 seconds.