Welcome Guest, Not a member yet? Register   Sign In
viewDirectory in config Paths
#1

Hi.
In my CI projects, I created an event to detect which theme to show and then update the config('Paths')->viewDirectory variable
PHP Code:
Events::on('pre_system', static function ()
{
 
$configPath config(Paths::class);
 
$configTema config(AppTema::class);

 
// tema passato in GET (mi accerto esista il tema)
 
if ($tmp = \Config\Services::request()->getGet('tema'))
 {
 if (
is_dir($configPath->viewDirectory .'/'$configTema::admin_tpl .'/'$tmp)) session()->set('tema_admin'$tmp);
 if (
is_dir($configPath->viewDirectory .'/'$configTema::frontend_tpl .'/'$tmp)) session()->set('tema_frontend'$tmp);
 }

 
// imposto l'eventuale tema memorizzato in sessione
 
if (session()->has('tema_admin')) $configTema->admin_tema session()->get('tema_admin');
 if (
session()->has('tema_frontend')) $configTema->frontend_tema session()->get('tema_frontend');
 
 
define('TEMA_ADMIN'$configTema::admin_tpl .'/'$configTema->admin_tema); // utile per ricerca moduli o estensione template
 
define('TEMA_FRONTEND'$configTema::frontend_tpl .'/'$configTema->frontend_tema); // utile per ricerca moduli o estensione template
 
 
$configTema->asset_admin site_url($configTema::admin_asset .'/'$configTema->admin_tema);
 
$configTema->asset_frontend site_url($configTema::frontend_asset .'/'$configTema->frontend_tema);

 
// capisco se mostrare il template di admin o di frontend
 
if (url_is(SEGMENTURL_ADMIN.'*') || url_is('login*'))
 {
 
$configPath->viewDirectory .= '/'TEMA_ADMIN .'/';
 
$configTema->asset site_url($configTema::admin_asset .'/'$configTema->admin_tema);
 }
 else
 {
 
$configPath->viewDirectory .= '/'TEMA_FRONTEND .'/';
 
$configTema->asset site_url($configTema::frontend_asset .'/'$configTema->frontend_tema);
 }
}); 


I upgraded one of my projects to CI version v4.3.7 and this code stopped working.
I debugged and noticed that in the \codeigniter\system\Config\Services.php file, line 483 was replaced with 
PHP Code:
$viewPath $viewPath ?: (new Paths())->viewDirectory
(old version $viewPath = $viewPath ?: config('Paths')->viewDirectory)
This change effectively undoes my work in Events because Path is reinitialized.

How can I solve this problem?
Thanks
Reply
#2

Try to extend the view service.

Create your own view service class that extends the built-in view service and overide the method where the view path is determined to use the modified 'viewDirectory'

Use your custom view service throughout your application.
Shortcode Κατασκευή Eshop + Δημιουργία Ιστοσελίδων 
Athens Greece 
Reply
#3

The current Config is too liberal and also degrades the performance of CI4.
I recommend all devs not change the Config values at runtime.
Then, you will be able to use Config caching in the future to get performance gain.

In this case, you can pass $viewPath to Services::renderer().
See https://github.com/codeigniter4/CodeIgni...#L477-L487

Or you could create your own Services::renderer() with the code in the previous version.
Reply
#4

Thank you!
I deleted the function in "pre_system" and declared the constant "APPTHEME" in app\Config\Constants.php.
Then I rewrote all view() calls to view( APPTHEME .'template.php') in all Controllers.

I just have to understand how to manage the possible GET passage of the theme to be displayed temporarily.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB