• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A better way to handle params?

#1
[eluser]davewilly[/eluser]
Hey guys, I'm finding it a bit of a messy affair dealing with multiple params on a site I'm rebuilding in CI.

I have a URL like http://www.site.com/stag-do/bristol/packages/1 and have been struggling to handle it in a clean manner without some ugly if/elseif. I had thought it would be a case of a nice switch statement. I've had to cascade if/elseif statement, with what I would consider the wrong logical flow to handle it.

My psuedo code is this:

if a package (ie. /1, /2 etc) param is set
---- Test it in a switch and display package details page
elseif packages segment isset and is equal to 'packages'
---- Show the packages page
elseif location segment is set, and location segment is equal to 'bristol' and packages segment is not set..
---- Show location page
else
---- 404

I would really appreciate if someone has a better solution. Perhaps this is more of a general PHP questions opposed to a CI specific question!

Here is code from my controller, the method I am refering to is bristol()

Code:
<?php
// Stag do controller
class stag_do extends Controller {

    /**
     * Remap
     * Calls method if it exists and passes any params, else 404's
     */
    function _remap($method)
    {
        // If method exists call it
        if(method_exists('stag_do', $method))
        {
            // Call method, and pass in URI segments
            $this->$method($this->uri->segments);
        }
        else // Method doesn't exist, call index
        {
            $this->not_found();
        }

    } // End _remap

    /**
     * Index
     */
    function index()
    {
        echo "Stag do";
    } // End index
    
    /**
     * Bristol
     */
    function bristol($segments)
    {
        // Full segment dump: Array ( [1] => stag-do [2] => bristol [3] => packages [4] => 1 )
        // Show package if package param exists
        if(isset($segments[4]))
        {
            // Show appropriate package
            switch($segments[4])
            {
                // Package 1
                case '1' :
                echo "Package 1";
                break;
                
                // Package 2
                case '2' :
                echo "Package 2";
                break;

                // Package 3
                case '3' :
                echo "Package 3";
                break;

                // Package 4
                case '4' :
                echo "Package 4";
                break;

                // Package 5
                case '5' :
                echo "Package 5";
                break;

                // Package 6
                case '6' :
                echo "Package 6";
                break;                
            
                // Invalid package param
                default :
                $this->not_found();
            
            }
        }
        // Show Packages
        elseif(isset($segments[3]) && $segments[3] == 'packages')
        {
            echo "Packages";
        }
        // Show Bristol
        elseif(isset($segments[2]) && $segments[2] == 'bristol' && !isset($segments[3]))
        {
            echo "Bristol";
        }
        // Not found
        else
        {
            $this->not_found();
        }
        
        
        
    } // End Bristol

    /**
     * Bournemouth
     */
    function bournemouth() {
    
        echo "bournemouth";

    } // End Bournemouth

    /**
     * Oxford
     */
    function oxford() {
    
        echo "Oxford";

    } // End Oxford

    /**
     * Oxford
     */
    function not_found() {
    
        echo "404";

    } // End Oxford        
    
} // End Stag do

/* End of file stag_do.php */
/* Location: ./system/application/controllers/stag_do.php */

Thanks,

Dave


Messages In This Thread
A better way to handle params? - by El Forum - 07-30-2010, 06:32 AM
A better way to handle params? - by El Forum - 07-30-2010, 07:07 AM
A better way to handle params? - by El Forum - 07-30-2010, 07:27 AM
A better way to handle params? - by El Forum - 07-30-2010, 07:31 AM
A better way to handle params? - by El Forum - 07-30-2010, 07:38 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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