这是我第一个php框架。我的控制器中有一个php文件是posts。php但是当我试图运行它localhost/codeigniter/index。php/posts时,它显示错误404
。应用程序文件夹
<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。
$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 in model文件夹
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() {
}
}
这样编码器就不会被弄糊涂了。
Post Controller为
文件名: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();
}
}
同样在你的url中,如果没有设置codeigniter/htaccess来删除index.php,那么你的url将需要在每个地方使用index.php。
http://localhost/project/index.php/post
http://www.example.com/index.php/post
注意:如果需要,不要触摸应用程序文件夹中的htaccessHtaccess在主目录中添加一个html For Codeigniter
以前版本的codeigniter v3之前,你不需要担心控制器的ucfirst,但现在你需要为版本3及以上。
添加…
$route['posts'] = 'posts/index';
除了路由配置错误之外,还有一种可能。
我注意到一个类似的问题,当你的控制器名称看起来像这样:"ControllerName"。
要解决这个问题,你只需要像这样重命名你的控制器:"Controllername"这就解决了我的问题:
进入config/routes.php,在第52行定义默认控制器;
如果您需要另一个函数而不是索引,我会从索引函数调用该函数。但那是你的责任。