Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Pass two variable with router
  • Hello
    I met a little problem, pass two variable with router.
    I would like create a blog with fuelphp, and the url look like http://www.exemple.fr/category/post but, when configure routes.php I can't pass this two variable in url, because the action was called is all time the first action in the router, not the second

    In the router this code is present:
    '(:any)' => 'post/list/$1',

    '(:any)/(:any):' => 'post/view/$1/$2',
    And in my Controller_Post:

    public function action_list($categorySlug)
    {
    // somes code
    }
    public function action_view($categorySlug, $postSlug)
    {
    //some code
    }
    Sorry for some mistake ^^'
    Thank you for your help

    Dryusdan :)
  • The router method gets passed two arguments: the first is the name of the action called, the second an array with parameters.

    Under normal circumstances you don't need a router, so you should check that first. If it turns out you do, you do something like this:

    public function router($method, $params)
    {
        // some code

        // make sure the method to call is callable
        if ( ! is_callable("action_".$method))
        {
            throw new HttpNotFoundException;
        }

        // call the method and return the results
        return call_user_func_array(array($this, "action_".$method), $params)
    }

  • Hello
    Thank for you response :)
    I would like hide the action (post/view) and the router looks like a good file for config this no?

    (It's possible if I not understand :-S I m French and a new user of fuelphp )
    Dryusdan :)
  • With your routing config you do "hide" it from the outside world, but not from the framework itself.

    The framework (the Request class) just sees the final route, which will be either "post/list" or "post/view", so creating actions for those is perfectly fine, no router needed.

    You would need a router if the action name can be random, for example "/user/<username>", in which you use the router to catch all requests. But even this can be handled using a route:

    "user/(:any)" => "user/view/$1"

    and create

    public function action_view($username)
    {
        // code
    }

    to handle these requests...
  • Hello
    I doing this ^^ but, when I hide list / view with the router and I put two random variable (name of category and name of title) the list page display. 
    I make this route:


    'blog/([a-zA-Z0-9._-]+)' => 'post/list/$1',
    'blog/([a-zA-Z0-9._-]+)/([a-zA-Z0-9._-]+)' => 'post/view/$1/$2',

    And it work fine ;) It's correct for fuelphp?


    Thank you for your help :)
  • HarroHarro
    Accepted Answer
    You don't need to write your own regex, you can also use
    'blog/(:segment)' => 'post/list/$1',
    'blog/(:segment)/(:segment)' => 'post/view/$1/$2',
    or even
    'blog/(:any)' => 'post/list/$1',
    'blog/(:segment)/(:any)' => 'post/view/$1/$2',

    if you expect further parameters.
  • Your first method work, the second display post/list

    Thank you :D

    Another question, what is difference between ":any" and ":segment"?  (I haven't understood the difference in the doc ^^")
  • :segment matches only a single segment, :any matches zero or more segments.

    So If you have a URL like this:

    "/blog/post/12/comments/10"

    and you have a route like this: "blog/post(:any)" => "load/$1",
    the route will translate to "load/12/comments/10".

    If you have a route like this:"blog/post/(:segment)" => "load/$1",
    you will not have a route match, since the URL contains more than one segment.

  • Okay :)
    Thank you for you answer :)

Howdy, Stranger!

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

In this Discussion