Search code examples
phproutesfat-free-framework

How to get previous route name (pattern) in Fat Free Framework?


I'm trying to create simple language switcher for my site on Fat Free Framework. My logic is next:

  1. Create route /lang/@lang
  2. Inside the route set the language form PARAM.lang
  3. Reroute user to the same page

The main issue for me – how to get that previous page route name? I'll be appreciated for any help.


Solution

  • You need to track the previous page URL somehow. Here are 2 ways to do it:

    • add the current page URI to the lang switcher link, as a query string: /lang/@lang?uri={{ urlencode(@URI) }}
    • rely on the referer URL specified by the browser: $_SERVER['HTTP_REFERER']. Since this value is not 100 % reliable and may even not be set at all, you should make a quick check of its relevancy (same domain?) before using it, and fallback on the homepage otherwise.