CodeIgniter Forums
how to data in basecontroller.php pass to footer.php view codeigniter 4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: how to data in basecontroller.php pass to footer.php view codeigniter 4 (/showthread.php?tid=82638)



how to data in basecontroller.php pass to footer.php view codeigniter 4 - startup - 08-03-2022

1. BaseController.php
PHP Code:
<?php
namespace App\Controllers;
use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\CLIRequest;
use 
CodeIgniter\HTTP\IncomingRequest;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Psr\Log\LoggerInterface;
use 
App\Models\HeaderModel;

class 
BaseController extends Controller{
  
    
protected $data=array();
    protected $session;
    protected $option ='';
    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger){
        parent::initController($request$response$logger);

        $this->db = \Config\Database::connect();
        $this->session = \Config\Services::session();

        $mSite= new HeaderModel();
        $this->option $mSite->site_option_bottom();
        $this->data['site_name']        $this->option->site_name;
    }

    


2.footer.php
PHP Code:
<?php echo $this->site_name?>

NOTE FOR SOMEONE NEED

i only write demo, if you do your project, you can write  method function to show

1. in BaseController.php 
PHP Code:
<?php
namespace App\Controllers;
use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\CLIRequest;
use 
CodeIgniter\HTTP\IncomingRequest;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Psr\Log\LoggerInterface;
use 
App\Models\HeaderModel;

class 
BaseController extends Controller{
  
    
protected $data;
    protected $session;

    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger){
        parent::initController($request$response$logger);

        $this->data = \Config\Database::connect();
        $this->session = \Config\Services::session();

        $mSite = new HeaderModel();
        $data['test']= $mSite->site_option_bottom();
     //path view footer.php in your project
        return view('Site\footer',$data);
    }

    


in footer.php we show this

PHP Code:
<?php

print_r
($test); 
?>



RE: how to data in basecontroller.php pass to footer.php view codeigniter 4 - ozornick - 08-03-2022

PHP Code:
return view(”template", $this->data);
// Send param in controller view 



RE: how to data in basecontroller.php pass to footer.php view codeigniter 4 - startup - 08-03-2022

(08-03-2022, 11:04 PM)ozornick Wrote:
PHP Code:
return view(”template", $this->data);
// Send param in controller view 

#ozornick ,no, I mean without controller but still showing data in the footer.php, in codeigniter 3 can do this, ci4 cant do this


RE: how to data in basecontroller.php pass to footer.php view codeigniter 4 - ozornick - 08-03-2022

In what situation is the template called without a controller?
If this is some private information output, it must be passed to the method.
Do you have a working use case?


RE: how to data in basecontroller.php pass to footer.php view codeigniter 4 - startup - 08-03-2022

(08-03-2022, 11:22 PM)ozornick Wrote: In what situation is the template called without a controller?
If this is some private information output, it must be passed to the method.
Do you have a working use case?

in codeigniter 3, i only write code in  my_controller

view footer.php do like


$demo =$this->option ->myfunction();

echo $demo->site_name


RE: how to data in basecontroller.php pass to footer.php view codeigniter 4 - ozornick - 08-03-2022

Hmm. In CI 3 $this is "more global" as I recall.
For your last example, the function should return something (object).


PHP Code:
// in my_function(), example
return $this


I can’t guess anymore, expect others or try it yourself


RE: how to data in basecontroller.php pass to footer.php view codeigniter 4 - startup - 08-04-2022

(08-03-2022, 11:54 PM)ozornick Wrote: Hmm. In CI 3 $this is "more global" as I recall.
For your last example, the function should return something (object).


PHP Code:
// in my_function(), example
return $this


I can’t guess anymore, expect others or try it yourself


thank you, too difficul in codeigniter 4, so simply, write in controller
stop here



RE: how to data in basecontroller.php pass to footer.php view codeigniter 4 - kenjis - 08-04-2022

See https://codeigniter4.github.io/CodeIgniter4/outgoing/views.html#adding-dynamic-data-to-the-view

PHP Code:
return view('template', ['site_name' => $this->option->site_name]); 

PHP Code:
<?php echo esc($site_name); ?>



RE: how to data in basecontroller.php pass to footer.php view codeigniter 4 - startup - 08-04-2022

(08-04-2022, 12:52 AM)kenjis Wrote: See https://codeigniter4.github.io/CodeIgniter4/outgoing/views.html#adding-dynamic-data-to-the-view

PHP Code:
return view('template', ['site_name' => $this->option->site_name]); 

PHP Code:
<?php echo esc($site_name); ?>

thank you