Routing in Tina4

Please see the How Do I - Beginner section for more basics on Routes.

For further information on routing, please click on this link.

Simple Syntax

The simple syntax of a Route is as follows

<?php

\Tina4\Method::add(string $routePath, $function)


So the Route to capture the URL https://mywebsite.com/starthere would be as below. This uses a Get method, with a simple closure function, with the return using the Response object.

<?php

\Tina4\Get::add("/starthere", function(\Tina4\Response $response, \Tina4\Request $request) {
  // Put all the code that you want to here
  return $response("Hello Mars", HTTP_OK, TEXT_HTML);
});

Route Methods

There are a number of routing options in Tina4, they are based around the common API routing methods.

  • GET - TINA4_GET
  • POST - TINA4_POST
  • PATCH - TINA4_PATCH
  • DELETE - TINA4_DELETE
  • PUT - TINA4_PUT

A further route is introduced which encapsulates all the above routes and intercepts all the different options

  • ANY. - TINA4_ANY

So by using the Get & Post method we get two unique Routes.

<?php

Get::add("/starthere", $function) //--> this will retrieve data sent back and forth between your device and servers
||
Post::add("/starthere", $function) //--> this secures the retrieved data sent back and forth between your device and servers

Including Variables

Variables can be included in three different ways.

Path variables

Variables can be included as part of the actual URL path and passed to the front of the closure function.

mywebsite.com/starthere/3 will return 3

<?php

\Tina4\Get::add("/starthere/{variable1}", function($variable1, \Tina4\Response $response, \Tina4\Request $request) {
  // Use the variables in your code or as the response
  return $response($variable1, HTTP_OK, TEXT_HTML);
});


mywebsite.com/starthere/3/nextlevel/5 will return 15

<?php

\Tina4\Get::add("/starthere/{variable1}/nextlevel/{variable2}",
function($variable1, $variable2, \Tina4\Response $response, \Tina4\Request $request)) {
  // Use the variables in your code or as the response
  $product = $variable1 * $variable2;
  return $response($product, HTTP_OK, TEXT_HTML);
});
 


WARNING: Two routes with the same method, mywebsite.com/starthere/{variable1} and mywebsite.com/starthere/anotherroute are in danger of clashing. In this case it is important to declare the definitive route before the route with the variable. If the definitive route is declared after the variable, then “anotherroute” will end up in variable1 with almost certainly undesired results.

Inline Variables

Variables can also be included as query parameters in the url. The Request object packages these up into the params array. So mywebsite.com/starthere?variable1=3&variable2=5 will return 15

<?php

\Tina4\Get::add("/starthere", function(\Tina4\Response $response, \Tina4\Request $request) {
  // Variables are available from the params array
  $variable1 = $request->params["variable1"];
  $variable2 = $request->params["variable2"];

  $product = $variable1 * $variable2;
  return $response($product, HTTP_OK, TEXT_HTML);
});
 


Form data

Data posted from a form to a Post Route can be accessed from the Request data object. So a form submitted with two fields of a person's name “Joe” “Smith” will return “Hello Joe Smith”

<?php

\Tina4\Post::add("/endhere", function(\Tina4\Response $response, \Tina4\Request $request) {
  // Variables are available from the params array
  $firstName = $request->data->firstName;
  $lastName = $request->data->lastName;

  $message = "Hello ". $firstName . " ". $lastName;
  return $response($message, HTTP_OK, TEXT_HTML);
});


Securing Routes

Tina4 makes use of phpDoc blocks through the Annotation class. This in combination with the Auth class allows us to secure routes with a single line of code. The @secure enables the Tina4\Auth class, which will now require at least a formToken, or can even make use of custom Bearer tokens.

<?php

/**
* @secure
**/
\Tina4\Get::add("/starthere", function(\Tina4\Response $response, \Tina4\Request $request) {
  // Put all the code that you want to here
  return $response("Hello Mars", HTTP_OK, TEXT_HTML);
});


For more information on Tina4 Security, please see the Reference Section on Tina4 Auth

The Response Object

The response object is an integral part of a Route. The Route object takes three parameters. Basic syntax is $response($content, $httpCode, $contentType).

The return from the Response object will always be a string, if the $content is an array or object it will be converted to a JSON or XML response.

The httpCode is a standard HTTP Response Status Code.

The contentType is a standard HTTP Mime Type, indicating what kind of content you are returning.

It is highly recommended to ALWAYS set both the $httpCode and the $contentType to ensure that your intentions are clear in terms of what kind of content you are sending and your view on it's relative success.

Ruth - Legacy Routes

This documentation is for Ruth, the legacy route mechanism. Please only use to maintain older projects

The basic syntax for Ruth Routes bear much resemblance to current Tina Routes.

<?php

Ruth::addRoute(TINA4_GET, "/hello/world", function(){
  echo "Hello World!";
});


And also includes Path variables

<?php

Ruth::addRoute(TINA4_GET, "/content/{name}", function($name){
  echo "OK {$name}!";
});


Ruth can also be used much like Tina, taking advantage of the Request and Response objects

<?php

Ruth::get("/hello/world", function(\Tina4\Response $response, \Tina4\Request $request) {
   return $response ("Hello World!", HTTP_OK, TEXT_HTML);
});

Ruth::get("/hello/{name}", function($name, \Tina4\Response $response, \Tina4\Request $request) {
   return $response ("Hello {$name}!", HTTP_OK, TEXT_HTML);
});
Powered by ComboStrap