Search code examples
phpcodeigniter-4

Module view file showing invalid in Codeigniter 4


I've added the module folder in ...\app\Config\Autoload.php

public $psr4 = [
          APP_NAMESPACE => APPPATH, // For custom app namespace
          'Config'      => APPPATH . 'Config',
          'Blog'      => ROOTPATH . 'example/blog',
];

My directory:

/Home directory of main project folder
/example
    /Blog
        /Config
            /Routes.php
        /Controllers            
            /Blog.php
        /Views
           /show_blog.php

Routes.php

namespace Config;

// Create a new instance of our RouteCollection class.
$routes = Services::routes();

$routes->get('blog', 'blog::index', ['namespace' => 'Blog\Controllers']); 

Blog.php

namespace Blog\Controllers;

class Blog extends \CodeIgniter\Controller {

    function index() { 
        echo view('Example\Blog\Views\show_blog');
    }

} 

After running my_domain/index.php/blog, it's showing this error:

CodeIgniter\View\Exceptions\ViewException

Invalid file: Example\Blog\Views\show_blog.php

Didn't get any solution from this one: Codeigniter 4 View file invalid

Where am I wrong?


Solution

  • Changing Blog.php to this, worked for me.

    namespace Blog\Controllers;
    
    class Blog extends \CodeIgniter\Controller {
    
        function index() { 
            echo view('Blog\Views\show_blog');
        }
    
    } 
    

    Credit: CI Forum