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/12 11:01]
andre
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>  
  
-Test it by hitting up [[http://localhost:7145/test/route]]  +Now lets test it by spinning up a built in web-server 
 +===== Spinning Up A Web-Server ===== 
 +Spin up a web-server by entering 
  
-A route with inline parameters can be composed as follows+<code bash> 
 +composer start 
 +</code>
  
 +Hit up [[http://localhost:7145/test/route]] in your Web Browser ( e.g. FireFox, Chrome etc).
  
-<code php src/routes/someroute.php>+===== Various Routing Examples ===== 
 +\\ 
 +A route with inline parameters can be composed as follows 
 +<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
  
Line 60: 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