How about just having a function for each page? This follows CodeIgniter's usual URI pattern example.com/class/function/id/
- something like this:
class Jstorecontroller extends CI_Controller
{
function index()
{
//Do what you want... load the home page?
}
//Load the 'home' page
function home()
{
$this->load->view('public/home');
}
//Load the 'about' page
function about()
{
$this->load->view('public/about');
}
//Load the 'contact' page
function contact()
{
$this->load->view('public/contact');
}
}
Routing can be used to map URLs: To map jstorecontroll as the first segment and anything as the second segment to the index
function in your jstorecontroll
controller, you could use this route (in application/config/routes.php):
$route['jstorecontroll/(:any)'] = "jstorecontroll/index/$1";
You may want to use regex to restrict what is mapped though, for example:
$route['jstorecontroll/([a-z]+)'] = "jstorecontroll/index/$1";
You could then have a function in your controller that would filter through and load the corresponding page. However, be wary of the user input - don't trust them! Make sure you sanitise the input.
class Jstorecontroll extends CI_Controller
{
function index($page = FALSE) //Default value if a page isn't specified in the URL.
{
if($page === FALSE)
{
//Do what you want if a page isn't specified. (Load the home page?)
}
else
{
switch($page)
{
case "home":
$this->load->view('public/home');
break;
case "about":
$this->load->view('public/about');
break;
case "contact":
$this->load->view('public/contact');
break;
}
}
}
}
The use of the above route may produce undesired results if you have other function in this controller that you want to be called from a URI, they won't be called but instead mapped as a parameter to the index
function. Unless you look at changing (or adding) the routes, or you could look into remapping functions. Personally, I'd just use a function for each page!