Trongate Docs
switch to dark modeswitch to dark mode
»
»
Reading Values From The URL

Reading Values From The URL

Sometimes it's useful – or even essential – for web apps to be able to read values from the URL. The Trongate Framework offers two techniques by which values can be easily read from the website URL.

The segment() method

The first technique is to Trongate's built-in URL helper class to declare the part(s) of the URL that you wish to read.  As mentioned previously, Trongate uses a segment based approach to URLs.  In this instance, a segment is defined as a character or block of characters that appear in the URL after the base URL.

Segments are separated by the forward-slash character.

Below is an example of a page URL that has three segments:

https://trongate.io/first/second/third

To read the first segment of the URL and assign it to a variable, we could use Trongate's inbuilt segment method like so:

$first_segment = segment(1);


Doing this would mean that our $first_segment variable would be assigned with a value of 'first'.

Of course, the second segment could be read with:

$second_segment = segment(2);

..and the third with:

$third_segment = segment(3);

As you can see, the approach here involves simply calling the (in-built) segment() method and adding the number of the segment that you wish to fetch in parentheses.

Understanding The Segment Method

The segment() method accepts two arguments:

  • FIRST ARGUMENT (required) : an integer representing the segment number to be checked
  • SECOND ARGUMENT (optional) : a string that represents the type of variable that you expect to receive from the URL.

Top Tip
The optional second argument calls PHP's built-in settype() method to ensure that variables are converted to the type that is expected from the code. This is great for security!  For example, whenever you expect (let's say) an integer from a URL segment then pass in a second argument of 'int'.  Passing in a variable type will dramatically reduce your chances of having malicious code injected into your scripts via the URL.

$id = segment(3, 'int');

   

Just To Let You Know
In very early versions of Trongate the syntax for reading values from the URL was slightly different.  Older versions of Trongate used:

$var = $this->url->segment(3);

As you can see, the code being used today is a little more compact.

The Third Segment As An Argument

Whenever you create a method inside a Trongate class, Trongate will assume that the third segment of your current URL contains a value that is to be passed into your method.

For example. Consider the following URL:

example.com/items/display/88

Using Trongate's basic URL routing, this URL would:

  • load an 'items' module
  • load an 'items' class (from within the controllers folder)
  • invoke a method called 'display'
  • pass the value 88 into the display method as an argument

​Therefore, if our goal is to build a method that reads an ID from the URL we can two options.  The first is to use the segment() method, like so:

function display() {
    $id = segment(3);
    echo $id; //outputs: 88
}


Our second option would be to take advantage of Trongate's assumption that the third segment should be automatically passed into the method.  For example,

function display($id) {
    echo $id; //outputs: 88
}


HELP & SUPPORT

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

 
×