• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delving into Routes... Help!

#1
[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

#2
[eluser]Valdis Ozols[/eluser]
If that works as supposed then it seems ok taking into account that there are only few routes defined.

#3
[eluser]JamesTaylor[/eluser]
yes it's good that it works, but i think that by creating my 1st route it has actually generated the need for the other 6 routes... which makes me think i haven't implemented it very effectively - i.e adding 1 route has caused me problems on 6 other routes?

#4
[eluser]JamesTaylor[/eluser]
My current setup has also meant i have to set routes which are identical to the the original URL:
Code:
$route['Admin/Menus/Update'] = "Admin/Menus/Update";


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.