Application - Router
#Router
#Overview
Routing is the process of taking a URL endpoint (that part of the URL, resource path on image as below) and decomposing it(query) into parameters to determine which module, route, and action of that controller should receive the request.

- http://www.yourdomain.com/show/ => `show/` is token by router
- http://www.yourdomain.com/show/124 => `show/124` is token by router
- http://www.yourdomain.com/show/?key1=value1&key2=value2 => `show/` is token by router
- http://www.yourdomain.com/show/value1/value2 => `show/value1/value2` is token by router
Routing from the other hand is usually not part of the web server. It is written in PHP in a way similar to what you did. There is no web server configuration, except the .htaccess you are using. Some servers such embedded web in PHP even omit any configuration - they redirect everything to index.php
Routing also accept the request methods such as GET, POST, DELETE, PUT etc. The CL framework supports method as below.
#HTTP support method and NOT support method
#How To Use
To write php code for routing, It SHOULD be in a route file as index.php.
#GET Method
You can add a route that handles only GET HTTP requests with the CL framework application’s get() method. The GET method of RestFull is for getting data from database. It accepts two arguments:
- The route pattern (with optional named placeholders)
- The route callback
// VISIT AT "http://yourdomain.com/helloworld" with the "GET" method
$this->router->get("helloworld", function () {
echo "Hello World";
});
// VISIT AT "http://yourdomain.com/helloworld/userid1" with the "GET" method
// VISIT AT "http://yourdomain.com/helloworld/testid1?key=1&key=2" with the "GET"
// VISIT AT "http://yourdomain.com/helloworld/goodid1?key1=value&key2=value2" with the "GET"
$this->router->get("helloworld/:id", function ($id) {
echo "Hello World" . $id;
});
#POST Method
You can add a route that handles only POST HTTP requests with the CL framework application’s post() method. The POST method of RestFull is for creating new data to database. It accepts two arguments:
- The route pattern (with optional named placeholders)
- The route callback
$this->router->post("helloworld", function () {
echo "Hello World";
// VISIT AT "http://yourdomain.com/helloworld" with the "POST" method
});
$this->router->post("helloworld/:id/:name", function ($id, $name) {
echo "ID : " . $id;
echo "NAME : " . $name;
// VISIT AT "http://yourdomain.com/helloworld/test/jack" with the "POST" method
});
#DELETE Method
You can add a route that handles only DELETE HTTP requests with the CL framework application’s delete() method. The DELETE method of RestFull is for deleting preview data on database. It accepts two arguments:
- The route pattern (with optional named placeholders)
- The route callback
$this->router->delete("aaaa", function () {
echo "Hello World";
// VISIT AT "http://yourdomain.com/aaaa" with the "DELETE" method
});
$this->router->delete("bbbb/:id", function ($id) {
echo "Hello World" . $id;
// VISIT AT "http://yourdomain.com/bbbb/abcd" with the "DELETE" method
});
$this->router->delete("bbbb/:id/:name", function ($id, $name) {
echo "Hello World" . $id;
echo "Hello World Name" . $name;
// VISIT AT "http://yourdomain.com/bbbb/userid@naver.com/jake" with the "DELETE" method
});
#PUT Method
You can add a route that handles only PUT HTTP requests with the CL framework application’s put() method. The PUT of RestFull method is for updating preview data on database. It accepts two arguments:
- The route pattern (with optional named placeholders)
- The route callback
$this->router->put("put", function () {
echo "Hello World";
// VISIT AT "http://yourdomain.com/put" with the "PUT" method
});
$this->router->put("put/:id", function ($id) {
echo "Hello World" . $id;
// VISIT AT "http://yourdomain.com/put/12" with the "PUT" method
});
#Full Example
index.php
<?php
require_once("/home/projects/my_project/vendor/autoload.php");
class MyTestIndex extends \CafeLatte\Core\BaseRoute implements \CafeLatte\Interfaces\RouterInterface
{
public function routing()
{
$this->router->get("", function () {
echo "Hello World!!! GET Request";
});
$this->router->post("", function () {
echo "Hello World!!! POST Request";
});
$this->router->put("", function () {
echo "Hello World!!! PUT Request";
});
$this->router->delete("", function () {
echo "Hello World!!! DELETE Request";
});
//DO NOT REMOVE
$this->router->run();
}
}
$framework = new MyTestIndex("/home/projects/my_project/cafelatte.json");
$framework->execute();
#Tutorial
To get more information, move to tutorial page, Click `Here`