CodeIgniter Forums
How to use public variables from controller in views in CI 4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: How to use public variables from controller in views in CI 4 (/showthread.php?tid=78288)

Pages: 1 2


How to use public variables from controller in views in CI 4 - ciddict - 12-29-2020

Here is my controller:

PHP Code:
namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
MY_Controller extends Controller {

    public $public_variable;

    public function __construct() {
        $this->public_variable "Some value";
    }

    function index() {
        return view("my_folder/index");
    }




I can't call it from the view file:
in views/my_folder/index.php

PHP Code:
<?php
echo $this->public_variable;
?>


Showing this error:

Code:
ErrorException

Undefined property: CodeIgniter\View\View::$public_variable


But in CI3, I could easily use it. What's wrong here?
I've to call the same thing everywhere mostly. So, I shouldn't pass the variable to view(). Isn't it?

Please provide any solution.
Thanks in advance.



RE: How to use public variables from controller in views in CI 4 - includebeer - 12-29-2020

Pass the variable to the view. There’s no more global object in CI4 like there was in CI3.


RE: How to use public variables from controller in views in CI 4 - John_Betong - 12-29-2020

@ciddict,
Other untried methods would be to define a constant or to set a session variable.

Edit:
Tried and both options work OK.


RE: How to use public variables from controller in views in CI 4 - MIS - 12-29-2020

PHP Code:
$view = \Config\Services::renderer();
$data = [
        'blog_title'  => 'My Blog Title',
        'blog_heading' => 'My Blog Heading'
];

echo 
$view ->setData($data)
            ->render('blog_template'); 

https://codeigniter.com/user_guide/outgoing/view_parser.html?highlight=setdata#setData
https://codeigniter.com/user_guide/outgoing/view_parser.html?highlight=setdata


RE: How to use public variables from controller in views in CI 4 - adnzaki - 12-29-2020

There is no way to call any variables outside of view() method. You have to pass them into view() so it can reads your data.
If you have a property in your class, you have to pass it to data array in order to make it callable in view. Consider writing this code:
PHP Code:
<?php namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
MyController extends Controller
{
    public $globalVariable 'I am callable';

    public function index()
    {
        $data['global'] = $this->globalVariable;

        return view('my_view'$data);
    }

In your view, you can call it like any other variable:
PHP Code:
<div>
    <p><?= $global ?></p>
</div> 



RE: How to use public variables from controller in views in CI 4 - ciddict - 12-29-2020

Thanks for the answers. But this gonna be a mess for me. 
Could you guys anyone please provide me any special trick to perform this?


RE: How to use public variables from controller in views in CI 4 - InsiteFX - 12-30-2020

You can use a library and the viewCells


RE: How to use public variables from controller in views in CI 4 - ciddict - 12-30-2020

(12-30-2020, 12:57 AM)InsiteFX Wrote: You can use a library and the viewCells

Even with this, I'll have to modify all the existing usages... Sad


RE: How to use public variables from controller in views in CI 4 - John_Betong - 12-30-2020

@ciddict,

Did you try the two suggestions I made in post #3? I did, both work and either one could solve your problems.

Edit:

I just re-checked the original post and also advise to extend base_controller instead of each controller:

> class MY_Controller extends Base_Controller {


RE: How to use public variables from controller in views in CI 4 - includebeer - 12-30-2020

Like John_Betong suggested, extend the base controller. What I like to do is something like this:
PHP Code:
class BaseController extends Controller
{
    protected $data = [];

    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        parent::initController($request$response$logger);

        $this->data['some_data'] = "Something I need for all controllers";
        $this->data['other_data'] = "Some other things..."

Then in my controllers I add some other data specific to that controller/method:
PHP Code:
class SomeController extends BaseController
{
    public function index()
    {
        $this->data['another_thing'] = "Something I need just for this method";
        return view('index'$this->data);
    }