This is an old revision of the document!


The Route of It

Tina4 supports 2 types of routes, those created in code and those that are assumed based on files you drop in the src/templates folder.

Dynamic Routing Using Files in src/templates

Consider the following folder structure and the routes you can hit up in the browser.

You can use either .twig or .html extensions. The html extension will cause the built in Tina4 template engine to be used.

Coded Routing in src/routes with subfolders

The folders are simply suggested folders where you can place php files to manage your routing. Any php file place in the routing folder and sub folders will automatically be parsed and run when Tina4 is hit up.

Try placing the following code in a php file of your choice in the routes folder

<?php
\Tina4\Get::add ("/test/route", function(\Tina4\Response $response) {
    return $response ("Hello World!");
});

Now lets test it by spinning up a web-server & hitting up http://localhost:7145/test/route

Spinning Up A Web-Server

Spin up a web-server by entering one of these two commands:

composer start

OR

php -S localhost:7145 index.php 


Ensure you are using the correct port number above as per your server, env, mail settings etc.

A route with inline parameters can be composed as follows

<?php
\Tina4\Get::add ("/test/route/{name}",function($name, \Tina4\Response $response) {
  return $response ("Hello {$name}!");
});

A POST ROUTE looks as follows and will need a FORM KEY token to accept POSTS to it for CORS prevention

<?php
\Tina4\Post::add ("/test/post", function(\Tina4\Response $response, \Tina4\Request $request) {
    return $response ("Hello {$request->param["someInput"]}!");
});

Routing directly to a class

<?php
\Tina4\Get::add("/test", ["TestClass", "someRouter"]);
   
class TestClass
{
   public function someRouter (\Tina4\Response $response, \Tina4\Request $request) {
     return $response("Hello");
   }
}

Go to the Twig documentation page if you would like to familiarize yourself with Twig.

Powered by ComboStrap