Welcome Guest, Not a member yet? Register   Sign In
How to use public variables from controller in views in CI 4
#1

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.
Reply
#2

Pass the variable to the view. There’s no more global object in CI4 like there was in CI3.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(This post was last modified: 12-29-2020, 09:13 PM by John_Betong.)

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

Edit:
Tried and both options work OK.
Reply
#4

(This post was last modified: 12-29-2020, 07:33 PM by MIS.)

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/outgo...ta#setData
https://codeigniter.com/user_guide/outgo...ht=setdata
Reply
#5

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> 
Reply
#6
Sad 

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?
Reply
#7

You can use a library and the viewCells
What did you Try? What did you Get? What did you Expect?

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

(This post was last modified: 12-30-2020, 02:31 AM by ciddict.)

(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
Reply
#9

(This post was last modified: 12-30-2020, 06:33 AM by John_Betong.)

@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 {
Reply
#10

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);
    } 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB