[英]Codeigniter 3.0.0 - error 404 page not found
这是我的第一个php框架。 我的控制器中有一个php文件,它是posts.php但是当我尝试运行localhost / codeigniter / index.php / posts时,它显示错误404
.htaccess在应用程序文件夹中
<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
autoload.php
$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');
config.php文件
$config['base_url'] = 'http://localhost/codeigniter/';
$config['index_page'] = 'index.php';
routes.php文件
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
模型文件夹中的post.php
class Post extends CI_Model{
function get_posts($num = 20, $start = 0){
//$sql = "SELECT * FROM users WHERE active=1 ORDER BY date_added DESC LIMIT 0,20;";
$this->db->select()->from('posts')->where('active', 1)->order_by('date_added', 'desc')->limit(0, 20);
$query=$this->db->get();
return $query->result_array();
}
}
控制器文件夹中的posts.php
class Posts extends CI_Controller{
function index(){
$this->load->model('post');
$data['posts'] = $this->post->get_posts();
echo "<pre>";
print_r($data['posts']);
echo "</pre>";
}
}
它应该显示一个空数组,但它显示错误404
使用codeigniter 3时
所有控制器和模型都应该有首字母的类名和文件名作为大写示例Welcome.php而不是welcome.php
对于您的模型,因为它与控制器的名称相同。 我会将模型名称更改为Model_post
文件名:Model_post.php
<?php
class Model_post extends CI_Model {
public function some_function() {
}
}
这样,codeigniter就不会混淆了。
后控制器将是
文件名:Post.php
<?php
class Post extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('model_post');
}
public function index() {
$this->model_post->some_function();
}
}
另外在你的网址中如果没有设置codeigniter / htaccess来删除index.php那么你的url将需要在每个地方使用index.php。
http://localhost/project/index.php/post
http://www.example.com/index.php/post
注意:如果你需要在主目录中添加一个htaccess,请不要触摸应用程序文件夹中的htaccess Htaccess For Codeigniter
在v3之前的codeigniter的早期版本你不需要担心ucfirst控制器,但现在你做的版本3及更高版本。
添加像......这样的路线
$route['posts'] = 'posts/index';
在我的情况下,这解决了这个问题:
转到config / routes.php并在第52行定义默认控制器;
如果你需要另一个函数而不是索引,我会从索引函数调用该函数。 但那是在你身上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.