Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
username as first parameter
  • How can I make FuelPHP able to handle uri similar to Facebook, Github, Twitter where the first parameter is the username?

    e.g.
    facebook.com/username
    github.com/fuelphp
    twitter.com/theuser

    If I set the route:
    (:uid)(/:any)? => 'user/profile'

    it will capture all uri which are not listed in the routes.php before the rule above.
  • You can only do that with a front controller. You route every request to it (that matches your URI format), and have it check the parameters.

    If they match, you can use an HMVC call to a second controller to deal with the information, and if they don't match, do an HMVC call using the 'do not route' flag to see if there is a controller/method combination that matches the URI.

    The router has no mechanism to do a parameter lookup before making a routing descision. It's a feature we're thinking about for a future version.
  • I have a temporary solution for that which is used the _404_ route.
    action_404 will check whether user has logged in and then try to search the user id from the database.

    I'm doing something similar to facebook e.g. FB user, FB page, FB group and some other feature using the same user id index. If I use a front controller, it will somehow increase the delay when request a static page.
  • That is correct.

    To work around that you'll need routes for your other pages that are captured before the generic catch-all, but that's far from ideal either.

    Using the 404 as a catch-all is quite common, you'll only have to make sure it deals with real 404...
  • I found a way to do this.

    The routes.php can use Regex as the url format. So, assume that the user ID is a fixed 20 hex numbers, I can create a route like this:

    "([0123456789abcdef]{20})(/:any)?" => "front/check"

    So the front controller will do the job.
    But I'm doing some other way. I've changed to use an object instead of a controller. So I created a class Front ( classes/front.php ). The route then use a closure:

    "([0123456789abcdef]{20})(/:any)?" => function()
    {
        if (Front::check())
        {
            Front::forward();
        }
        else
        {
            throw new HttpNotFoundException();
        }
    }


    The only problem is, if the user id can be anything, like facebook, twitter, github, etc, user can use any name they want except system reserved keywords, then the route will have to check from the other way.
    Route will have to check the user name is not in the reserved keywords list:

    "([^system|app|user|page|something_else])(/:any)?" => function()
    {
        ...
    }


    So, I think that route should able to continue find the correct controller before throw an exception.

    Let say there is a controller user with action_home but not listed in routes.php because it will be able to access automatically. But Route will pass it to the Closure that check for user id. It will fail. So if somehow the Closure can tell the Route continue to find the correct controller, then it will work.

    If you can't get what I mean, just ask me.

Howdy, Stranger!

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

In this Discussion