this first php framework. have php file in controller posts.php when tried run localhost/codeigniter/index.php/posts, displays error 404
.htaccess inside application folder
<ifmodule authz_core_module> require denied </ifmodule> <ifmodule !authz_core_module> deny </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 in model folder
class post extends ci_model{ function get_posts($num = 20, $start = 0){ //$sql = "select * users active=1 order 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 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>"; } } it should display empty array shows error 404 instead
when using codeigniter 3
all controllers , models should have there first letter of class name , file name upper case example welcome.php , not welcome.php
for model because same name controller. change model name model_post
filename: model_post.php
<?php class model_post extends ci_model { public function some_function() { } } that way codeigniter not confused.
post controller
filename: 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(); } } also in url if not set codeigniter / htaccess remove index.php url need use index.php every where.
http://localhost/project/index.php/post http://www.example.com/index.php/post note: not touch htaccess in application folder if need htaccess add 1 in main directory htaccess codeigniter
previous versions of codeigniter before v3 did not need worry ucfirst controllers version 3 , above.
Comments
Post a Comment