-
nicojmb
Junior Member
-
Posts: 38
Threads: 7
Joined: Feb 2015
Reputation:
2
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!
-
nicojmb
Junior Member
-
Posts: 38
Threads: 7
Joined: Feb 2015
Reputation:
2
(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 $request, ResponseInterface $response, LoggerInterface $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(); ?>
-
5flex
Junior Member
-
Posts: 30
Threads: 11
Joined: Sep 2019
Reputation:
2
04-20-2020, 06:55 AM
(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!
-
nicojmb
Junior Member
-
Posts: 38
Threads: 7
Joined: Feb 2015
Reputation:
2
(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 $request, ResponseInterface $response, LoggerInterface $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(); ?>
-
5flex
Junior Member
-
Posts: 30
Threads: 11
Joined: Sep 2019
Reputation:
2
-
nicojmb
Junior Member
-
Posts: 38
Threads: 7
Joined: Feb 2015
Reputation:
2
(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 $request, ResponseInterface $response, LoggerInterface $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 $request, ResponseInterface $response, LoggerInterface $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') ?>
-
nicojmb
Junior Member
-
Posts: 38
Threads: 7
Joined: Feb 2015
Reputation:
2
(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.
|