Codeingiter 和 HMVC 在 2 级子文件夹中无法找到控制器

问题描述 投票:0回答:1

如果我将控制器放在 2 级子文件夹“类别”中,将无法找到它

application > modules > catalog > controllers > forum

application > modules > catalog > controllers > forum > categories

// unable to locate
application > modules > catalog > controllers > forum > categories > Category.php

// able to locate
application > modules > catalog > controllers > forum > Category.php 

问题是否可以修改MY_Router.php,以便可以正确找到文件夹而不影响其他任何事情。

我正在使用 codeigniter 3.1.0 和 HMVC

我已经看过这个无法访问子文件夹中的控制器但是启用时它不会显示default_controller。

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/* load the MX_Router class */
require APPPATH."third_party/MX/Router.php";

class MY_Router extends MX_Router {

    /** Locate the controller **/
    public function locate($segments)
    {
        $this->located = 0;
        $ext = $this->config->item('controller_suffix').EXT;

        /* use module route if available */
        if (isset($segments[0]) && $routes = Modules::parse_routes($segments[0], implode('/', $segments)))
        {
            $segments = $routes;
        }

        /* get the segments array elements */
        list($module, $directory, $controller) = array_pad($segments, 3, NULL);

        /* check modules */
        foreach (Modules::$locations as $location => $offset)
        {
            /* module exists? */
            if (is_dir($source = $location.$module.'/controllers/'))
            {
                $this->module = $module;
                $this->directory = $offset.$module.'/controllers/';

                /* module sub-controller exists? */
                if($directory)
                {
                    /* module sub-directory exists? */
                    if(is_dir($source.$directory.'/'))
                    {   
                        $source .= $directory.'/';
                        $this->directory .= $directory.'/';

                        /* module sub-directory controller exists? */
                        if($controller)
                        {
                            if(is_file($source.ucfirst($controller).$ext))
                            {
                                $this->located = 3;
                                return array_slice($segments, 2);
                            }
                            else $this->located = -1;
                        }
                    }
                    else
                    if(is_file($source.ucfirst($directory).$ext))
                    {
                        $this->located = 2;
                        return array_slice($segments, 1);
                    }
                    else $this->located = -1;
                }

                /* module controller exists? */
                if(is_file($source.ucfirst($module).$ext))
                {
                    $this->located = 1;
                    return $segments;
                }
            }
        }

        if( ! empty($this->directory)) return;

        /* application sub-directory controller exists? */
        if($directory)
        {
            if(is_file(APPPATH.'controllers/'.$module.'/'.ucfirst($directory).$ext))
            {
                $this->directory = $module.'/';
                return array_slice($segments, 1);
            }

            /* application sub-sub-directory controller exists? */
            if($controller)
            { 
                if(is_file(APPPATH.'controllers/'.$module.'/'.$directory.'/'.ucfirst($controller).$ext))
                {
                    $this->directory = $module.'/'.$directory.'/';
                    return array_slice($segments, 2);
                }
            }
        }

        /* application controllers sub-directory exists? */
        if (is_dir(APPPATH.'controllers/'.$module.'/'))
        {
            $this->directory = $module.'/';
            return array_slice($segments, 1);
        }

        /* application controller exists? */
        if (is_file(APPPATH.'controllers/'.ucfirst($module).$ext))
        {
            return $segments;
        }

        $this->located = -1;
    }
}
php codeigniter
1个回答
2
投票

也许有可能,但你真的需要这样做吗?

我有点看到使用的感觉...

application > modules > catalog > controllers > forum > Category.php

但我不明白使用的意义...

application > modules > catalog > controllers > forum > categories > Category.php

为了把东西放进自己的小盒子里,这有点太深了。

我个人会将论坛作为一个模块并...

application > modules > forum > controllers > categories > Category.php
© www.soinside.com 2019 - 2024. All rights reserved.