CodeIgniter Forums

Full Version: basic CI codes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I still wonder why:

http://localhost/blog/

404 Page Not Found
The page you requested was not found.


views/blog.php

PHP Code:
<html>

<
title>Blog</title>

<
br>

<
h1>BLOG</h1>


<?
php echo $this->calendar->generate(); ?>

<?php $blogs $this->blog_model->select_blog();  ?>

<?php foreach ($blogs as $row) : ?>

    <i><?php echo $row->date?></i><br><br>
    <b><?php echo $row->title?></b><br><br>
    <?php echo $row->content?><br><br><br>
    
<?php endforeach; ?>


</html> 


controllers/blog.php


PHP Code:
<?php


class Blog extends CI_Controller {

        
// If you want code to run for every method in a controller, you need to create a constructor
 
       
        
public function __construct()
 
       {
                
parent::__construct();
                
                
$this->load->helper('date');                 
                
$this->load->helper('form');  
                $this
->load->library('calendar');                          
                $this
->load->library('form_validation');
 
               $this->load->model('blog_model');
        }        
                
        public function 
index()
        {
            
 
               $data['title'] = $this->blog_model->select_blog();
                
$data['content'] = $this->blog_model->select_blog();
                
$data['date'] = $this->blog_model->select_blog();
                
                
                
$this->load->view('blog'$data);
                                
        }
        
        public function 
blog_form()
        {
                
                
$this->form_validation->set_rules('pic''pic''required');
                
$this->form_validation->set_rules('title''title''required');
                
$this->form_validation->set_rules('content''content''required');
                
                if (
$this->form_validation->run() === FALSE)
                {
                
$this->load->view('blog_form');
                }
                
                else
                {
                    
$this->blog_model->insert_blog();
                    
$this->load->view('blog_form/success');                    
                }
                                
        }
}



?>


routes.php

PHP Code:
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['blog_form'] = 'blog/blog_form';

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1'

Which code do I need to fix?
Make a basic html file called home.html

Does your local host find that file? If not it is probably not a CI problem

Usually you connect via something like localhost/index.php/blog

If you have removed index.php then you have probably not done something correct there with .htaccess
PHP Code:
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['default_controller'] = 'pages/view';
$route['blog'] = 'blog/index';
$route['blog_form'] = 'blog/blog_form';
$route['(:any)'] = 'pages/view/$1'

*** Place wildcards  at the bottom ***
*** $route['blog'] = 'blog/index';
*** $route['blog/(:any)'] = 'blog/view/$1';
*** $route['(:any)'] = 'pages/view/$1';
CI 3.X Documentation default controller:

This route points to the action that should be executed if the URI contains no data, which will be the case when people load your root URL. The setting accepts a controller/method value and index() would be the default method if you don’t specify one. In the above example, it is Welcome::index() that would be called.

Note

You can NOT use a directory as a part of this setting!
what he is saying is change this

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

to something like

$route['default_controller'] = 'defaultview';

or whatever you want the controller to be called . the default controller can not be in a folder, it has to be at top level
Do you have htaccess in place to remove the index.php from your route? Sounds like you don't or it's not setup correctly. Try http://localhost/index.php/blog and see if that works. If it does, you need to remove the index.php from the URI.
controllers/Blog.php