CodeIgniter Forums
Call BaseController custom function in view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Call BaseController custom function in view (/thread-76185.html)

Pages: 1 2


Call BaseController custom function in view - nicojmb - 04-20-2020

Hi,

I've created a custom function in BaseController an i need to call inside a view but I can't find how.

I would like to to create a "helper" and inside a view call it, buy i don't know access to BaseController variable in helper file.

Please helpme.

Regards!


RE: Call BaseController custom function in view - 5flex - 04-20-2020

BaseController is a common base for expanding your other controllers. However, it is the same Controller as the rest. Everything that you call in your controllers can be called in it in the same way. If not difficult, can you show your code?


RE: Call BaseController custom function in view - nicojmb - 04-20-2020

(04-20-2020, 06:01 AM)5flex Wrote: BaseController is a common base for expanding your other controllers. However, it is the same Controller as the rest. Everything that you call in your controllers can be called in it in the same way. If not difficult, can you show your code?

Hi, i need to call inside a view a custom function created in BaseController.

BaseController:
PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Psr\Log\LoggerInterface;

class 
BaseController extends Controller
{

    protected 
$helpers = [];

    public function 
initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        
parent::initController($request$response$logger);

    }

    private function 
customFunction()
    {
          return 
'Testing';
    }



View:
PHP Code:
<?php
<?= $this->extend('layout'); ?>

<?= $this->section('content'?>

<div>
    <?= Here call function "customFunction" in BaseController ?>
</div>

<?= $this->endSection(); ?>



RE: Call BaseController custom function in view - 5flex - 04-20-2020

How you can try to call the function if it has PRIVATE modificator?  Change private on public or protected!

And further. You cannot access your BaseController directly. You must create your controller and extend it (through extends) from BaseController!


RE: Call BaseController custom function in view - nicojmb - 04-20-2020

(04-20-2020, 06:55 AM)5flex Wrote: How you can try to call the function if it has PRIVATE modificator?  Change private on public or protected!

It's a typographic error writing this post, the function is public:


PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Psr\Log\LoggerInterface;

class 
BaseController extends Controller
{

    protected 
$helpers = [];

    public function 
initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        
parent::initController($request$response$logger);

    }

    public function 
customFunction()
    {
          return 
'Testing';
    }



In view:

PHP Code:
<?php
<?= $this->extend('layout'); ?>

<?= $this->section('content'?>

<div>
    /* I tried this but do not work */
    <?= $this->customFunction(); ?>
</div>

<?= $this->endSection(); ?>



RE: Call BaseController custom function in view - 5flex - 04-20-2020

For this implementation you must use view_cells(), because $this-> in Views it inctance of Vews but not the Controller.

https://codeigniter.com/user_guide/outgoing/view_cells.html


RE: Call BaseController custom function in view - kilishan - 04-20-2020

Or run the custom function within your controller, assigning the output to a variable which is then passed to the view. That's more of a standard practice.


RE: Call BaseController custom function in view - nicojmb - 04-20-2020

(04-20-2020, 08:37 AM)kilishan Wrote: Or run the custom function within your controller, assigning the output to a variable which is then passed to the view. That's more of a standard practice.

mmm, mi real idea is, in view add assets with "section" function, inside this section make a function to prepend path and append file version.

And

BaseController (frontend):
PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
BaseController extends Controller
{
      protected 
$assets =  [
            
'basePath' => '/assets/frontend/'
      
];

     public function 
initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
     {
          
parent::initController($request$response$logger);
     }

     public function 
assets($type$file)
    { 
          
$assetFile $this->assets['basePath'] . $file;
          
$md5 md5_file($assetFile);

          return 
$assetFile '?' $assetFile;
    }



BaseController (backend):
PHP Code:
<?php

namespace App\Controllers\Backend;

use 
CodeIgniter\Controller;

class 
BaseController extends Controller
{
      protected 
$assets =  [
            
'basePath' => '/assets/backend/'
      
];

     public function 
initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
     {
          
parent::initController($request$response$logger);
     }

     public function 
assets($type$file)
    { 
          
// $type (custom action depend $type file)
          
$assetFile $this->assets['basePath'] . $file;
          
$md5 md5_file($assetFile);

          return 
$assetFile '?' $assetFile;
    }



Layout View:
PHP Code:
<!doctype html>
<
html lang="es">

<
head>
    <
title>Example App</title>
    <
meta charset="utf-8">
    <
meta http-equiv="X-UA-Compatible" content="IE=edge">
    <
meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- 
CSS -->
    <
script src="<?= $this->assets('css', 'main.css');?>"></script>
    <?= 
$this->renderSection('css'?>
</head>

<body>

    <!-- SECTION -->
    <?= $this->renderSection('content'?>

    <!-- JS -->
    <script src="<?= $this->assets('js''main.js');?>"></script>
    <?= $this->renderSection('js'?>
</body>

</html> 

Controller View:
PHP Code:
<?= $this->extend('layout'); ?>

<?= $this->section('content'?>

Main web content

<?= $this->endSection(); ?>

<script src="<?= $this->assets('js''secondary.js');?>"></script>

<?= $this->section('js'?>



RE: Call BaseController custom function in view - kilishan - 04-20-2020

You could make that a helper, and include it in your BaseController, then it's available within all views.

Just a note: The $this context refers to the sytsem's View class, not the controller.


RE: Call BaseController custom function in view - nicojmb - 04-20-2020

(04-20-2020, 09:23 AM)kilishan Wrote: You could make that a helper, and include it in your BaseController, then it's available within all views.

This is my real idea, but how can access $assets variable defined in "BaseController"?

Is there where base folder is configured.