Routing

The purpose of websites is to respond to requests (home page, subpages, assets) and routing in Bear Framework makes this very easy. When you develop your application, you can define request handlers to process various request types. In the example below is shown a callback function that will be called if the pattern specified matches the request path. The response object (BearFramework\App\Response) returned by the callback will be sent to the client.

use BearFramework\App;

$app App::get();

// The callback specified will be executed when request path is '/' and response method is GET.
// The text "Hi" will be shown on the screen.
$app->routes->add('/', function() {
    return new 
App\Response('Hi');
});

// Will match GET http://example.com/products/ 
$app->routes->add('/products/', function() {
    
// ...
});

Routes are checked from top to bottom. If a matching route does not return an object of type BearFramework\App\Response, the following routes will be checked too. If there is no match, the framework will respond with an object of type BearFramework\App\Response\NotFound.

Path patterns

You can write exact match patterns (as the one in the example above) or use '?' and '*' to match everything. The '?' symbol matches specific path segment and '*' matches everything. To get the values in the path matched by '?' or '*' you can use the BearFramework\App\Request object. It provides methods to retrieve information about the request. Here is an example:

use BearFramework\App;

$app App::get();

// Defines a handler that matches paths starting with "/products/", ending with "/" and something in between.
$app->routes->add('/products/?/', function(App\Request $request) {
    
$value $request->path->getSegment(1); // Gets the value in the second path segment.
    
return new App\Response\HTML('Product: ' $value);
});

// Will match GET http://example.com/products/iphone/6s/
$app->routes->add('/products/?/?/', function() {
    
// ...
});

// Will match: 
// GET http://example.com/stores/
// GET http://example.com/stores/new-york/
// GET http://example.com/stores/california/
// GET http://example.com/stores/california/working-hours/
$app->routes->add('/stores/*', function() {
    
// ...
});

// Will match every path
$app->routes->add('*', function() {
    
// ...
});

Request method matching

The routing feature supports matching different methods (GET, POST, DELETE, etc). You can specify one or more HTTP verbs in front of the path pattern.

use BearFramework\App;

$app App::get();

// Will match POST request for for http://example.com/
$app->routes->add('POST /', function() {
    
// ...
});

// Will match GET and HEAD requests for http://example.com/products/
$app->routes->add('GET|HEAD /products/', function() {
    
// ...
});

Learn more

Retrieve information about the HTTP request
Store and retrieve data
Store frequently used data for later usage

The information on this page is created for version 1.1 of Bear Framework and may not be applicable for other versions of the framework.