Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Problem with routes (slug)
  • Hi,

    i need to have route with slug like this : mysite.com/julien, mysite.com/fabien, etc..

    I have tried to put the route pattern "(:slug)" at the end of routes.php files. It's work, but if i want to go on other page with no route, just "controller/action/params" it's doesn't work.

    How i can resolv my problem?
  • HarroHarro
    Accepted Answer
    if you don't want to create routes for these other controllers, the easiest way is to deal with this in your 404 method.

    Do a lookup of first parameter, and if found, deal with it. if not set the http status to 404 and show your error page.
  • In routes.php, i have at the end :
    '(:slug)' => array('frontend/adherent/view_page_by_slug/$1', 'name' => 'adherent_view_page'),

    The problem is if i want to go on controller/action with no route (like: mysite.com/mycontroller/myaction), the route "adherent_view_page" is selected, it's not a 404 route
  • This could be great when i go on the route "adherent_view_page", and don't find the page with slug, it's go on other next routes.

    I put my route at the top of routes.php (:slug)' => array('frontend/adherent/view_page_by_slug/$1', 'name' => 'adherent_view_page'),
    And the function :

        public function action_view_page_by_slug($slug = null)
        {
            is_null($slug) and Response::redirect('adherent');
            
            if (!$data['adherent'] = Model_PageAdherent::query()->where('slug', '=', $slug)->get_one()) {
    // Go other next routes
            }
            
    // Show the page
        }
  • Yeah, that doesn't work because it captures all requests.

    That's why I suggested to let it fall through, and deal with it in your 404 route.

    The routing engine doesn't have any option at the moment to attach code to it, this will be introduced in v2.0.

  • Ok i have understand what you said with the 404 route, thanks
  • Hmm, how i can get the first parameter in 404 action ?

    example: mysite.com/julien
    In the action if i do : var_dump(Uri::string()), it's said : welcome/404
  • Ok it's work if i do :

    public function action_404()
    {
                    $slug = substr($_SERVER['REQUEST_URI'], 1);
                    if ($adherent = Model_Pageadherent::query()->where('slug', '=', $slug)->get_one()) {
                       return Request::forge('frontend/adherent/view_page_by_slug/'.$slug, false)->execute();
                    }
    return Response::forge(ViewModel::forge('welcome/404'), 404);
    }
  • The 404 is handled via an exception and launching a new request, so you no longer have access to the original URI. But that will work fine...

Howdy, Stranger!

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

In this Discussion