繁体   English   中英

为什么这个控制器在 code-igniter 版本 3.0.1 中不起作用?

[英]Why this controller is not working in code-igniter version 3.0.1?

class Users extends CI_Controller{

    public function index(){
        echo 'hello';
    }
}

我将文件名保留为 Users.php,但仍然无法正常工作。

请考虑Codeigniter 命名转换

config.php

$config['base_url'] = '';
$config['index_page'] = '';

.htaccess放在应用程序文件夹之外

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule>

routes.php

$route['default_controller'] = "users";//default controller name

并在网址中

http://localhost/<path to file>/project_name/users

注意:您不能访问带有任何文件扩展名(如.php.html URL。

在您的班级中启动合约功能

class Users extends CI_Controller{

    public function __construct(){
        parent::__construct();
    }

    public function index(){
        echo 'hello';
    }
}

并运行 url 作为example.com/index.php/users/

这可能会帮助你:

-> app/config/routes.php

$route['default_controller'] = 'pages/homePage';

-> app/controllers/Pages.php(注意文件上的大写P)

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

class Pages extends MY_Controller {//Note the capital Class name
    public function homePage()
    {
        $this->load->view("pages/homepage.php");
    }
}

另外要摆脱 index.php 执行以下操作:

-> app/config/config.php

$config['index_page'] = '';

在 app 文件夹外创建一个名为“.htaccess”的文件,并将以下代码放入其中:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

这个 HTaccess 来自另一个名为 Laravel 的 PHP 框架,它似乎比他们在其网站上提供的标准 CI 的 HTaccess 工作得更好。

我希望这能够帮到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM