Exemplo n.º 1
0
 public function testGetChecksum()
 {
     $this->route->add(null, 'anypattern')->tokens(['token' => 'pattern']);
     $this->route->addTokens(['token' => 'pattern']);
     $this->route->addPatterns(['pattern' => 'regex']);
     $this->assertEquals(md5(json_encode(['routes' => [0 => ['path' => 'anypattern', 'group' => '', 'method' => '', 'tokens' => ['token' => 'pattern']]], 'tokens' => ['token' => 'pattern'], 'patterns' => ['pattern' => 'regex']])), $this->route->getChecksum());
 }
Exemplo n.º 2
0
<?php

use Ignaszak\Router\Collection\Route;
use Ignaszak\Router\Host;
use Ignaszak\Router\UrlGenerator;
use Ignaszak\Router\Matcher\Matcher;
use Ignaszak\Router\Response;
include __DIR__ . '/vendor/autoload.php';
// Create Router instance to collect routes
$route = Route::start();
// Add new route
// First parameter: name (is not required but if is defined
// it must be unique for each defined routes).
// Second: pattern
// Third: http method (it is possible to compine all http methods e.g.:
// 'GET|POST', not required, if is empty - route match for all methods)
$route->add('test', '/test/(\\w+)/', 'GET');
// There are two more add methods:
$route->get('get', '/match/only/get');
$route->post('post', '/match/only/post');
// Add tokens
$route->add(null, '/tokens/{token1}/{token2}/')->tokens(['token1' => '(\\w+)', 'token2' => '(\\d+)']);
// It is possible to define global token avilable for all routes.
$route->addTokens(['slug' => '(\\w+)', 'user' => '(\\w+)', 'page' => '(\\d+)']);
// Define default values for global tokens
$route->addDefaults(['user' => 'Demo']);
// Add controller
$route->add('user', '/user/{user}/')->controller('UserController');
// Define controller from route
$route->add(null, '/test/{controller}/{action}')->controller('\\Namespace\\{controller}::{action}')->tokens(['controller' => '([a-zA-Z]+)', 'action' => '([a-zA-Z]+)']);
// Add attachment