Welcome Guest, Not a member yet? Register   Sign In
Views Within Modules
#1
Question 
(This post was last modified: 03-18-2021, 12:44 PM by rmartin93.)

Hi,

So I've got a site 99% of the way setup, but I can't seem to get views to work within Modules. 

I have a project where the view's path is: 

poineer/app/Modules/Admin/Views/dashboard.php

My controller, located at 

poineer/app/Modules/Admin/Controllers/Dashboard.php

looks like this: 

PHP Code:
<?php namespace App\Modules\Admin\Controllers;

use 
App\Modules\Admin\Models\UserModel;
use 
CodeIgniter\Controller;

class 
Dashboard extends BaseController
{
    private $userModel;

    /**
     * Constructor.
     */
    public function __construct()
    {
        $this->userModel = new UserModel();
    }

    public function index()
    {
        
$data = [
        
    'title' => 'Pioneer Dashboard',
            // 'view' => 'admin/dashboard',
            'data' => $this->userModel->getUsers(),
        ];

        return 
$this->template('Admin/Views/dashboard'$data);
    }



You'll notice I'm loading the view with a template function, and that can be found in my BaseController, located here

poineer/app/Modules/Admin/Controllers/BaseController.php


PHP Code:
<?php
namespace App\Modules\Admin\Controllers;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *     class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 *
 * @package CodeIgniter
 */

use CodeIgniter\Controller;

class 
BaseController extends Controller
{

    
/**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var array
     */
    
protected $helpers = [];

    
/**
     * Constructor.
     */
    
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        
// Do Not Edit This Line
        
parent::initController($request$response$logger);

        
//--------------------------------------------------------------------
        // Preload any models, libraries, etc, here.
        //--------------------------------------------------------------------
        // E.g.:
        // $this->session = \Config\Services::session();
    
}

        public function 
template(string $page, array $data)
    {
        echo view('template/header'$data);
        echo 
view('template/nav'$data);
        echo view($page$data);
        echo view('template/footer'$data);
    



This all works fine if I change my Dashboard controller to reference the default Views folder like this, 'admin/dashboard', but as soon as I try to reference a view in a different views folder (the way you see my trying to do it above), it breaks. 

Here is my pioneer/app/Config/Autoload.php


PHP Code:
<?php namespace Config;

require_once 
SYSTEMPATH 'Config/AutoloadConfig.php';

/**
 * -------------------------------------------------------------------
 * AUTO-LOADER
 * -------------------------------------------------------------------
 * This file defines the namespaces and class maps so the Autoloader
 * can find the files as needed.
 */
class Autoload extends \CodeIgniter\Config\AutoloadConfig
{
    public 
$psr4 = [];

    public 
$classmap = [];

    
//--------------------------------------------------------------------

    /**
     * Collects the application-specific autoload settings and merges
     * them with the framework's required settings.
     *
     * NOTE: If you use an identical key in $psr4 or $classmap, then
     * the values in this file will overwrite the framework's values.
     */
    
public function __construct()
    {
        
parent::__construct();

        
/**
         * -------------------------------------------------------------------
         * Namespaces
         * -------------------------------------------------------------------
         * This maps the locations of any namespaces in your application
         * to their location on the file system. These are used by the
         * Autoloader to locate files the first time they have been instantiated.
         *
         * The '/app' and '/system' directories are already mapped for
         * you. You may change the name of the 'App' namespace if you wish,
         * but this should be done prior to creating any namespaced classes,
         * else you will need to modify all of those classes for this to work.
         *
         * DO NOT change the name of the CodeIgniter namespace or your application
         * WILL break. *
         * Prototype:
         *
         *   $Config['psr4'] = [
         *       'CodeIgniter' => SYSPATH
         *   `];
         */
        
$psr4 = [
            
'App'           => APPPATH,                    // To ensure filters, etc still found,
            
APP_NAMESPACE   => APPPATH,                    // For custom namespace
            
'Config'        => APPPATH 'Config',
            
'Modules'       => APPPATH 'Modules\Admin' ,
        ];

        
/**
         * -------------------------------------------------------------------
         * Class Map
         * -------------------------------------------------------------------
         * The class map provides a map of class names and their exact
         * location on the drive. Classes loaded in this manner will have
         * slightly faster performance because they will not have to be
         * searched for within one or more directories as they would if they
         * were being autoloaded through a namespace.
         *
         * Prototype:
         *
         *   $Config['classmap'] = [
         *       'MyClass'   => '/path/to/class/file.php'
         *   ];
         */
        
$classmap = [];

        
//--------------------------------------------------------------------
        // Do Not Edit Below This Line
        //--------------------------------------------------------------------

        
$this->psr4     array_merge($this->psr4$psr4);
        
$this->classmap array_merge($this->classmap$classmap);

        unset(
$psr4$classmap);
    }

    
//--------------------------------------------------------------------



I hope I've included everything you guys would need to help me. 

I also included a screenshot of my file structure in case that helps. 

Let me know if you need anything else. 
Reply
#2

(This post was last modified: 03-18-2021, 06:06 PM by iRedds.)

1. It is not clear why you changed the startup. This is not critical, but some kind of garbage.
PHP Code:
require_once SYSTEMPATH 'Config/AutoloadConfig.php'

2.
PHP Code:
'Modules'       => APPPATH 'Modules\Admin' 

The Modules namespace should be found in the APPPATH . 'Modules\Admin' directory.
Although you register a namespace, you don't use it.
Instead, you use the application namespace App\Modules\Admin\Controllers
And this only works for you because the directory with the module is in the app directory.

The view takes either a path to a relative default template directory (changed in the Path config) or a namespace.

PHP Code:
view('aaa/bbb'// <-- app/Views/aaa/bbb.php
view('Namespace\To\view'// <-- NamespaceLocation/Namespace/To/view.php

// and 
return $this->template('Admin/Views/dashboard'$data); // <-- app/Views/Admin/Views/dashboard.php 
Reply
#3

Okay, what I was working with was a framework someone said they had working on GitHub so I started over with a new project.

I followed the instructions here to create a module: https://codeigniter.com/user_guide/general/modules.html

The only difference with my project is I have a users module instead of "acme" and I don't have something like "Blog" underneath my users module, it just goes straight into the controllers, models, views, etc.

The documentation says the structure of the module should be nearly identical to that of the app folder, which is fine, but it doesn't specify how to get those files created. Do I just copy them from the app folder and away I go?

I've tried that, but it doesn't quite work.

I'm just trying to get modules set up with their own controllers and view folders and nothing seems to work.
Reply
#4

Got it! It looks like you can copy everything from the app folder except for Config.

From what I can tell, all I need in the config folder for my module is a Routes.php file, which I made to look like this...


PHP Code:
<?php

if(!isset($routes))

    $routes = \Config\Services::routes(true);
}

$routes->group('users', ['namespace' => 'Users\Controllers'], function($subroutes){

    $subroutes->add('/''Home::index');

}); 

The only other thing after that that tripped me up was how to call the view. It wasn't working at first, but as soon as I changed my "/" to "\" it started working...

PHP Code:
return view('Users\Views\userHome'); 

I still don't know why the slashes have to be like that instead of the normal way but hey, it works! 

While I'm here, is there anything else critical I need to include in each module's config file or is it that simple? 

Thanks again.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB