route() public method

Bind handler to route pattern
public route ( $pattern, $handler, $ttl, $kbps ) : null
$pattern string|array
$handler callback
$ttl int
$kbps int
return null
示例#1
0
 public function on($event, $pattern, $handler)
 {
     $bak = $this->f3->ROUTES;
     $this->f3->ROUTES = array();
     $this->f3->route($pattern, $handler);
     $this->routes[$event] = $this->f3->ROUTES;
     $this->f3->ROUTES = $bak;
 }
示例#2
0
 function __construct()
 {
     $this->f3 = \Base::instance();
     $config = $this->f3->get('MULTILANG');
     //languages definition
     if (!is_array(@$config['languages'])) {
         user_error(self::E_NoLang, E_USER_ERROR);
     }
     foreach ($config['languages'] as $lang => $locales) {
         if (is_array($locales)) {
             $locales = implode(',', $locales);
         }
         if (!$this->languages) {
             $this->f3->set('FALLBACK', $locales);
             $this->primary = $lang;
         }
         $this->languages[$lang] = $locales;
         $this->rules[$lang] = array();
     }
     //aliases definition
     $this->_aliases = $this->f3->get('ALIASES');
     if (is_array(@$config['rules'])) {
         foreach ($config['rules'] as $lang => $aliases) {
             $this->rules[$lang] = $aliases;
         }
     }
     //global routes
     if (isset($config['global'])) {
         if (!is_array($config['global'])) {
             $config['global'] = array($config['global']);
         }
         $prefixes = array();
         foreach ($config['global'] as $global) {
             if (@$global[0] == '/') {
                 $prefixes[] = $global;
             } else {
                 $this->global_aliases[] = $global;
             }
         }
         if ($prefixes) {
             $this->global_regex = '#^(' . implode('|', array_map('preg_quote', $prefixes)) . ')#';
         }
     }
     //migration mode
     $this->migrate = (bool) @$config['migrate'];
     //detect current language
     $this->detect();
     //rewrite existing routes
     $this->rewrite();
     //root handler
     $self = $this;
     //PHP 5.3 compatibility
     $this->f3->route('GET /', @$config['root'] ?: function ($f3) use($self) {
         $f3->reroute('/' . $self->current);
     });
 }
示例#3
0
<?php

require_once __DIR__ . '/../vendor/autoload.php';
$f3 = new Base();
//main page
$f3->route('GET /', function () {
    include 'main.php';
});
// Get the product lists
$f3->route('GET /example', function () {
    include 'example1.php';
});
//add a new product to all stores
$f3->route('GET|POST /new', function () {
    include 'newproducts.php';
});
//update the stock of a given product
$f3->route('GET|POST /products/*', function () {
    include 'update.php';
});
$f3->route('GET /brew/@count', function ($f3, $params) {
    echo $params['count'] . ' bottles of beer on the wall.';
});
$f3->run();