Routes

Basic Routing

Routes for your plugin will be defined in the app/routes.php file. Herbert routes consist of a Name, URI and a Closure callback. The URI will be appended to the site url, for example: http://example.com/simple

Basic GET Route

$router->get([
    'as'   => 'simpleRoute',
    'uri'  => '/simple',
    'uses' => function()
    {
        return 'Hello World';
    }
]);

Basic POST Route

$router->post([
    'as'   => 'simpleRoute',
    'uri'  => '/simple',
    'uses' => function()
    {
        return 'Hello World';
    }
]);

Support for PUT, DELETE and PATCH

$router->put();
$router->delete();
$router->patch();

Accessing framework components within a closure

$router->get([
    'as'   => 'simpleRoute',
    'uri'  => '/simple',
    'uses' => function(\Herbert\Framework\Http $http)
    {
        return "This route was accessed through http {$http->method()} method";
    }
]);

Route Parameters

You can set route parameters in your URI by defining as {param}. These parameters then be accessed by your Closure or Controller as $param

$router->get([
    'as'   => 'userProfile',
    'uri'  => '/user/{id}',
    'uses' => function($id)
    {
        return "User: {$id}";
    }
]);

Routing To Controllers

Herbert allows you to not only route to closures, but also to controller classes, visit the documentation on Controllers for more details.

$router->get([
    'as'   => 'userProfile',
    'uri'  => '/user/{id}',
    'uses' => __NAMESPACE__ . '\Controllers\UserController@profile'
]);