Search code examples
phproutesfat-free-framework

Fat Free Framework routing cannot not differentiate parameters from URI


I am trying to create proper routing for all of the Categories, Subcategories and Single Pages.

My problem is that my Subdirectory Route and my Single Page Route are very much identical:

Routes

GET /@category_slug = CategoryController->CategorySlug
GET /@category_slug/@subcategory_slug = CategoryController->SubcategorySlug
GET /@category_slug/@template_slug = SingleProductController->SinglePageSlug

The categories controller handles everything that it needs to handle in relation to categories and subcategories, but clearly won't handle anything related to the single pages. What I mean is the route will not differentiate parameters from URI and will not recognise whether it is Subcategory or a Single page.

Here is a sample URI

example.com/MainCategory/Subcategory
example.com/MainCategory1/Subcategory1
example.com/MainCategory/SinglePage1
example.com/MainCategory/SsinglePage2 

Any Idea how to solve this problem?

Thanks in advance


Solution

  • The reason why the framework cannot differentiate the two routes is the same as for a human. How would you know if /foo/bar is a subcategory or a subpage?

    So you have to make the distinction clear in your URL structure. This is up to your imagination. Here are a few examples:

    ex1:

    • /foo
    • /foo/bar
    • /foo/bar/page/baz

    ex2:

    • /cat/foo
    • /cat/foo/bar
    • /page/baz

    ex3:

    • /foo
    • /foo/c/bar
    • /foo/p/baz

    ex4:

    • /foo
    • /foo/bar
    • /foo/baz.html << here the suffix helps distinguish subcategories from articles

    ex5:

    • /foo
    • /foo/bar
    • /foo/bar/baz << here we keep the subcategory level

    There's also the solution, as you found out yourself, to keep an ambiguous URL structure and have the controller guess if it should display a subcategory or a single page.

    GET /@category_slug/@slug = CategoryController->GuessSubcategoryOrSinglePage
    

    But I wouldn't advise it, for the following reasons:

    • maintenance: bad code readability
    • performance: one extra useless SQL call for each page display
    • SEO: search engines try to guess a lot of things from the directory structure. Here they would fail, exactly as the framework or a human would do.

    NB1: my personal preference goes to example #2, because giving each page its own URL makes it possible to have a page belonging to multiples categories without having to deal with duplicate content issues. Also it makes your life easier when you have to build a page URL (no need to build the categories hierarchy, no matter where you are in the code).

    NB2: don't focus too much on crafting "pretty urls" as most end users don't ever care about them.