Question

Using Fuelphp, I'd like to be able to use a URL system similar to here on StackOverflow to get to specific pages.

Desired behavior: -stackoverflow.com/questions -> page with many questions -stackoverflow.com/questions/1982 ->page with specific question

Here's my controller:

class Controllers_Images extends Controller_Template
{
   public function action_index($id=null)
   {
      if($id)
      {
         //Use Model_Image to find specific image
         //Create appropriate view
      }
      else
      {
         //Use Model_Image to find many images
         //Create appropriate view
      }
   }
}

I can access the "generic" page with mysite.com/images/ - this is routed to the action_index function. I can access a "specific" page with mysite.com/images/index/1. What I'd like to do is be able to skip index in this case too, so that mysite.com/images/1 works. Right now I'm getting 404. How do I set up routing for this?

Was it helpful?

Solution

After poking around a bit more, I came up with a solution that works fine:

class Controllers_Images extends Controller_Template
{
   public function action_index()
   {
      //Use Model_Image to find many images
      //Create appropriate view
   }
   public function get_id($id)
   {
      //Use Model_Image to find specific image
      //Create appropriate view
   }
}

In routes.php:

return array(
   'images/(:num)'=>'images/id/$1',
   //other routing stuff...
);

With this setup, the url "mysite.com/images/1" now appropriately displays the selected image as if "mysite.com/images/id/1" was used instead. Not a huge deal, but it is a nicer interface, which is important!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top