Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How different routes can be pointed to same controller
  • Hey FuelPHP folks,

    I'm stuck with this routing in FuelPHP. Please if anyone can help me. At start it seems easy but I'm still not able to make this routing thing work.

    I have different category pages on website like catone, cattwo, catthree and each category has its own page like http://domain.com/catone, http://domain.com/cattwo, http://domain.com/catthree respectively.

    I want each of this URL should point to single controller and action page/index. Inside this action I want to know from which url user came like cat1 or cat 2 or mayb different cat.

    I tired doing in version 1.6.1 below but didn't work.

    routes.php has ->

    ':segment' => 'page/index/$1'

    controller page.php has ->

    class Page extends Controller {
         public function action_index($cat) {
             // Here I get $cat as null which is supposed to be "catone" when i visit http://domain.com/catone
         }
    }

    How can I correct myself? Help please
  • HarroHarro
    Accepted Answer
    Your route doesn't only have to match, you also have to indicate it's a variable that needs to be inserted on the righthand side.

    A route is a regex, so you do that with brackets:

    '(:segment)' => 'page/index/$1',
  • Thanks I put it to work. It's working, but now came up with another problem. What I did was:-

    routes.php ->  ':cat' => 'page/index'

    controller file ->
    class Page extends Controller {
        public function action_index() {
            $p = $this->param('cat');
        }
    }


    In the same controller. I'm making one HMVC request. i.e. to another controller which nav/nav.
    Ofcourse nav matches with route ':cat' (I do have nav controller)  and so its again going Pages controller... Page again calls nav controler... this way infinite loop goes on.

    Is there a way I can fix this ?
    Or can I have something like this, that all my routes will be present in routes.php like

    'catone' => 'page/index',
    'cattwo' => 'page/index',
    'catthree' => 'page/index',
    'catfour' => 'page/index',
    (Categories are no more than 7).

    But my concern is how would I know via which URL user came in?

    I can get URI and find that thing using PHP.

    But anything better FuelPHP already has ?
  • Anyways. I tried you answer and it works. Thanks Harro
  • You can use the last parameter on Request::forge(), set it to false to bypass the routing table.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion