【问题标题】:CodeIgniter 4 - Views within ModulesCodeIgniter 4 - 模块内的视图
【发布时间】:2021-06-16 07:39:56
【问题描述】:

我有一个 CodeIgniter 4 网站,几乎和我想要的一样,但我无法从我的 Modules 文件夹中的视图文件夹加载视图。

我正在尝试加载位于 app/Modules/Admin/Views/dashboard.php 中的视图。

我正在这样做,使用位于 app/Modules/Admin/Controllers/Dashboard 的以下控制器

<?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);
    }

}

还值得注意的是,我正在使用模板函数,可以在位于 app/Modules/Admin/Controllers/BaseController.php 的 BaseController 文件中找到该函数

<?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);
    } 

}

我的 app/Config/Autoload.php 文件如下所示:

<?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' ,
        ];

        /**
         * -------------------------------------------------------------------
         * 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);
    }

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

}

顺便说一句,如果我将仪表板控制器功能更改为:

return $this->template('admin/dashboard', $data);

而不是

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

提前感谢任何可以解决此问题的人。

【问题讨论】:

  • 我试图包含我的文件结构,但它让我超出了字符限制。

标签: codeigniter-4


【解决方案1】:

如果我不理解,我相信您需要加载确切的模块。目前您只加载模块而不是子文件夹调用 admin。

我也看不到你的模块是否在你的应用文件夹中。如果它们在您的应用文件夹中,请将 ROOTPATH 更改为 APPPATH

在您的 app/Config/Autoload.php 中将以下内容添加到 $psr4 数组中

'Modules'               => ROOTPATH . 'modules', // The modules section    
'Modules/Admin'         => ROOTPATH . 'modules/admin', // The Admin Module

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多