[eluser]JamesTaylor[/eluser]
I have just delved into using routes for the 1st time, i've got things working as i expect they should but i don't think i've done it very effeciently!
In my contoller named Menus i have an index function which is displaying a front end form for the user and determining what info should be shown, what function the form should perform - insert new record or edit existing (it is part of an admin section).
If the form is inserting it submits to the insert function within this controller, when its editing / updating it submits to the update function.
In the controller i then have additional functions to actually perform the insertion, updating and deleting of records.
here's the controller (in summery to save on the character limit!):
Code:
<?php
class Menus extends Controller {
function Menus()
{
stuff
}
function index($Tavern = 'null', $MenuType = 'null', $MenuCat = 'null')
{
$data['Tavern'] = $Tavern;
$data['MenuType'] = $MenuType;
$data['MenuCat'] = $MenuCat;
$data = array(
'Tavern' => $Tavern,
'MenuType' => $MenuType,
'MenuCat' => $MenuCat
);
$this->session->set_userdata($data);
//generates menu links url's
$data['MenuTypeURL'] = 'Admin/Menus/'.$Tavern;
$data['MenuCatURL'] = 'Admin/Menus/'.$Tavern.'/'.$MenuType;
//If Tavern / Menu Type / Menu Cat have been set - select from DB to display for edit / delete
if ( $Tavern != 'null' && $MenuType != 'null' && $MenuCat != 'null' ) {
$DB = 'menus';
$this->load->model('Admin/menus_model');
$data['Results'] = $this->menus_model->get($DB, $Tavern, $MenuType, $MenuCat);
}
//
$data['FormFunction'] = 'Insert';
$data['MainMenu'] = 'Admin/MainMenu';
$data['MainContent'] = 'Admin/menus';
$data['h2'] = 'Menus';
$this->load->view('Admin/template-admin', $data);
}
function form_validation()
{
Validation code
}
function insert()
{
run validation - if successfuly insert to DB - then run InsertSuccess to show user taask completed
}
function InsertSuccess()
{
Display a success page
}
function edit()
{
Display Form to edit existing record - on submit run Update function
}
function update()
{
run validation - if success perform update of DB record
}
function UpdateSuccess()
{
Display success page
}
function delete()
{
display page to confirm delete is required - on confirm run DeleteConfirmed function
}
function deleteconfirmed()
{
perform delete of record from DB
}
function DeleteSuccess()
{
show success page to user
}
On the index function because it is catching variables from the url segments it wasn't running the 'index' function and was instead looking for a corresponding function.
for example if the URL came in as .../Menus/PubName/MenuType/MenuCat the idea is that the PubName / MenuType / MenuCat were caught as variables - but it was trying to run the function 'PubName'
As i wanted to keep my URL cleaner and without the .../Menus/index/... i set up my 1st route!
Code:
$route['Admin/Menus/(:any)'] = "Admin/Menus/index/$1";
but i soon realised that meant every URL was getting routed to the index function so i started adding more routes and have ended up with the following:
Code:
$route['Admin/Menus/Insert'] = "Admin/Menus/insert";
$route['Admin/Menus/Edit/(:any)'] = "Admin/Menus/edit/$1";
$route['Admin/Menus/Update'] = "Admin/Menus/update";
$route['Admin/Menus/Delete/(:any)'] = "Admin/Menus/delete/$1";
$route['Admin/Menus/DeleteConfirmed/(:any)'] = "Admin/Menus/deleteconfirmed/$1";
$route['Admin/Menus/DeleteSuccess'] = "Admin/Menus/deletesuccess";
$route['Admin/Menus/(:any)'] = "Admin/Menus/index/$1";
The routes work and everything behaves as i expect but i figure i haven't really implemented this effectively so was hoping people would offer me some advice and show me where i've gone wrong!
I am i right in thinking that the main problem lies in the fact i'm using the index function? if i changed that name to 'view' then i wouldn't need any routes at all, but i would end up with view in my URL's which isn't actually needed?
James