CodeIgniter Forums
Page not found error in create a news tutorial - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Page not found error in create a news tutorial (/showthread.php?tid=48857)

Pages: 1 2


Page not found error in create a news tutorial - El Forum - 01-31-2012

[eluser]GND94MCA[/eluser]
Hi i am new codeigniter i was just used the given tutorial and there is showing page not found error how can i solve it. help me.


Page not found error in create a news tutorial - El Forum - 01-31-2012

[eluser]InsiteFX[/eluser]
Gee let me find my Crytal Ball!

Show some errors and code...



Page not found error in create a news tutorial - El Forum - 01-31-2012

[eluser]GND94MCA[/eluser]
Hi All, Here i am using user guide tutorial for Create news items, i have go through as define in user guide but when we input some data into form and submit, the url will show in the browser 'http://localhost/generater/index.php/news/localhost/?news/create' and 404 error will get. and the code process will given here see and help me.

1- Create the new view at application/views/news/create.php.

<h2>Create a news item</h2>

&lt;?php echo validation_errors(); ?&gt;

&lt;?php echo form_open('news/create') ?&gt;

<label for="title">Title</label>
&lt;input type="input" name="title" /&gt;&lt;br />

<label for="text">Text</label>
&lt;textarea name="text"&gt;&lt;/textarea><br />

&lt;input type="submit" name="submit" value="Create news item" /&gt;

&lt;/form&gt;

2- News Controler class
class News extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}

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');
// $this->form_validation->run();

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');
}
}

}
3- Model class
class News_model extends CI_Model {

public function __construct()
{
$this->load->database();
}
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);
}

}
4- changes in routes.php

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

Any one help me.


Page not found error in create a news tutorial - El Forum - 01-31-2012

[eluser]InsiteFX[/eluser]
Code:
// there should be no ? mark in the url!
http://localhost/generater/index.php/news/localhost/?news/create

// should be this.
http://localhost/generater/index.php/news/localhost/news/create

Unless it is a typo!

And I do not see your posted page controller.



Page not found error in create a news tutorial - El Forum - 03-10-2012

[eluser]greggie[/eluser]
Hi, I have the same 404 error problem after clicking the create news item button -instead of the success page, or the form again. The URL doubles up from -"http://localhost/CI_2.1.0/index.php/news/create" to "http://localhost/CI_2.1.0/index.php/news/localhost/index.php/news/create".

All my code for the pages are as follows, as I worked through the tutorial.
(I would note that working through the tutorials is difficult, as there seems to be the odd code error-based on the forum input- and a bit vague as to which page the next code snippet should go to! Perhaps being able to down load the working files at the end of each tut would aid us newbies, and reduce your work load!).

create.php =
&lt;?php echo validation_errors(); ?&gt;
&lt;?php echo form_open('news/create') ?&gt;
<label for="title">Title</label>
&lt;input type="input" name="title" /&gt;&lt;br />
<label for="text">Text</label>
&lt;textarea name="text"&gt;&lt;/textarea><br />
&lt;input type="submit" name="submit" value="Create news item" /&gt;
&lt;/form&gt;

news.php =
&lt;?php
class News extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}

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)
{
$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');
}
}
}
?&gt;

success.php =
&lt;?php
echo "Success, new article loaded";
?&gt;

news_model.php =
&lt;?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);
}
}

routes.php =
$route['news/(:any)'] = 'news/view/$1'; // used for a dynamic tut
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

/* An extra rule to start adding news items. This makes sure CodeIgniter sees'create' as a method instead of a news item's slug. */
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

I can't see where the problem is. Needless to say, nothing is getting written to the Db.
Any help is much appreciated. Thanx.



Page not found error in create a news tutorial - El Forum - 03-10-2012

[eluser]InsiteFX[/eluser]
Check your ./application/config/config.php base_url setting.

If you leave it blank CI will figure it out for you.



Page not found error in create a news tutorial - El Forum - 03-11-2012

[eluser]greggie[/eluser]
Aaah, ok, InsiteFX. Thanx for that, that works ! cheers


Page not found error in create a news tutorial - El Forum - 03-15-2012

[eluser]StevenTen[/eluser]
Hi,

The tutorial example also not work for me. When I create a news, and submit. It will not go to success page.

Instead it will direct to a weird url: http://www.abc.com/index.php/news/www.abc.com/index.php/news/create

Where, Suppose my base url is www.abc.com

Why this happend? I just followed the tutorial exactly. The only thing I added it a success.php page to views/news/

Thanks


Page not found error in create a news tutorial - El Forum - 03-15-2012

[eluser]InsiteFX[/eluser]
Show your config.php base_url



Page not found error in create a news tutorial - El Forum - 03-15-2012

[eluser]StevenTen[/eluser]
Before I just left it blank, and it wont work

and I then set it to be


$config['base_url'] = 'www.abc.com';

still can not work