Modules and Views |
CodeIgniter 4.1.1 PHP7.4.4
Cannot get module views to work: http://localhost/Airports/moduleTest results in: viewException: Invalid file: Bobk\airports\views\moduleTest.php This is the setup. I've created my namespace called 'Bobk' in app/Config/Autoload.php Code: // app/Config/Autoload.php In folder ROOTPATH/Bobk I have a module folder called 'airports' airports/controllers/Airports.php airports/models/Airport_model.php (not used to show problem) airports/views/moduleTest.php My airports/controllers/Airports.php file: Code: <?php namespace Bobk\Airports\controllers; // app\Config\routes.php Code: /* // ROOTPATH/Bobk/airports/views/moduleTest.php Code: <html> THE PROBLEM // system/View/View.php LINE:195 Code: if (! is_file($this->renderVars['file'])) This call to locateFile() has the $folder argument set to the literal string 'Views' This results in the resolved view path being: ROUTEPATH\Bobk\/Views/airports/views/moduleTest.php" (I've used ROUTEPATH to avoid having to type the complete path) Notice that this call to locateFile has placed </Views/> (the $folder argument) in between Bobk\ and airports. This is obviously an incorrect path and results in ViewException: Invalid file: Bobk\airports\views\moduleTest.php Note: locateFile() returns false if it can't locate the view file, NOT the offending path which is probably why the invalid file displayed on screen doesn't match. WHAT APPEARS TO WORK (but for how long ?) Edit system/View/View.php LINE:197 and replace 'Views' argument with an empty string '' Code: 197 $this->renderVars['file'] = $this->loader->locateFile($this->renderVars['view'], '', empty($fileExt) ? 'php' : $fileExt); Not advocating this as a real fix but haven't yet managed to find out why the 'Views' argument is there and what it is meant to achieve. Testing this modification with modules and with normal app controllers and views works. Must be a reason for 'Views' argument and I look forward to finding out from those wiser (more informed) than me.
After all that I seem to have found the solution.
Code: // Airports Controller Previously had it set to: echo view('Bobk\airports\views\moduleTest', $data); This is regardless of how the folder names are actually written in the file system. Mine are all lower case (windows) after the namespace prefix Bobk/airports/controllers Bobk/airports/models Bobk/airports/views This is where 'Views' must be capitalized first letter system/AutoLoader/FileLocator::locateFile Code: // If we have a folder name, then the calling function If $folder is set to 'views' (lowercase first letter) strpos() returns false and "Views/" is added to $path resulting in a $path which is incorrect. If $folder is set to 'Views' (capital first letter) strpos() returns a positive number (position) and the If block is NOT executed so $path is not modified and subsequently the correct full pathname is returned.
When calling views in modules you need to pass the full namespace to the view.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Thanks InsiteFX for replying although I'm not sure what contribution you are trying to make.
Is this just a generic comment not directly related to my post Where in my code do I not use a fully qualified namespace when I call the view function ? As it turned out, the main thing to surface from this was that you MUST use a capital 'V' in the word 'Views' in the FQ namespace when calling a module view. Regardless of how you have spelt it in the filesystem. Using a lowercase 'v' will result in a ViewException. Try it. |
Welcome Guest, Not a member yet? Register Sign In |