Welcome Guest, Not a member yet? Register   Sign In
Making data available in ALL views
#1

I am attempting to make some data available in all view files. This information is displayed in the page masthead and sidebar menu. I know I could pull the data in each and every single controller, but that is tedious and really not efficient. I would like to pull it at some global point and have it available in the view. 
The key is I don't want to have to modify all the controllers to pass the data to the view (or remember to pass in the future). How can I set a variable that is available in a view in say the base controller?
What I have so far (in the base controller):

PHP Code:
class BaseController extends Controller
{

 protected 
$helpers = ["form""auth""phone""text"];

 public 
$siteInfo;

 public function 
initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
 {

 
parent::initController($request$response$logger);

 
$session = \Config\Services::session();

 
helper('siteinfo');
 
$this->siteInfo siteinfo();
 } 
Then in the view I have:
PHP Code:
<?= $siteInfo ?>

I have confirmed that $siteInfo has data before you get to the view, but in the view I get: "Undefined variable $siteInfo"
Feels like I am missing something simple here... help!
Reply
#2

Can you not use
PHP Code:
<?= siteinfo(); ?>
in your views?
Reply
#3

(This post was last modified: 06-15-2021, 12:28 AM by ikesela.)

why don't make it as Helper (put file in helper folder, procedural function) . accessible on all view. wonder why you using so many controller , should be one controller is enough per site ( i mean: frontpage 1 controller, members 1 controller, admin 1 controller )
Reply
#4

In your BaseController just load the helper through the helpers array
Then in your view just do this.

PHP Code:
<?= siteinfo();?>

Make sure that your helper is named siteinfo_helper.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(06-14-2021, 10:02 PM)paulkd Wrote: Can you not use
PHP Code:
<?= siteinfo(); ?>
in your views?

No, when I do I get:
Code:
Undefined variable $siteInfo
Reply
#6

(06-15-2021, 12:25 AM)ikesela Wrote: why don't make it as Helper (put file in helper folder, procedural function) . accessible on all view. wonder why you using so many controller , should be one controller is enough per site ( i mean: frontpage 1 controller, members 1 controller, admin 1 controller )

It is in a helper. My issue is I cannot access it from the view. I get "Undefined variable" when I do. 

This is a very large business management site. Each section of the admin has it's own controller because it is so large. For instance the customer management controller is over 600 lines alone (and I am not done with that section yet - will probably double).
Reply
#7

(06-15-2021, 01:21 AM)InsiteFX Wrote: In your BaseController just load the helper through the helpers array
Then in your view just do this.

PHP Code:
<?= siteinfo();?>

Make sure that your helper is named siteinfo_helper.

Ok, so I changed the view code to what you said and then I removed the code from the Base Controller that calls the helper and moved it up into the array like so:
Code:
protected $helpers = ["form", "auth", "phone", "text","siteinfo"];
When I load the view I am still getting:
Code:
Undefined variable $siteInfo
Reply
#8

(06-14-2021, 10:02 PM)paulkd Wrote: Can you not use
PHP Code:
<?= siteinfo(); ?>
in your views?

I missed that you dropped the "$"... now it works!!!!! Thanks!!
Reply
#9

(06-15-2021, 01:21 AM)InsiteFX Wrote: In your BaseController just load the helper through the helpers array
Then in your view just do this.

PHP Code:
<?= siteinfo();?>

Make sure that your helper is named siteinfo_helper.

I missed that you dropped the "$"... now it works!!!!! Thanks!!
Reply
#10

Thanks everyone for the help. Just needed a push in the right direction. Somewhat new to CI and the community support is great.
Now that it is working/returning data, I wonder if there is a better way to return the data. In my view file I am using:
Code:
<?= siteInfo()['leads']; ?>

In my helper file:
Code:
<?php

if (! function_exists('siteinfo')) {

function siteinfo()
{
$leadModel = new \App\Models\LeadsModel();
$leads = $leadModel ->where('status != "Closed"')
->countAllResults();

$custModel = new \App\Models\CustomersModel();
$custs = $custModel ->where('status_customer != "Closed"')
->countAllResults();

$data = array(
'leads' => $leads,
'customers' => $custs,
);

return $data;
}
}
Any suggestions to make that more efficient or easier to access? Always looking to learn new ways!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB