CodeIgniter无法执行基于段的方法

时间:2013-08-04 18:50:36

标签: codeigniter codeigniter-routing

所有文件都在localhost中运行,我正在使用XAMPP,但我可以访问基于段的方法中的内容。

例如http:// localhost / auth / login

在配置中,我有

 <?php
 $config['base_url'] = 'http://localhost/';
 $config['default_controller'] = 'main'; // Default controller to load
 $config['error_controller'] = 'error'; // Controller used for errors (e.g. 404, 500 etc)
 ?>

在控制器中,我有

<?php

class Auth extends Controller {

function index()
{
    // This is the default function (i.e. no function is set in the URL)
}
function login()
{
    echo 'Hello World!';
}
}
?>

所以当我运行它时,它应该回显“hello world”但是,它表示找不到对象!在此服务器上找不到请求的URL。是因为localhost不支持基于段的方法吗?

2 个答案:

答案 0 :(得分:2)

你的档案中没有录音index.php档案  url is http:// localhost/auth/login but it must be http://localhost/project_name/index.php/controller_name

您无需指定base_url只留下

<?php
 $config['base_url'] = '';
$config['default_controller'] = 'main'; // Default controller to load
$config['error_controller'] = 'error'; // Controller used for errors (e.g. 404, 500 etc)
 ?>

现在打开http://localhost/project_name,它将转到您的default_controller ..

如果您要删除index.php而不是必须编写.htaccess规则

答案 1 :(得分:1)

将来,请处理子文件夹下的项目(例如:http://localhost/projectA/

你需要设置.htacces文件(覆盖xampp文件)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>