无法从URL调用类函数

时间:2013-06-02 22:04:43

标签: php codeigniter .htaccess url-rewriting function-calls

我是CodeIgniter的新手,我在默认类中调用函数时遇到了麻烦。

控制器

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

class Main extends CI_Controller 
{

public function index()
    {
    $this->welcome();
    }

public function welcome()
    {
    $this->load->view('view_welcome');      
    }
}

htaccess文件

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /pickme/

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php

目前它默认调用此主控制器,正如我所期望的那样。

例如

localhost/sitename
AND
localhost/sitename/index.php
AND
localhost/sitename/index.php/main/index
(All have the same result)

所以我的印象是

localhost/sitename/class/function/id

将是htaccess如何工作但我在尝试访问

时遇到404错误
localhost/sitename/main/welcome

1 个答案:

答案 0 :(得分:1)

尝试使用用户手册中建议的重写规则。

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

我不是apace重写模块的专家,但你肯定看错了。您不希望从浏览器访问您的应用程序和系统文件夹。最佳做法实际上是将其保存在您的Web根文件夹之外。

此外,我认为没有理由从索引方法中调用welcome方法,只需将逻辑保留在索引控制器中即可。

但你的想法是:

hostname/folder/class/function/parameters

非常正确:)

最后但并非最不重要的是,您可能忘记在config.php中留下索引文件,如果要删除index.php,则需要执行此操作。

祝你好运(:

修改

尝试在htaccess文件中使用它:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ ./index.php/$1 [L]
</IfModule>

在你的config.php中记得添加一个尾部斜杠。

这可以在这里工作。