Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Subdomain wildcard routing
  • We have custom made web page which uses custom made routing based on wildcard subdomains string + other words behind / that are searched in database. How can be wildcard subdomain routing accomplished in fuelphp? I dont get it :)
    Eg. destination-persons.page.com
    destination and persons are seached in db and if they exists we show listing page for products.
    Thanks
  • As this is information in the hostname and not in the URI, standard FuelPHP routing can not deal with this. There are two options: - route all requests to a single controller method (page/search for example), have that get the hostname and from that the search terms
    - use mod_rewrite to convert destination-persons.page.com to index.php/page/search/destination/persons and use the standard URI methods
  • Alternatively, doesn't route rely on Uri to start with?
    Can't you extend Uri to work in that way?
  • Possible, but then you're basically going the 'mod_rewrite' route, but then in code. Extending a core class to hack this in whouldn't be my preferred approach. If you want to go that route it's better to modify $_SERVER in your index.php and have URI do it's thing on that modified value.
  • I like the solution with pointing everything to one controller method and if I assume there that the composition of subdomain and other URI parts do not comply with search page I call from that method standard URI routing and continue. Did I get it correctly?
  • If you take that approach you're already in a routed controller, the routing process has been completed at that point. If your controller decides the URI is not for him, you can only feed the URI back into the routing engine using an HMVC call. This call can not use routing rules, as that would mean you'll end up in your first controller method again. So if you want to go this route, you have to:
    - setup custom routes if you have them
    - end with a catch-all route to your controller method
    - if the search fails, do an HMVC call with the URI with disabled routing
    - throw a 404 if that call fails Only downside is that custom routes now go before your search, standard routes (the ones that directly map to a controller method) go after your search. Might not be an issue, but you have to be aware of this.
  • Thanks it sound complicated to me because I'm coming from plain php background and i developed custom web pages without frameworks for 8 years. Ill try what you have proposed.
  • Not that complicated. Make sure you're last route is:
    '(.*)' => 'catchall/index/$1', // route all to the catchall controller, index method
    

    In that index method you do your search thing. If not found, do an HMVC call with the URI:
    // assume $uri contains the URI you have retrieved
    try
    {
        return \Request::forge($uri, false)->execute();
    }
    catch (\HttpNotFoundException $e)
    {
        // request failed, deal with the 404 situation
    }
    

    You can also drop the try/catch if you want, in which case the default 404 handler in Fuel will deal with it.

Howdy, Stranger!

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

In this Discussion