-
-
Notifications
You must be signed in to change notification settings - Fork 4
Usage
To enable/disable a method add/remove the method from the .env file. HTTP methods not specified in .env file will return a 405 status code.
http_method=HEAD,GET,POST,PUT,DELETE
Register routes in app.php
// $destination: route to navigate to
// $handler : callback
$route->get($destination, $handler);
// Default route "/"
$route->get('', function() {
Index::get();
});
$route->post('/users', function() {
Index::post();
});
Variables in the routes take the format <type:var_name>
. The valid datatypes are
- integers
([0-9]+)
=<int:user_id>
- strings
(\S+)
=<string:username>
Variables are defined as such
$route->post('/users/<int:id>', function() {
Index::delete();
});
// Routes can contain multiple variables
$route->get('/group/<string:group_name>/<int:id>', function() {
Index::get();
});
Handlers for routes can be written within the function block where the route is defined but the preferred way for callback management is defining a controller in the Controllers/
directory. Controllers take this default template
require_once __DIR__ . '/BaseController.php';
class User extends BaseController
{
}
The BaseController
file needs to be imported and the class, extended in order to access certain methods. It is not compulsory to name methods to match the request type being handled.
Extending the BaseController
class gives access to the following methods
-
render
: returns templates
// $filename: the template to be rendered
// $data : variables to pass the template
render($filename, $data);
render('index.php', array("title" => "value", "group" => "group name"));
All templates passed to the render function MUST be in View/layout
directory. Variables passed to templates can be accessed from the template using its unique key.
<html>
<body>
This is the <?= $title ?> page
This is the <?php echo $group; ?> group
</body>
</html>
-
reqVar
: access variables supplied in the route
// $key: variable name supplied when creating the route
reqVar($key)
-
json
: returns json output
// $data : data to be json encoded
// $statusCode: HTTP response code
json($data, $statusCode);
json('User Created', 200);
-
xml
: returns xml output
// $data : data to be xml encoded
// $statusCode: HTTP response code
xml($data, $statusCode);
xml('User Created', 200);
app.php
: Route Definition
<?php
require __DIR__ . '/routes.php';
$route->get('/user/<int:id>', function() {
User::get();
});
$route->get('', function() {
User::get();
});
$route->submit();
Controller/User.php
: Callback Management
<?php
require_once __DIR__ . '/BaseController.php';
class User extends BaseController
{
public static function get()
{
render('user.php', array('title' => 'Index Page', "time" => time()));
}
public static function post()
{
$userId = reqVar('id');
... # perform logic
json('user data has been updated', 200);
}
}
View/layout/user.php
: Template
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= $title ?></title>
</head>
<body>
<h1> The current time is <?= $time ?> in milliseconds </h1>
</body>
</html>
- All requests are automatically logged to
Logs/server.log
- Displaying of errors is enabled by default. To deactivate change the value of
display_error
to0
in the .env file - Error pages (404, 405) can be found at
View/defaults
and the template can be modified