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


Messages In This Thread
RE: How to use public variables from controller in views in CI 4 - by includebeer - 12-30-2020, 07:01 AM



Theme © iAndrew 2016 - Forum software by © MyBB