Welcome Guest, Not a member yet? Register   Sign In
Call BaseController custom function in view
#1

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

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

(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(); ?>
Reply
#4

(This post was last modified: 04-20-2020, 06:59 AM by 5flex.)

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!
Reply
#5

(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(); ?>
Reply
#6

(This post was last modified: 04-20-2020, 07:44 AM by 5flex.)

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/outgo...cells.html
Reply
#7

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

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

(This post was last modified: 04-20-2020, 09:24 AM by kilishan.)

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

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB