Codeigniter error 404 on news item tutorial - php

I saw plenty of questions and tried all those fixes similar to this issue but could not get this resolved. Please help.
I am following codeigniter news item tutorial. I displayed news from database. When I click on a link to view particular news it throws 404.
Here is the code:
Controller:
<?php
class News extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url');
}
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);
// var_dump($data);exit;
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');
}
}
Model:
<?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();
}
}
View:
news.php
<?php
class News extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url');
}
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);
// var_dump($data);exit;
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');
}
}
view.php
<?php
echo '<h2>' . $news_item['title'] . '</h2>';
echo $news_item['text'];
routes:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
Base url is blank in config.
CI version is 2.1.4 (latest)
This URL shows all the news items correctly - http://localhost/educon/index.php/news/
This URL throws 404 - http://localhost/educon/index.php/news/view/n1

The problem is in your routes. Your routes file have the line as
$route['news/(:any)'] = 'news/view/$1';
But your url is
http://localhost/educon/index.php/news/view/n1
This will redirect to
http://localhost/educon/index.php/news/view/view/n1
In your view($slug) method you have a parameter. You dump this $slug in first line of view() and check it.

I had a similar issue. For me, the routes.php file was fine (by "fine" I mean it matched what the tutorial said to do (also version 2.1.4)), but my views/news/index.php file generated the news article link like so:
<p>View article</p>
After noting the issue with the generated path, I dropped news from the path and this worked:
<p>View article</p>
So links went from: /news/news/news-item-2 to /news/news-item-2
And given the tutorial's routing:
$route['news/(:any)'] = 'news/view/$1';
That then made sense.

I encountered this just now, and I also just copied everything in the tutorial, the thing is, it is in the arrangement of the routing, at first, I just added the new codes in routes.php like this....
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['news'] = 'news';
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
then change it to
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
And now it works like a charm...

Using slug without spaces worked for me.
The 'News Section' part of the tutorial tells you to create some seed records. In my case, my slug contained spaces and I got the 404 error. Removing the spaces fixed this. (CodeIgniter v.3.1.4)

try to check your link that goes to news listing it should be something like this
<li>News</li>
you need to change it to read as follows, because i tried it out and its the fix for the problem
<li>News</li>
It will be great if someone with more experience can guide on how to correct this in the route.php
below is my route.php
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news/index';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
note it will work even if you make the second line as:
$route['news'] = 'news';
and .htaccess is as:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
best

Related

How to solve page blank problem with codeigniter "news" section?

I'm following the Codeigniter tutorial: http://localhost:8080/myproject/user_guide/tutorial/news_section.html
When i point my browser to http://localhost:8080/myproject/news as the tutorial indicates at the final of the section, "Point your browser to your document root, followed by index.php/news and watch your news page." appears a blank page.
I tried to point to localhost:8080/index.php/news or /myproject/news but occurs the same problem.
Also i tried to set in autoload.php the next: $autoload['libraries'] = array('database');
as is indicated in codeigniter news section tutorial not working but not solve, so i leave it like this: $autoload['libraries'] = array('');
This is the routes:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
The News.php:
<?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();
}
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');
}
}
the view.php:
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
and the News_model.php:
<?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();
}
}
I expect to watch the news page

404 index() function codeigniter

Well, when I try to access to:
http://example.com/index.php/pages/index
I have no problem, but when I try with:
http://example.com/index.php/pages
I have an 404 error.
My controller's code:
<?php
class Pages extends CI_Controller {
public function index(){
echo "index page";
}
public function view($page = 'home'){
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php')){
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
?>
My routes config:
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
In your routes.php, add the following line of code above the line: $route['(:any)'] = 'pages/view/$1';
$route['pages'] = 'pages';
If that doesn't work try commenting the line $route['(:any)'] = 'pages/view/$1'; and check the url: http://example.com/index.php/pages

CodeIgniter Tutorial Not Working

I'm working on the second half of the CodeIgniter beginners tutorial which can be found at http://www.codeigniter.com/userguide3/tutorial/news_section.html and like many others I can't get it to work properly.
The post index works fine but every time I click on "view article" I just get a 404 message.
I've looked over a number of posts about this and tried a number of solutions that were offered but so far nothing has worked which is driving me slightly crazy considering I copied and pasted the code from the tutorial to make sure I didn't copy it wrong by mistake.
My code is as follows:
Controller:
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url');
}
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'] = $this->news_model->get_news($slug);
if (empty($data['news']))
{
show_404();
}
$data['title'] = $data['news']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
}
Model:
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();}}
View:
view.php
echo '<h2>'.$news['title'].'</h2>';
echo $news['text'];
index.php
<h2><?php echo $title ?></h2>
<?php foreach ($news as $news): ?>
<h3><?php echo $news['title'] ?></h3>
<div class="main">
<?php echo $news['text'] ?>
</div>
<p>View article</p>
<?php endforeach ?>
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';
Thanks in advance!
Did you managed to work it out?
What is the href in the anchor provides you with? I believe you should use the site_url() method to fix it, so the anchor should be:
<p>View article</p>
I swapped the

CodeIgniter news/view doesn't work, page 404's

I am currently learning my ways with CodeIgniter and I need some help with the view news part of the tutorial. It shows a 404 page. My site is Tristans.tk. Click on news and then on one of the titles. Here is my code:
this is my model:
class News_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_news($ID=FALSE){
if($ID===FALSE){
$query=$this->db->order_by("post_time", "DESC")->get('news');
return $query->result_array();
}
$query=$this->db->get_where('news',array('ID'=>$ID));
return $query->row_array();
}
}
this is the news controller:
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($ID){
$data['news_item'] = $this->news_model->get_news($ID);
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');
}
}
and this is the view:
<?php
print_r($news_item);
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['content'];
?>
the routes:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
news index:
<?php foreach ($news as $news_item): ?>
<div class='post'>
<h1 class='post_title'><?php echo $news_item['title'] ?></h1>
<p class='post_time'><?php echo $news_item['post_time'] ?></p>
<p class='post_content'><?php echo $news_item['content'] ?></p>
<span class='comment_link'>Comment</span>
</div>
<?php endforeach ?>
try change this
$route['news/(:any)'] = 'news/view/$1';
to
$route['news/(:any)'] = 'news/$1';
by using this: $route['news/(:any)'] = 'news/view/$1';,
if you acces yourdomain.tk/news/view/1
that actually you access yourdomain.tk/news/view/view/1
Change
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
$route['news'] = 'news';
$route['news/create'] = 'news/create';
$route['news/show/(:any)'] = 'news/show/$1';
to
$route['news'] = 'news';
$route['news/create'] = 'news/create';
$route['news/show/(:any)'] = 'news/show/$1';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
good luck

dynamic Page control in codeigniter

In my CI project, I would like to use a full dynamic page control. So, I’ve two controller methods, which load the php files. The model control’s queries are based on url segments. All page output generated automatically in the views php file depending on url and results of database, except index.php file.
Is this a right way?
Controller
public function index()
{
$data['title'] = "Index";
$data['nav'] = $this->content_model->get_index_nav(); //TODO
$this->load->view('templates/header', $data);
$this->load->view('templates/nav', $data);
$this->load->view('templates/nav_pict', $data);
$this->load->view('pages/aktualis', $data);
$this->load->view('templates/footer', $data);
}
public function view($page)
{
$page = 'content';
$this->load->helper('text');
$this->load->helper('url');
$page = lcfirst(convert_accented_characters(urldecode($page)));
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$data['nav'] = $this->content_model->get_nav();
$data['content'] = $this->content_model->get_content();
if(empty($data['content']))
{
show_404();
}
$this->load->view('templates/header', $data);
$this->load->view('templates/nav', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
Model (Each menu has a content in database. When create a new submenu you must be add content)
public function get_content()
{
$this->db->select('content.*, mainmenu.label');
$this->db->from('content');
$this->db->join('mainmenu', 'mainmenu.id = content.katId', 'left');
$this->db->where('mainmenu.label', mysql_escape_string(urldecode(end($this->uri->segments))));
$query = $this->db->get();
return $query->result_array();
}
Routing
$route['404_override'] = '';
$route['/:any/(:any)'] = 'pages/view/$1';
$route['(:any)'] = 'pages/view/$1';
$route['Index'] = 'pages/index';
$route['default_controller'] = 'pages/index';
You're using CodeIgniter just fine, there's no need for any structure modification.

Categories