Welcome Guest, Not a member yet? Register   Sign In
How to use public variables from controller in views in CI 4
#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


Messages In This Thread
RE: How to use public variables from controller in views in CI 4 - by adnzaki - 12-29-2020, 08:56 PM



Theme © iAndrew 2016 - Forum software by © MyBB