User Tools

Site Tools


basic-routing

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!");
});

Test it by hitting up http://localhost:7145/test/route

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 validate

<?php

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

** Routing directly to a class

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

On twig templates you can add this simple filter to include a formToken for you as a hidden input

{{ "reason for token" | formToken | raw }}

Alternatively you can use the twig global variable

{{formToken}}  
basic-routing.txt · Last modified: 2021/05/28 08:02 by matthew