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.

Directory URL URL 2
src/templates/index.html http://localhost:7145/ http://localhost:7145/index
src/templates/store/index.html http://localhost:7145/store http://localhost:7145/store/index
src/templates/store/shop.html http://localhost:7145/store/shop

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, \Tina4\Request $request) {
    return $response ("Hello World!", HTTP_OK, TEXT_HTML);
});

Now lets test it by spinning up a built in web-server

Spinning Up A Web-Server

Spin up a web-server by entering :

composer start

Hit up http://localhost:7145/test/route in your Web Browser ( e.g. FireFox, Chrome etc).

Various Routing Examples


A route with inline parameters can be composed as follows

<?php

\Tina4\Get::add ("/test/route/{name}",function($name, \Tina4\Response $response, \Tina4\Request $request) {
  return $response ("Hello {$name}!", HTTP_OK, TEXT_HTML);
});

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->params['someInput']}!"], HTTP_OK, TEXT_HTML);
});

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");
   }
}
Powered by ComboStrap