php 子目录中的 CodeIgniter 默认控制器不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6529026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:38:57  来源:igfitidea点击:

CodeIgniter default controller in a sub directory not working

phpcodeigniter

提问by Mike

My default_controller in the routes configuration is set as "home.php".

我在路由配置中的 default_controller 设置为“home.php”。

I have a sub directory for my controllers, lets call it "folder". So if I visit http://mysite.com/folder/, the default controller "folder/home.php" should be called right?

我的控制器有一个子目录,我们称之为“文件夹”。因此,如果我访问http://mysite.com/folder/,则应该调用默认控制器“folder/home.php”,对吗?

However for some reason this doesn't work, I get a 404. Visiting http://mysite.com/folder/homeor http://mysite.com/folder/home/indexworks as expected. In addition to this, the default controller works in the root directory (http://mysite.comloads home.php).

但是由于某种原因这不起作用,我得到一个 404。访问http://mysite.com/folder/homehttp://mysite.com/folder/home/index按预期工作。除此之外,默认控制器在根目录中工作(http://mysite.com加载 home.php)。

Any ideas, has anyone else experienced this? I can't get my head around it - it would appear to be a CI issue but I can't find anybody else having the same problem.

任何想法,有没有其他人经历过这个?我无法理解它 - 这似乎是一个 CI 问题,但我找不到其他人有同样的问题。

The documentation, from the way I understand it at least, suggests that this should work fine: http://codeigniter.com/user_guide/general/controllers.html#subfolders

至少从我理解的方式来看,文档表明这应该可以正常工作:http: //codeigniter.com/user_guide/general/controllers.html#subfolders

Setting the default controller to "folder/home.php" means that http://mysite.com/folder/works fine as expected. Except for I want the default controller to just be "home.php" - whether in the root or in a sub directory, home.php within that directory should be loaded, as the documentation suggests.

将默认控制器设置为“folder/home.php”意味着http://mysite.com/folder/可以正常工作。除了我希望默认控制器只是“home.php” - 无论是在根目录中还是在子目录中,都应该加载该目录中的 home.php,正如文档所建议的那样。

Cheers

干杯

回答by Yasser Souri

For each sub-folder in your controllers folder you must specify a default controller in routes.php. The built in $route['default_controller']will not work for sub-folders.

对于控制器文件夹中的每个子文件夹,您必须在routes.php. 内置$route['default_controller']将不适用于子文件夹。

e.g: For setting the default controller for you foldersub-folder to homeadd the following to your /application/config/routes.phpfile:

例如:用于为您的folder子文件夹设置默认控制器以home将以下内容添加到您的/application/config/routes.php文件中:

$route['folder'] = "folder/home";

which means http://mysite.com/folder/is the same as http://mysite.com/folder/homeas URL.

这意味着与 URLhttp://mysite.com/folder/相同http://mysite.com/folder/home

回答by Girish

You can extend system router as per requirements,

您可以根据需要扩展系统路由器,

  1. Create My_Router.php in application/core/directory
  1. application/core/目录中创建 My_Router.php

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

/* * 要更改此许可证标题,请在项目属性中选择许可证标题。* 要更改此模板文件,请选择工具 | 模板 * 并在编辑器中打开模板。*/

/**
 * Description of My_Router
 *
 * @author girish
 */
class My_Router extends CI_Router {

    //put your code here
    public function __construct($routing = NULL) {
        parent::__construct($routing);
    }

    protected function _set_default_controller() {
    if (empty($this->default_controller)) {
        show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
    }

    // Is the method being specified?
    if (sscanf($this->default_controller, '%[^/]/%[^/]/%s', $directory, $class, $method) !== 3) {
        $method = 'index';
    }
    if (is_dir(APPPATH . 'controllers' . DIRECTORY_SEPARATOR . $directory) === true) {

        if (!file_exists(APPPATH . 'controllers' . DIRECTORY_SEPARATOR . $directory . DIRECTORY_SEPARATOR . ucfirst($class) . '.php')) {
            // This will trigger 404 later
            return;
        }
        $this->set_directory($directory);
        $this->set_class($class);
        $this->set_method($method);
    } else {
        if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
            $method = 'index';
        }
        if (!file_exists(APPPATH . 'controllers' . DIRECTORY_SEPARATOR . ucfirst($class) . '.php')) {
            // This will trigger 404 later
            return;
        }
        $this->set_class($class);
        $this->set_method($method);
    }
    // Assign routed segments, index starting from 1
    $this->uri->rsegments = array(
        1 => $class,
        2 => $method
    );

    log_message('debug', 'No URI present. Default controller set.');
   }

}

and overwrite _set_default_controller()from custom method, it will work from sub-directory controller as well root directory controller.

_set_default_controller()从自定义方法覆盖,它将从子目录控制器以及根目录控制器工作。

And in application/config/routes.php

而在 application/config/routes.php

if you need sub-directory default controller, then

如果你需要子目录默认控制器,那么

 $route['default_controller'] = "admin/admins/login";
  • admin -- folder
  • admins -- controller
  • login -- method
  • 管理员——文件夹
  • 管理员——控制器
  • 登录——方法

if you need root-directory default controller, then

如果你需要根目录默认控制器,那么

 $route['default_controller'] = "welcome/index";
  • welcome -- controller
  • index -- method
  • 欢迎——控制器
  • 索引——方法

not sure it will work in all versions, but tested in CI3.0.6

不确定它是否适用于所有版本,但已在 CI3.0.6 中测试

回答by movAX13h

If you want to stay flexible you need to pass on everything after the starting folder (in application/config/config.php):

如果您想保持灵活性,您需要在起始文件夹(在application/config/config.php)之后传递所有内容:

$route['home'] = "home/whatever";
$route['home/(:any)'] = "home/whatever/";

回答by Galaxy Patel

Add this line in application/config/routes.php

在 application/config/routes.php 中添加这一行

$this->set_directory( "yourfoldername" );
$route['default_controller'] = 'controller name';

回答by Vamsi Krishna B

Default route is used to tell CI , which controller class should be loaded if the URI contains no data.

默认路由用于告诉 CI,如果 URI 不包含数据,应该加载哪个控制器类。

enter image description here

在此处输入图片说明

$route['default_controller'] = "unicorn/best";

So, when I load

所以,当我加载

http://example.com/index.php/unicorn/

the best controller will be loaded.

最好的控制器将被加载。

also when I load

也当我加载

http://example.com/

or

或者

http://example.com/index.php/horse/

the best controller will be loaded.

最好的控制器将被加载。

回答by VU Learning Tips

In application/config/routes.php just add this

在 application/config/routes.php 中添加这个

$this->set_directory( "user" );
$route['default_controller'] = 'home/index';

Here, user is my folder name. Then in default controller you can call any controller that is in user folder following by function name

在这里,用户是我的文件夹名称。然后在默认控制器中,您可以按函数名称调用用户文件夹中的任何控制器

回答by sopno pori

If i use the following code

如果我使用以下代码

$this->set_directory( "user" );
$route['default_controller'] = 'home/index';

then another route in another sub directory does not work.

然后另一个子目录中的另一个路由不起作用。

Suppose i am using sub-directory user in controller directory for default controller. but if i like to use FrontEnd sub-directory for another route,

假设我在默认控制器的控制器目录中使用子目录用户。但如果我喜欢将 FrontEnd 子目录用于另一条路线,

$this->set_directory( "FrontEnd" );
$route['product/(:any)'] = 'product/';

then it is not working.

那么它不起作用。

回答by Jauhar xlr

MY FOLDER STRUCTURE

我的文件夹结构

--controllers
  --backend
  --frontend
    --home.php
    --products.php
    --productDetail.php
--homeIndex.php

In config/routes.php

在 config/routes.php

$route['default_controller'] = 'homeIndex';
$route['frontend'] = 'frontend/home';
$route['backend'] = 'backend/home';

In controllers/homeIndex.php

在控制器/homeIndex.php

<?php    
defined('BASEPATH') OR exit('No direct script access allowed');
require_once(APPPATH.'controllers/frontend/Home.php');    
class homeIndex extends home {    
    public function index() {
        $this->action();
    }    
}

by default homeIndex will be loaded and from homeIndex i call to frontend/home/action function.

默认情况下,将加载 homeIndex 并从 homeIndex 调用前端/主页/操作函数。