0

所有文件都在 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 不支持基于段的方法吗?

4

2 回答 2

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规则

于 2013-08-05T07:25:09.443 回答
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>
于 2013-08-04T19:34:34.097 回答