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
$router->get([
'as' => 'simpleRoute',
'uri' => '/simple',
'uses' => function()
{
return 'Hello World';
}
]);
$router->post([
'as' => 'simpleRoute',
'uri' => '/simple',
'uses' => function()
{
return 'Hello World';
}
]);
$router->put();
$router->delete();
$router->patch();
$router->get([
'as' => 'simpleRoute',
'uri' => '/simple',
'uses' => function(\Herbert\Framework\Http $http)
{
return "This route was accessed through http {$http->method()} method";
}
]);
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}";
}
]);
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'
]);