问题描述
这是我的第一个php框架。我有一个php文件在我的控制器是posts.php但是当我试图运行它localhost / codeigniter / index.php / posts,它显示错误404
This is my first php framework. I have a php file in my controller which is posts.php but when I tried to run it localhost/codeigniter/index.php/posts, it displays error 404
。 htaccess在应用程序文件夹中
.htaccess inside application folder
<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
autoload.php
autoload.php
$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');
config.php
config.php
$config['base_url'] = 'http://localhost/codeigniter/';
$config['index_page'] = 'index.php';
routes.php
routes.php
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
post.php在模型文件夹中
post.php in model folder
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在控制器文件夹
posts.php in controller folder
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>";
}
}
推荐答案
使用codeigniter 3时
When using codeigniter 3
所有控制器和模型都应有类名和文件名的第一个字母作为大写示例 Welcome.php ,而不是welcome.php
All controllers and models should have there first letter of class name and file name as upper case example Welcome.php and not welcome.php
对于您的模型,因为它与控制器具有相同的名称。我会将模型名称更改为Model_post
For your model because it is the same name as controller. I would change model name to Model_post
文件名:Model_post.php
<?php
class Model_post extends CI_Model {
public function some_function() {
}
}
这样codeigniter不会感到困惑。
That way codeigniter will not get confused.
Post控制器是
文件名: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();
}
}
up codeigniter / htaccess删除index.php然后你的url将需要使用index.php每个地方。
Also in your url if not set up codeigniter / htaccess to remove index.php then your url will need to use index.php every where.
http://localhost/project/index.php/post
http://www.example.com/index.php/post
之前版本的codeigniter before v3你不需要担心ucfirst为控制器,但现在你做的版本3及以上。
Previous versions of codeigniter before v3 you did not need to worry about ucfirst for controllers but now you do for version 3 and above.
这篇关于Codeigniter 3.0.0 - 找不到错误404页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!