Welcome Guest, Not a member yet? Register   Sign In
routing, or default index with optional parameter?
#1

[eluser]andychurchill[/eluser]
What I'm trying to achieve is something like this:

/people/ (shows a list of people)
/people/andy-churchill (shows andy-churchill's profile)

I've already got a rewrite rule to handle the index.php bit, so that I'm not having to call /index.php/people/ which works fine. What I have next is:

<?php

class people extends CI_Controller {

function people()
{
parent::__construct();
}

// Default action for people, list of people in A-Z order
function index($id = null)
{
if (!is_null($id))
{
$this->_showPerson($id);
return;
}

$this->load->view('header');
$this->load->view('peoplelist');
$this->load->view('footer');
}

private function _showPerson($slug)
{
// $data is set;
$this->load->view('header');
$this->load->view('person', $data);
$this->load->view('footer');
}
}
?>

So far it works fine for /people, but not when I try to add a parameter, I just get the CI 404 error. If I change the function name from "index" to "test" and then hit /people/test/ and /people/test/something, I get the expected results, but how do I make this work without having to specify "test", as in my original requirement?

Not sure if I need to add a wildcard to the route, or add a new route entry completely, or whether I'm doing something wrong and haven't quite understood what is possible. :S
#2

[eluser]andychurchill[/eluser]
think I figured this out in the end:

<?php

class people extends CI_Controller {

function people()
{
parent::__construct();
}

function index()
{
// $data got from a model;
$this->load->view('header');
$this->load->view('peopleList', $data);
$this->load->view('footer');
}

public function _remap($param)
{
if ($param == "index")
{
$this->index();
}
else
{
$this->_showPerson($param);
}
}

private function _showPerson($slug)
{
// $data set from a model;
$this->load->view('header');
$this->load->view('person', $data);
$this->load->view('footer');
}
}
?>

This seems to be working correctly now, but I don't know if this is the "correct" way to do it, or if there is a better way?
#3

[eluser]augustowloch[/eluser]
hi!
my approach is a little different. i'm basically using routing rules:

$route['people/(:any)'] = "people/list/$1";

so.. in my controller i got the listing on:

Code:
public function index() {
...
// $data got from a model;
$this->load->view(‘peopleList’, $data);
...
}

and the specific person method:

Code:
public function list ($personName) {
...
//fill data from model using $personName
$this->load->view(‘person’, $data);
...
}
#4

[eluser]andychurchill[/eluser]
That looks like another good way of doing it, but I wonder which one is more efficient?




Theme © iAndrew 2016 - Forum software by © MyBB