【问题标题】:Make localhost/ call a controller in CodeIgniter使 localhost/ 在 CodeIgniter 中调用控制器
【发布时间】:2011-10-01 14:18:18
【问题描述】:

我正在使用 codeigniter,并且正在使用 .htaccesslocalhost/index.php/some_class 重写为 localhost/some_class

我怎样才能让localhost/ 自己调用某个控制器?因为 localhost 将是一个类,localhost/some_func 是可能的。

编辑:

这是我当前的 .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ /index.php/$1 [L] 

【问题讨论】:

  • 我认为您应该在 codeigniter 框架中执行此操作,而不是在 .htaccess 文件中执行此操作。 .htaccess 中的基本设置就足够了,其余的您可以在应用程序中配置。

标签: php codeigniter mod-rewrite url-rewriting


【解决方案1】:

是的,您必须定义一个默认控制器,当您调用 localhost/ 时将执行其索引功能

但是,你也可以做你想做的另一件事,直接用那个控制器的方法而不用控制器名称来捕捉

localhost/some_func

你需要在路由中使用通配符,here is how

【讨论】:

    【解决方案2】:

    你想编辑 application/config/routes.php

    $route['default_controller'] = "some_class";
    

    通过改变default_controller,这将调用some_class的索引函数。您将无法使用此方法调用 my_function 属于 some_class 的 localhost/my_function。

    编辑:

    我误读了您的帖子,您想编辑您的 htaccess 文件以将所有内容重定向到 some_class。如果您发布您的 .htaccess,我们可以为您提供帮助。

    类似这样的:

    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|stylesheets|scripts|robots\.txt)
    RewriteRule ^(.*)$ /index.php/some_class/$1 [L]
    

    注意:这种方式违背了 MVC 结构。

    【讨论】:

    • 这些重写规则看起来不太合理。我会建议删除RewriteCond 并只做一个RewriteRule ^$ index.php/some_class [L] 而不是引入不必要的问题。
    • 谢谢,所以你答案的上半部分与我的情况无关,但编辑是?
    • 同意,我只是提供了一个来自谷歌搜索的快速示例。主要只是突出显示 /index.php/some_class/$1 方面。
    • @Eddie,我会把这个控制器放在什么目录下?它会被命名为 index.php 吗?
    • 阅读此内容:codeigniter.com/user_guide/general/controllers.html 在继续之前了解如何创建控制器。
    猜你喜欢
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 2018-01-05
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    相关资源
    最近更新 更多