Other Helpful Features
The Trongate framework has some useful features that are not part of a particular helper file or class. Nevertheless, these features do form an essential part of the framework and they can assist in making web development faster. The following features can be invoked from within any module controller file or view file:
The Anchor Function
The 'anchor' function can be used to produce a clickable text link that leads to a URL that has been set relative to your BASE_URL.
For example, let's assume that you own the website yoursite.com. So, this means that your homepage might be available at http://www.yoursite.com. If you had a contact us form at http://www.yoursite.com/contact then you could use the anchor function to produce a clickable text link that takes visitors to the contact page. The syntax for doing this is:
<?= anchor('contact', 'Get In Touch') ?>
The anchor text function accepts up to four arguments. They are:
- $target_url ~ The URL that website visitors should be taken to when the text link is clicked.
- $text ~ The visible text that should appear inside the 'a' tags.
- $attributes(optional) ~ An array of key-value pairs that can be added onto the opening 'a' tag.
- $additional_code(optional) ~ An string of characters that can be added onto the opening 'a' tag.
PLEASE NOTE: The anchor function can be used to generate links to external websites. Simply add the full target URL as an argument and a valid link will be produced.
The IP Address Function
Another useful feature that's baked into the Trongate framework is the 'IP address' function. The IP Address Function can be used to return the IP address of whomever is accessing your webpage. The syntax for this function is as follows:
//assuming that this is a view file
Your IP Address is <?= ip_address() ?>.
Generating Random Strings
Trongate has an inbuilt make_rand_str() function that can be used to generate random strings. An example of how to use this is shown below:
$str = make_rand_str(32);
The numeric argument that has been passed in represents the length of the random string to be generated. If the boolean value of true is also passed into the make_rand_str() method, as a second argument, then the random string that's produced will be in upper case.
$code = make_rand_str(6, true);
echo $code; //displays something like 'A4WX28'
If you would like to get the file path (within your computer or server) that leads to your web application, you can do so by calling upon the APPPATH constant. For example:
<?= APPPATH ?>
The BASE_URL constant is also accessible throughout the Trongate framework. Use this if you would like to return the main URL of your website, as defined inside config.php. For example:
<?= BASE_URL ?>
The JSON Function
Trongate also comes with a built-in json() function for helping developers to see a visual representation of data that's available, either inside a controller or a view file. The syntax for the json() function is shown below:
The first argument (in this case, $data) represents the data to be displayed. Please note that the json() method can also contain a second argument of true. If true is passed into json(), as a second argument then a die(); statement is immediately executed after the data has been displayed and all PHP code on the page will stop running.
In the screenshot below, the json() method has been applied to a view file, on line two, to produce a visual summary of the contents of a $data array:
Here's a screenshot of the output from the code above. Notice the collection of nicely formatted data near the top left-hand side of the screenshot:
HELP & SUPPORT
If you have a question or a comment relating to anything you've see here, please goto the Help Bar.