Give us a second

GET DYNAMIC ROUTES

Create dynamic routes with GET

Dynamic routing with GET

Pages displaying items, products, profiles, usually pass variables in the URL. This allows users to copy and paste the link to the page, search engines to discover the pages of your application, and browsers to cache that page.

There are 2 ways to pass data in the URL. The first one is via "query strings" and the second option is via "search engine friendly URLS".

Query strings

Using variables via query strings is simple, but search engines don't really like this approach. Example: The website displays an item based on a size. The URL will look like this:


item.php?size=10

Search engine friendly URLS

A better approach is to use friendly URLs is to use PHP Router and create routes that are understood/liked by search engines. In the following example the route is clear. It points to an item where the size is anything we pass as the variable in the URL. The URL will look like this "items/size/10" or it could also be "items/size/25" and the route will be:


get('/item/size/$size', 'views/item.php');

Route to dynamic page

Creating a dynamic route just means that you decide what variables will be passed in the URL. In principle there is no limit to the number of variables, though there is a limit to the length of the URL (4 kb).

If a route is needed where a user based on the gender needs to be displayed and the file in charge of it is under a "users" folder. Two routes could be created. The first one which is shorter but will confuse search engines, and the second example, where the route is more explicit and clear.


// not a clear route - do not use it
get('/user/$gender', 'users/user.php');


// clear route - use this
get('/user/gender/$g', 'users/user.php');