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

[eluser]xzela[/eluser]
Hi all,

I'm trying to use the routing feature in CI and I'm having an issue with handling empty strings with the (:any) flag.

Here is what I'm trying to accomplish:

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).

The above works (kind of). The issue is when a user goes to http://localhost/pages/cake-types they get a 404 Page Not Found message. Yet, when the user goes to 'child page' like http://localhost/pages/cake-types/cupcakes it works as expected.

What is it about my routes file? How do you set it so empty strings are treated correctly?

routes.php //routes file
Code:
$route['pages/cake-types/(:any)'] = 'pages/cake_types/$1';

pages.php //controller file
Code:
//...
public function cake_types($type = null) {
  if($type == null) {
     $this->load->view('pages/cakes/cake_type_list_view');
  }
  else if ($type == 'cupcakes') {
     $this->load->view('pages/cakes/cupcakes_view');
  }
  //... more if conditions
}
//...

Any information you can lend will be much appreciated.

Thanks
#2

[eluser]WanWizard[/eluser]
I don't see why you would need a route here, any URI starting with pages would be routed to your pages controller automatically.

The issue is probably that if you don't have a cake type in the URI, there is no match on the route rule, which should make it fall through to the default controller (or show a 404 if there is no default controller).
#3

[eluser]xzela[/eluser]
In this particular case, I am using the routing rules for SEO purpose:

I'm routing because functions can not have dashes in their names, a function name of cake-types() is illegal. However, dashes are used primarily as way to separate words in a URL. For some reason this is a form of best practices in the SEO world. Thus the reason for routing.

I probably should have mentioned that if i remove the routing rules (ie, goto the URI directly: http://localhost/pages/cake_types) it appears to work.

Not sure what's going on at this point.

Thanks for the help by the way.
#4

[eluser]xzela[/eluser]
So, after some experimentation, I may have a quick hack which solves the problem.

If I change my routing rules to this:

Code:
$route['pages/cake-types'] = 'pages/cake_types/'; //default, non-specific cake page
$route['pages/cake-types/(:any)'] = 'pages/cake_types/$1'; //specific cake page

So far, this works.

Thanks for the help everyone.
#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
    }
  }
  
}




Theme © iAndrew 2016 - Forum software by © MyBB