Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tina4:basic-routing [2021/07/16 15:25]
clayton added instructions to add
tina4:basic-routing [2022/07/18 19:19] (current)
nealryan
Line 7: Line 7:
 Consider the following folder structure and the routes you can hit up in the browser. Consider the following folder structure and the routes you can hit up in the browser.
  
-  * src/templates/index.html ->  [[http://localhost:7145/]] or [[http://localhost:7145/index]] +^ Directory      ^ URL       ^ URL 2          ^ 
-  src/templates/store/index.html -> [[http://localhost:7145/store]] or [[http://localhost:7145/store/index]] +src/templates/index.html    [[http://localhost:7145/]]    [[http://localhost:7145/index]]       | 
-  src/templates/store/shop.html -> [[http://localhost:7145/store/shop]]+src/templates/store/index.html    [[http://localhost:7145/store]] [[http://localhost:7145/store/index]]       | 
 +src/templates/store/shop.html    [[http://localhost:7145/store/shop]]     | 
  
 <WRAP center round info> <WRAP center round info>
Line 19: Line 20:
 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. 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+Try placing the following code in a php file of your choice in the routes folder.
  
-<code php src/routes/someroute.php>+<code php someroute1.php>
 <?php <?php
-\Tina4\Get::add ("/test/route", function(\Tina4\Response $response) { + 
-    return $response ("Hello World!");+\Tina4\Get::add ("/test/route", function(\Tina4\Response $response, \Tina4\Request $request) { 
 +    return $response ("Hello World!", HTTP_OK, TEXT_HTML);
 }); });
 </code>   </code>  
  
-Now lets test it by spinning up a web-server & hitting up [[http://localhost:7145/test/route]]  +Now lets test it by spinning up a built in web-server
 ===== Spinning Up A Web-Server ===== ===== Spinning Up A Web-Server =====
-Spin up a web-server by entering one of these two commands+Spin up a web-server by entering : 
  
 <code bash> <code bash>
 composer start composer start
 </code> </code>
- OR + 
-\\ +Hit up [[http://localhost:7145/test/route]] in your Web Browser ( e.g. FireFoxChrome etc). 
-<code bash> + 
-php -S localhost:7145 index.php  +===== Various Routing Examples =====
-</code> +
-\\ +
-Ensure you are using the correct port number above as per your  serverenv, mail settings etc. +
 \\ \\
 A route with inline parameters can be composed as follows A route with inline parameters can be composed as follows
-<code php src/routes/someroute.php>+<code php someroute2.php>
 <?php <?php
-\Tina4\Get::add ("/test/route/{name}",function($name, \Tina4\Response $response) { + 
-  return $response ("Hello {$name}!");+\Tina4\Get::add ("/test/route/{name}",function($name, \Tina4\Response $response, \Tina4\Request $request) { 
 +  return $response ("Hello {$name}!", HTTP_OK, TEXT_HTML);
 }); });
 </code>   </code>  
  
 **A POST ROUTE looks as follows and will need a FORM KEY token to accept POSTS to it for CORS prevention** **A POST ROUTE looks as follows and will need a FORM KEY token to accept POSTS to it for CORS prevention**
-<code php src/routes/someroute.php>+<code php someroute3.php>
 <?php <?php
 +
 \Tina4\Post::add ("/test/post", function(\Tina4\Response $response, \Tina4\Request $request) { \Tina4\Post::add ("/test/post", function(\Tina4\Response $response, \Tina4\Request $request) {
-    return $response ("Hello {$request->param["someInput"]}!");+    return $response (["Hello{$request->params['someInput']}!"], HTTP_OK, TEXT_HTML);
 }); });
 </code> </code>
  
 ** Routing directly to a class ** ** Routing directly to a class **
-<code php src/routes/someroute.php>+<code php someroute4.php>
 <?php <?php
 +
 \Tina4\Get::add("/test", ["TestClass", "someRouter"]); \Tina4\Get::add("/test", ["TestClass", "someRouter"]);
        
Line 70: Line 72:
    }    }
 } }
- 
 </code> </code>
-<WRAP center round info> 
-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}}   
-</WRAP> 
- 
Powered by ComboStrap