Welcome Guest, Not a member yet? Register   Sign In
routing wildcard help
#1

[eluser]simonCI[/eluser]
Hi!
I'm following the tutorial -> http://ellislab.com/codeigniter/user-gui...ction.html and having problem with my wildcard. This is my code:

Controller:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

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

public function index()
{
  echo 'Index news';
  $data['news'] = $this->news_model->get_news();
  $data['title'] = 'News archive';
  
  $this->load->view('index', $data);
}
public function view($slug)
{
  $data['news'] = $this->load->news_model->get_news($slug);
  if (empty($data['news_item']))
  {
   show_404();
  }
  
  $data['title'] = $data['news_item']['title'];
  $this->load->view('news_view', $data);
}
}

model:
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();
}
}

routes.php:
Code:
$route['news/(:any)'] = 'site/view/$1'; // wrong here too?
$route['site'] = 'site';
$route['(:any)'] = 'site/view/$1';  // problem with this one I think
$route['default_controller'] = "site";
$route['404_override'] = '';

/* from CI tutorial
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
*/

What looks wrong with my wildcard? I can access my index page, but not the view($slug) page,....

Any idea?

Regards
Simon
#2

[eluser]Stefan Hueg[/eluser]
Your routes seem to be correct, but I noticed this little bug:

Code:
$data['news'] = $this->load->news_model->get_news($slug);
  if (empty($data['news_item'])) //not set, always empty

... which will always be empty / not set. Change it to

Code:
$data['news_item'] = $this->load->news_model->get_news($slug);

And it should work
#3

[eluser]CroNiX[/eluser]
[quote author="simonCI" date="1335640891"]
Code:
routes.php:
[code]$route['news/(:any)'] = 'site/view/$1'; // wrong here too?
$route['site'] = 'site';
$route['(:any)'] = 'site/view/$1';  // problem with this one I think
$route['default_controller'] = "site";
$route['404_override'] = '';
[/quote]
First problem is on the first route, you didn't change it to "site", you left it as "news".
Second problem is that "default_controller" and "404_override" need to come before all others. I know the tutorial shows it incorrectly, but in the user guide for URI Routing, it states that those 2 rules need to come before any wildcard/regex routes at the very bottom.

Code:
$route['default_controller'] = "site";
$route['404_override'] = '';

$route['site/(:any)'] = 'site/view/$1'; // wrong here too?
$route['site'] = 'site';
$route['(:any)'] = 'site/view/$1';  // problem with this one I think
#4

[eluser]simonCI[/eluser]
Thank you for your replies!




Theme © iAndrew 2016 - Forum software by © MyBB