• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create news items Tutorial Not Working

#1
Hi guys

I'm a newbie in codeigniter and has been following the tutorial until I hit death end in the last tutorial 'Create New Item'. 

I tried to insert data into MySQL:
http://localhost/codeIgniter/index.php/news/create
and hit error:  
The webpage cannot be found HTTP 404


I tried to update my config.php from
Code:
$config['base_url'] = 'http://localhost/codeIgniter/';
to
Code:
$config['base_url'] = '';
but still the same
My routes.php:
Code:
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = "pages/view";
$route['404_override'] = '';

My controller News.php
Code:
<?php
class News extends CI_Controller {
        public function __construct()
        {
                parent::__construct();
                $this->load->model('news_model');
                $this->load->helper('url_helper');
        }
        public function index()
{
        $data['news'] = $this->news_model->get_news();
        $data['title'] = 'News archive';
        $this->load->view('templates/header', $data);
        $this->load->view('news/index', $data);
        $this->load->view('templates/footer');
}
       public function view($slug = NULL)
{
        $data['news_item'] = $this->news_model->get_news($slug);
 
        if (empty($data['news_item']))
        {
                show_404();
        }
        $data['title'] = $data['news_item']['title'];
        $this->load->view('templates/header', $data);
        $this->load->view('news/view', $data);
        $this->load->view('templates/footer');
}
public function create()
{
    $this->load->helper('form');
    $this->load->library('form_validation');
    $data['title'] = 'Create a news item';
    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('text', 'Text', 'required');
    if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('templates/header', $data);
        $this->load->view('news/create');
        $this->load->view('templates/footer');
    }
    else
    {
        $this->news_model->set_news();
        $this->load->view('news/success');
    }
}
}

My model News_model.php
Code:
<?php
class News_model extends CI_Model {
     public function __construct()
        {
                $this->load->database();
        }
public function get_news($slug = FALSE)
{
        if ($slug === FALSE)
        {
                $query = $this->db->get('news');
                return $query->result_array();
        }
        $query = $this->db->get_where('news', array('slug' => $slug));
        return $query->row_array();
}
public function set_news()
{
    $this->load->helper('url');
    $slug = url_title($this->input->post('title'), 'dash', TRUE);
    $data = array(
        'title' => $this->input->post('title'),
        'slug' => $slug,
        'text' => $this->input->post('text')
    );
    return $this->db->insert('news', $data);
}
   }

My view create.php
Code:
<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create'); ?>
    <label for="title">Title</label>
    <input type="input" name="title" /><br />
    <label for="text">Text</label>
    <textarea name="text"></textarea><br />
    <input type="submit" name="submit" value="Create news item" />
</form>
LoadModule rewrite_module modules/mod_rewrite.so is enable
Thanks in advance!
Reply

#2
Try comment the routes.php line:

PHP Code:
//$route['news/(:any)'] = 'news/view/$1'; 


And see if it works.

This route is overriding the "$route['news/create'] = 'news/create';" and going the the show_404() inside the view method.

If to comments solves, try put the news/create route below of the news/(:any). Remove the comment and test.
Reply

#3
Don't leave $config['base_url'] empty.
For more information: https://www.codeigniter.com/user_guide/i...index.html

Routes like:
PHP Code:
$route['news/create'] = 'news/create';
$route['news'] = 'news'

are useless. You're telling CodeIgniter to behave in a way it already knows. If the http request is news/create, then by default CI will look for a method (= function) named "create" inside the controller "News.php". You don't need a route for that.

The route
PHP Code:
$route['news/(:any)'] = 'news/view/$1'

is useful, because it tells CI to look for the method "view" inside the controller "News", and pass everything after the news/ part as a parameter for that method.
Reply

#4
(01-29-2018, 02:06 PM)Wouter60 Wrote: Don't leave $config['base_url'] empty.
For more information: https://www.codeigniter.com/user_guide/i...index.html

Routes like:
PHP Code:
$route['news/create'] = 'news/create';
$route['news'] = 'news'

are useless. You're telling CodeIgniter to behave in a way it already knows. If the http request is news/create, then by default CI will look for a method (= function) named "create" inside the controller "News.php". You don't need a route for that.

The route
PHP Code:
$route['news/(:any)'] = 'news/view/$1'

is useful, because it tells CI to look for the method "view" inside the controller "News", and pass everything after the news/ part as a parameter for that method.

Once I remark this, I hit error: 404 Page Not Found
Reply

#5
All routes that (:any) in them need to be the last routes in the routes file,
or they will over ride all the other routes.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#6
(01-30-2018, 05:07 AM)InsiteFX Wrote: All routes that (:any) in them need to be the last routes in the routes file,
or they will over ride all the other routes.

Tried the below but no luck Sad

Code:
$route['news/create'] = 'news/create';
$route['news'] = 'news';
$route['default_controller'] = "pages/view";
$route['404_override'] = '';
$route['news/(:any)'] = 'news/view/$1';
$route['(:any)'] = 'pages/view/$1';
Reply

#7
The  404 is being generated because the News Tutorial is missing
the view/news/success.php HTML view...

In the News/create method

view/news/success.php
PHP Code:
<?php
echo '<h2>Create News Item - Success</h2>'

I just built the whole tutorial and it runs fine!

There is an error in the News controllers constructor:

PHP Code:
   /**
     * __construct ()
     * -----------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
 
   public function __construct()
 
   {
 
       parent::__construct();

 
       $this->load->model('news_model');

 
       // This  should be ( $this->load->helper('url'); )
 
       $this->load->helper('url_helper');

 
       log_message('debug'"News Controller Class Initialized");
 
   
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#8
(02-01-2018, 01:01 PM)InsiteFX Wrote: The  404 is being generated because the News Tutorial is missing
the view/news/success.php HTML view...

In the News/create method

view/news/success.php
PHP Code:
<?php
echo '<h2>Create News Item - Success</h2>'

I just built the whole tutorial and it runs fine!

There is an error in the News controllers constructor:

PHP Code:
   /**
     * __construct ()
     * -----------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
 
   public function __construct()
 
   {
 
       parent::__construct();

 
       $this->load->model('news_model');

 
       // This  should be ( $this->load->helper('url'); )
 
       $this->load->helper('url_helper');

 
       log_message('debug'"News Controller Class Initialized");
 
   

Thanks for the reply.  However, the result is still the same Sad
I attach my codes here ...


Attached Files
.zip   codeignitor.zip (Size: 58.15 KB / Downloads: 36)
Reply

#9
You must have a setup configuration problem because it runs fine here.

For some reason it will not let me attach the zip file.

You can download my version from here.

News Tutorial Download

Like I said this is tested and working here so if it doe's not work for you
then you have a setup of apache php etc; Problem.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#10
(02-02-2018, 05:00 AM)InsiteFX Wrote: You must have a setup configuration problem because it runs fine here.

For some reason it will not let me attach the zip file.

You can download my version from here.

News Tutorial Download

Like I said this is tested and working here so if it doe's not work for you
then you have a setup of apache php etc; Problem.

Thanks.  I tried yours but same result.
I will reinstall apache/php/mysql and update the result here.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.