'codeigniter4 - How do I shorten the path to the view folder for modules?

Based on a video with Codeigniter4, I created the Modules folder on the ROOTPATH and the Controllers, Views..etc folders in the Modules folder. It works fine, but when I want to call my view file inside the module

<?php 
namespace Modules\Giris\Controllers;
use App\Controllers\BaseController;
class IndexController extends BaseController
{
    public function index(){
        return view('Modules\Giris\Views\index');
    }
}

I need to specify a very long path like How can I make it just like view('index') and call the file from the Views folder in that module if I write it in a module? I don't want to write "Modules\Login\Views" in short is this possible?

Thanks in advance for all the kind replies.



Solution 1:[1]

Because view requires a string you couldn't provide a namespaced reference as you might think you should. Furthermore the code adds a .php extension and the "view path" (defined in your config\paths file) as part of its process (see system/View/View.php and render()). Therefore without modifying Codeigniter (which could be done but would affect all your code) the easiest way is to simply declare a public property or constant and make reference to that instead. Also helps if you need to change the path at any point.

I.e. protected $path = 'Modules\Giris\Views\' and then view($this->path.'index'); is probably the easiest way.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Antony