Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Route for Vanity URL
  • Hi, Is there an easy way to use vanity URLs, without hacking or extending FUEL? e.g. <a href="http://www.twitter.com/username">http://www.twitter.com/username</a>; or <a href="http://www.twitter.com/username">http://www.twitter.com/username</a>/ Both should route to "profile" contoller's (just an example) "action_index" action with the "username" parameter. How this can be achieved, with fallback to 404 (and other controllers) if no "username" exists in database. Thanks!
  • You can use the 404 route as a catch all. Something like this:
    '404'	=> 'profiles/view',
    

    You'll have to access the arguements through Uri::string() or Uri::segment(1) though, they won't be given to your Controller_Profiles->view() method. And add a action_404 method as well to the same controller and put something like below in the view() method:
    $user = Model_Profile::find_by_username(Uri::segment(1));
    if (empty($user))
    {
        return $this->action_404();
    }
    

    Or if you put the action_404 method in another controller:
    Output::redirect('controllername/404');
    
  • Hi Jelmer, thank you very much indeed! :) It's great to see that FUEL does handle this behavior easily.
  • Saw this in the amiafucktard example earlier. Doesn't this do the same without hacking the 404? https://github.com/philsturgeon/amiafucktard.com/blob/master/fuel/app/config/config.php
    'routes'	=> array(
    		// This is the default route.  We use a "#" here so that we do not have any
    		// reserved routes.
    		'#'		=> 'profiles/random',
    		'404'	=> 'errors/404',
    
    		'profiles/(:any)' => 'profiles/$1',
    		'errors/(:any)' => 'errors/$1',
    		'(:any)' => 'profiles/view/$1'
    	),
    
  • Very different, even though they appear similar. The $route approach only allows the controllers which are defined in routing to be used. Because anything will match that :any rule (except the routes you define before it). The 404 approach, is not a hack - it is by design a catch-all as well. In such a case it will try 1. routes, 2. normal routing and only third will it fall back to the 404 route. Thus you won't need to manually register each new controller you create in your routes.

Howdy, Stranger!

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

In this Discussion