Routing
Requests, Routes, and URL Rewriting
Tatanka has a lot of stuff built in that is based on the incoming request.
Having an understanding of how the request and route relates to your layout file is important. Read creating a layout.
Making a Page
Making a page is simple. Just create a layout file for the page you want to create. For example if you wanted to create an /about page, you would create an about.php file in your layout directory. Tatanka handles the rest.
Building Applications Well
Part of building an application correctly is to separate all of your php code from your layout. Other than echoing values into the layout you shouldn't be doing any database or logic stuff in your layout files. Tatanka's app class handles all the controller logic in the GET and POST routers. It is suggested you build you app's child class in a similar manner, but there's nothing saying you have to.
GET Routes
GET routes are important because they are related to the layout file being served. Plus anything else you may want to happen when a page is requested but before your layout is parsed.
getRouter() Hook
When building modules the getRouter() function is a hook into the app class's getRouter. Below is an example of a module using the getRouter hook:
Reserved/Built-in GET Routes
Certain pages have certain responsibilities. Tatanka modules standardize some of the common page names and correlating functionality with the built-in GET routes. Each module has its own reserved routes/pages.
Route | Name | Description | Layout File Required |
---|---|---|---|
/account | User Account Page | This /account route is reserved for your user's account page. Tatanka automatically restricts this route to logged in user's only. | Yes |
/logout | User Logout | User is logged out, redirected to home page, and all sessions are destroyed. | No |
/admin/users | Admin Users | The /admin/users route is reserved for the admin user mgmt page. | Yes |
/admin/orders | Admin Orders | The /admin/orders route is reserved for the admin order mgmt page. | Yes |
GET: URL Rewriting
URL rewriting means that instead of yourapp.com/about.php?foo=bar&fizz=buzz, your app's URL will be yourapp.com/about/foo/bar/fizz/buzz. We do this because search engines and people like this :)
From creating a layout we know about the first part of the URL rewriting system. That being how the layout file is derived from the first part of the url. We call that the slug, its the part after the first slash. e.g. yourapp.com/about.
The second part of the URL rewriting system are the $_GET name and value pairs...
$_GET: Name & Value Pairs
The rest of the URL after the layout portion will build your name and value pairs. For example the URL yourapp.com/about/foo/bar/fizz/buzz will allow you to work with the following $_GET values:
<?php
// yourapp.com/about/foo/bar/this/that
$_GET['foo']; // = "bar"
$_GET['this']; // = "that"
?>
Does that make sense?
Try to imagine exploding apart the URL at the are slashes. The first section after the domain designates the template that will be served. Tatanka takes the rest of the URL sections, formats them as name/value pairs, and inserts them back into the $_GET and $_REQUEST array variables so you can work with them as you would expect to.
POST Routes
A general case for expecting a POST request is a form submission. Tatanka uses the reserved name action. The $_POST['action'] variable is used when submitting forms via POST to route the request into a desired controller function. The $_POST['action'] variable is generally passed via a hidden input field in the form named action.
Any other $_POST array values work as you would expect them to.
postRouter() Hook
When building modules the postRouter() function is a hook into the app class's getRouter. Below is an example of a module using the getRouter hook:
Reserved/Built-in Post Actions
Below are built-in $_POST['action'] routes in Tatanka's app class and modules:
Action | Description | Requires Inputs | Required Roles |
---|---|---|---|
adminlogin | User login, admin role required. | email, password | admin |
changepassword | |||
delete | Delete | ||
login | User login | email, password | |
register | User registration. | ||
updateaccount |
The Timeline of a Request
Below is the trace of a request through Tatanka function calls:
app@__construct() | The Tatanka app.class.php construct function is called first. |
app@autoLoad() | The Tatanka autoload function. |
app@processRequest() | Followed by the Tatanka app.class.php processRequest() function. |
app@router() | Then the Tatanka app router is called. |
app@getRouter() | Which calls the Tatanka Get router. |
app@postRouter() | Followed by the Tatanka Post router. |
childClass@__construct() | Finally your child class's construct function is called. Presumably your custom class's router will follow. |
Proper Routing
Do not use redirects in your class methods. The routers are where your app should delegate the path of your application's logic. Hence routers are is the proper place to put any redirects your app may require for GET and POST requests.
By maintaining your redirects in the routers it separates path logic from business logic. Your functions so that they can just return true or false, as they should. This helps created a more object-oriented application.
Updated less than a minute ago