CodeIgniter Forums
viewDirectory in config Paths - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Installation & Setup (https://forum.codeigniter.com/forumdisplay.php?fid=9)
+--- Thread: viewDirectory in config Paths (/showthread.php?tid=88238)



viewDirectory in config Paths - p.mancini - 08-12-2023

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


RE: viewDirectory in config Paths - shortcode - 08-12-2023

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.


RE: viewDirectory in config Paths - kenjis - 08-13-2023

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/CodeIgniter4/blob/4bc7a02a7153a69fc02bdc3f4d6ba75c36c77b15/system/Config/Services.php#L477-L487

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


RE: viewDirectory in config Paths - p.mancini - 08-16-2023

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.