(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') ?>