# Routing

All your application routes are inside routes/api.php.

First we create a router and then we define all the application routes.

<?php

$router = new Devlob\Routing\Router();

$router->get('/', 'Home\PagesController@home');
1
2
3
4
5

# Basic routing

The most basic routing with Tea accepts a closure:

$router->get('/hello', function() {
    return 'Hello world!';
});
1
2
3

# Available methods

With Tea you can use 4 routing methods, get, post, put, and delete:

$router->get($uri, $callback);
$router->post($uri, $callback);
$router->put($uri, $callback);
$router->delete($uri, $callback);
1
2
3
4

# You can return anything

Views, JSON, Text, Operations, Resources, Collections, etc.:

$router->get('/view', function() {
    return view('pages.home', ['name' => 'My awesome view']);
});

$router->get('/json', function() {
    return json(['creator' => 'Renato Hysa']);
});

$router->get('/text', function() {
    return 'Hello world!';
});

$router->get('/operations', function() {
    return 1 + 1;
});

$router->get('/resource', function() {
    $user = User::find(1);
    
    return new UserResource($user);
});

$router->get('/collection', function() {
    $users = User::all();
    
    return new UserCollection($users);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# Route parameters

You can define route parameters with double curly braces.

The name of the parameter doesn't matter as long as you follow the order:

$router->get('/users/{id}', function($id) {
    return $id;
});
1
2
3

# Controller callback

You can pass a controller along with the action as a callback:

$router->get('/users/{id}', 'UserController@show');
1

Show action inside UserController:

public function show($id)
{
    return $id;
}
1
2
3
4

# Route middlewares

You can define as many middlewares as you want for a route.

Middlewares will execute in the order you specify them.

In the example below, api will execute first and then isAdmin.

$router->get('/users/{id}', 'UserController@show')->middlewares(['api', 'isAdmin']);
1