Welcome Guest, Not a member yet? Register   Sign In
Routes, how to handle empty strings? [SOLVED, with hack]
#5

[eluser]nuwanda[/eluser]
[quote author="xzela" date="1279925214"]
When a user goes to the URI http://localhost/pages/cake-types I want to show the user a list of cake types. However, when the user goes to the URI http://localhost/pages/cake-types/cupcakes it should the appropriate page (a page related to cupcakes).
[/quote]

Hmmm, this is one of those times when you wonder if you're reading the question correctly.

No routes required.

You have a controller, say Cakes, with a method of cake_types. If no cake type is specified, show all cakes, else show the cake type requested.

Sample URIs:

http://www.houseofcakes/cakes/cake_type //shows all cakes
http://www.houseofcakes/cakes/cake_type/cupcakes //shows cupcakes

Code:
class Cakes extends Controller{
    
  function __construct()
  {
    parent::Controller();
  }
  
  function cake_type($type='')
  {
    if($type=='')
    {
      //show all types
    }
    else
    {
      //show $type
    }
  }
  
}

No?

And if you called the cake_type method in the index method, you could also use the URI

http://www.houseofcakes/cakes //shows all cakes because it calls the cake_type method without argument

Code:
<?php

class Cakes extends Controller{
    
  function __construct()
  {
    parent::Controller();
  }
  
  function index()
  {
    $this->cake_type();
  }
  
  function cake_type($type='')
  {
    if($type=='')
    {
      echo 'All!';//show all types
    }
    else
    {
      echo $type;//show $type
    }
  }
  
}


Messages In This Thread
Routes, how to handle empty strings? [SOLVED, with hack] - by El Forum - 07-23-2010, 08:14 PM



Theme © iAndrew 2016 - Forum software by © MyBB