Trongate Docs
switch to dark modeswitch to dark mode
»
»
Advanced Custom Routing

Advanced Custom Routing

There may be situations when you'd like to create custom page URLs.  For example, let's assume that you own an online shop that sells laptops.  A typical URL for a produce page might look something like this:

https://www.yoursite.com/store_items/display/88

Wouldn't it be nice if you could change these types of URLs so that they are:

  • More search engine friendly
  • Easier for visitors to remember

Custom routing makes this and more possible.  Let's go through a few different scenarios and explore how we could use custom routing to create attractive page URLs.

Scenario 1: 'News to Blog'

Challenge: You'd like to use a 'news' module from a previous project (we'll call it 'website A') to create a new blog for a second website ('website B').  However, you don't want the new blog module to contain URLs that start with 'news/'.  Instead, you'd like the URLs to start with 'my-blog/'.

More Info: Let's assume that the homepage for the 'news' module is:

http://yoursite.com/news

So, typing the URL shown above into the address bar would load the index method for the 'news' module.  Your goal might be to load the news module by going to:

http://yoursite.com/my-blog

How It's Done:

  • In your text editor, open the file 'custom_routing.php'.  It's inside the config/ directory.
  • Inside the custom_routing.php file, add a new key value pair to the $routes array where 'my-blog' is assigned to 'news'.  For example:

<?php
$routes = [
    'my-blog' => 'news'
    'tg-admin' => 'trongate_administrators/login',
    'tg-admin/submit_login' => 'trongate_administrators/submit_login'
];
define('CUSTOM_ROUTES', $routes);

Did You Know?
The $routes array (inside custom_routing.php) contains an array of key value pairs where each key refers to the (nice looking!) URL that you'd like to have and each value refers to endpoint that is to be loaded when the left side is typed into the address bar.

Top Tip
When you're working with key, value pairs in PHP, think of the => (equals and greater than symbol) as meaning 'assigned to'.

Scenario 2: 'Dealing With Numeric Wildcards'

Did You Know?
In web development, a 'wildcard' is a string of characters that can be subject to change.  In this situation, we're visualising a URL where one of the segments contains a changeable value (namely, a product ID).

Challenge: You have an online shop that sells laptops.  All of the product pages in your online shop have a first segment of 'store_items/. For example:

http://yoursite.com/store_items/display/88

You'd like to change the URLs so that instead of the first segment saying 'store_items', the product pages should all have first segment of 'laptops'.  For example:

http://yoursite.com/laptops/display/88

How It's Done:

In custom_routing add the following key value pair to the $routes array:

'laptops/display/(:num)' => 'store_items/display/$1',

Did You Know?
The (:num) wildcard guarantees that the value being accepted from the URL will always be numeric.  If a non-numeric value is passed via the URL then the page won't load.
 

Scenario 3: 'Super SEO friendly URLs'

Did You Know?
There appears to be evidence supporting the proposition that Google punishes websites that contain commercial indicators in their URLs.  Examples of commercial indicators would be strings like:

  • view-item
  • product
  • store-items
  • add-to-cart
  • special-offer
  • your-basket

However, there appears to be evidence supporting the proposition that Google rewards higher search engine rankings to websites that contain informational indicators. Examples of informational indicators would be strings like:

  • information
  • review
  • info
  • learn
  • tutorial


Challenge: Sticking with our online shop example, let's imagine that you'd like to have product pages where all of the URLs are super search engine friendly.  So, instead of having a URL like:

http://yoursite.com/laptops/display/88

Perhaps you would prefer to have a URL like this:

http://yoursite.com/laptops/information/macbook

This kind of URL could rightfully be considered to be super search engine friendly.  Having an online shop with those kinds of URLs could be extremely valuable for anyone who had an interest in achieving a high search engine ranking.​

How It's Done:

The first step to creating a super search engine friendly URL, as described above, would involve giving all of our products a unique 'slug'.  In the Trongate Desktop App this gets call a 'URL column'.  It's a column in the database table that contains a URL friendly version of the product title.  If you use the Trongate Desktop app then this can be taken care of for you automatically by simply choosing a 'URL COLUMN' that is equal to the 'product title' column (when you're creating a new module).

choosing a URL column

Having created a unique slug (URL column value) for each product, we would then add the following key value pair to the $routes array, inside custom_routing.php:

'laptops/information/(:any)' => 'store_items/display/$1',

If you can follow the steps above then congratulations!  You now know how to build pages that are super search engine friendly and that load approximately twenty times faster than Laravel.  Best of all, your clients will think you're a genius after they've made it to the top of Google and recommended you to all of their friends!

Did You Know?
The (:any) wildcard accepts non-numeric strings as well as numeric strings.
 


HELP & SUPPORT

If you have a question or a comment relating to anything you've see here, please goto the Help Bar.

 
×