本文整理汇总了PHP中Router::add方法的典型用法代码示例。如果您正苦于以下问题:PHP Router::add方法的具体用法?PHP Router::add怎么用?PHP Router::add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Router
的用法示例。
在下文中一共展示了Router::add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: generateRoute
/**
* Add all the routes in the router in parameter
* @param $router Router
*/
public function generateRoute(Router $router)
{
foreach ($this->classes as $class) {
$classMethods = get_class_methods($this->namespace . $class . 'Controller');
$rc = new \ReflectionClass($this->namespace . $class . 'Controller');
$parent = $rc->getParentClass();
$parent = get_class_methods($parent->name);
$className = $this->namespace . $class . 'Controller';
foreach ($classMethods as $methodName) {
if (in_array($methodName, $parent) || $methodName == 'index') {
continue;
} else {
foreach (Router::getSupportedHttpMethods() as $httpMethod) {
if (strstr($methodName, $httpMethod)) {
continue 2;
}
if (in_array($methodName . $httpMethod, $classMethods)) {
$router->add('/' . strtolower($class) . '/' . $methodName, new $className(), $methodName . $httpMethod, $httpMethod);
unset($classMethods[$methodName . $httpMethod]);
}
}
$router->add('/' . strtolower($class) . '/' . $methodName, new $className(), $methodName);
}
}
$router->add('/' . strtolower($class), new $className(), 'index');
}
}
示例2: registerProviders
public function registerProviders($providers)
{
foreach ($providers as $provider) {
$collection = $provider->connect($this);
/** @var ControllerCollection $collection */
foreach ($collection->getRoutes() as $route) {
$this->router->add($route);
}
unset($collection);
}
}
示例3: getRouter
public function getRouter()
{
$router = new Router();
foreach ($this->getActionRoutes() as $methodRoute) {
list($route, $methodName) = $methodRoute;
$method = new \ReflectionMethod($this, $methodName);
$httpMethod = 'GET';
$hasRoute = false;
if ($comment = $method->getDocComment()) {
if (preg_match('~^[\\s*]*\\@method\\s([^\\s]+)~im', $comment, $match)) {
$httpMethod = trim(strtoupper(array_pop($match)));
}
if (preg_match('~^[\\s*]*\\@route\\s([^\\s]+)~im', $comment, $match)) {
$route = trim(array_pop($match));
$hasRoute = true;
}
}
if (!$hasRoute) {
foreach ($method->getParameters() as $parameter) {
$route .= '/{' . ($parameter->isOptional() ? '?' : '') . $parameter->getName() . '}';
}
}
$router->add($httpMethod, $route, [get_class($this), $methodName]);
}
return $router;
}
示例4: testMatchMethod
public function testMatchMethod()
{
$this->object->add(new \PHPixie\Route('get', '/url', array('controller' => 'home', 'action' => 'get'), 'GeT'));
$this->object->add(new \PHPixie\Route('post', '/url', array('controller' => 'home', 'action' => 'get'), array('PosT')));
$route = $this->object->match('/url', 'GEt');
$this->assertEquals('get', $route['route']->name);
$route = $this->object->match('/url', 'POST');
$this->assertEquals('post', $route['route']->name);
}
示例5: boot
/**
* initialize boot function
*/
static function boot()
{
// load config
\Config::load('extdirect', true);
// get route
$route = \Config::get('extdirect.route', 'direct');
// add routes
\Router::add($route . '/(:any)', 'extdirect/$1');
\Router::add($route, 'extdirect/index');
}
示例6: testBuild
public function testBuild()
{
$router = new Router();
$router->add("home", new Route("/"));
$router->add("article", new Route("/show/{title}"));
$router->add("mvc", new Route("/{controller}/{action}/{param}", array("controller" => "home", "action" => "index", "param" => "")));
$this->assertSame("/", $router->build("home", array()));
$this->assertSame("/", $router->build("home", array("something" => "else")));
$this->assertFalse($router->build("article", array()));
$this->assertFalse($router->build("article", array("something" => "else")));
$this->assertFalse($router->build("article", array("title" => "")));
$this->assertFalse($router->build("article", array("title" => false)));
$this->assertSame("/show/1", $router->build("article", array("title" => "1")));
$this->assertSame("/show/thisisatitle", $router->build("article", array("title" => "thisisatitle")));
$this->assertSame("/", $router->build("mvc", array()));
$this->assertSame("/", $router->build("mvc", array("foo" => "bar")));
$this->assertSame("/", $router->build("mvc", array("controller" => "")));
$this->assertSame("/", $router->build("mvc", array("controller" => false)));
$this->assertSame("/", $router->build("mvc", array("controller" => "home")));
}
示例7: testAddVariableWithRegexRoute
public function testAddVariableWithRegexRoute()
{
$patternBuilder = \Mockery::mock('Rootr\\PatternBuilder');
$patternBuilder->shouldReceive('build')->andReturn(['/products/(\\d+)', ['id']]);
$router = new Router($patternBuilder);
$router->add('GET', '/products/{id}', function ($id) {
return "/products/{$id}";
});
assertThat($router->getStaticRoutes(), emptyArray());
assertThat($router->getVariableRoutes(), arrayWithSize(1));
assertThat($router->getVariableRoutes(), hasKeyInArray('/products/(\\d+)'));
}
示例8: __construct
/**
* Constructor of Sask, initialization of the database class.
*
* @param string $baseDir The base directory of the application
*/
public function __construct($baseDir = __DIR__)
{
self::$basedir = $baseDir;
self::$router = new Router();
/*
* Connect to the database
* ==================
* Set your database access details here.
*/
if (Configuration::$dbConnection['UseDatabase']) {
switch (Configuration::$dbConnection['Type']) {
case 'MySQL':
self::$database = new Database(Configuration::$dbConnection['Host'], Configuration::$dbConnection['User'], Configuration::$dbConnection['Password'], Configuration::$dbConnection['DatabaseName'], Configuration::$dbConnection['Port']);
break;
default:
break;
}
}
foreach (Configuration::$routes as $key => $value) {
self::$router->add($key, $value);
}
}
示例9: testNamedParams
public function testNamedParams()
{
$id = null;
$name = null;
Router::add('|^api/\\w+/(?<name>\\w+)/(?<id>\\d+)$|', function ($r) use(&$id, &$name) {
$id = (int) $r->param('id');
$name = $r->param('name');
return new Result(['id' => $id]);
});
$reflection = new \ReflectionMethod('alkemann\\h2l\\Router', 'matchDynamicRoute');
$reflection->setAccessible(true);
$route = $reflection->invoke(null, 'api/doesntmatter/tasks/12');
$this->assertTrue($route instanceof Route);
$this->assertEquals(['id' => '12', 'name' => 'tasks'], $route->parameters);
}
示例10: testBuildWithPathType
public function testBuildWithPathType()
{
$router = new Router();
$router->add("test", new Host("/", array()));
$this->assertSame("/", $router->build("test", array(), Host::PATH_ONLY));
$this->assertSame("/", $router->build("test", array(), Host::PATH_NETWORK));
$this->assertSame("/", $router->build("test", array(), Host::PATH_FULL));
// with _host
$this->assertSame("/", $router->build("test", array("_host" => "example.com"), Host::PATH_ONLY));
$this->assertSame("//example.com/", $router->build("test", array("_host" => "example.com"), Host::PATH_NETWORK));
$this->assertSame("//example.com/", $router->build("test", array("_host" => "example.com"), Host::PATH_FULL));
// with _scheme, no _host
$this->assertSame("/", $router->build("test", array("_scheme" => "https"), Host::PATH_ONLY));
$this->assertSame("/", $router->build("test", array("_scheme" => "https"), Host::PATH_NETWORK));
$this->assertSame("/", $router->build("test", array("_scheme" => "https"), Host::PATH_FULL));
// with _scheme and _host
$this->assertSame("/", $router->build("test", array("_host" => "example.com", "_scheme" => "https"), Host::PATH_ONLY));
$this->assertSame("//example.com/", $router->build("test", array("_host" => "example.com", "_scheme" => "https"), Host::PATH_NETWORK));
$this->assertSame("https://example.com/", $router->build("test", array("_host" => "example.com", "_scheme" => "https"), Host::PATH_FULL));
}
示例11: Router
<?php
namespace Epic;
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);
define('SITE_URL', 'http://epic-blog/lesson%2010/src/public/index.php');
require '../vendor/autoload.php';
Lib\connection(['host' => 'localhost', 'dbname' => 'blog', 'user' => 'root', 'password' => 'vagrant', 'encoding' => 'utf8']);
$router = new Router();
$router->add('home', '\\Epic\\Controllers\\Home', ['\\Epic\\Lib\\is_logged']);
$router->add('profile', '\\Epic\\Controllers\\Profile', ['\\Epic\\Lib\\is_logged']);
$router->add('login', '\\Epic\\Controllers\\Login');
$router->handle();
示例12: error_reporting
<?php
/**
* BakedCarrot application initialization file
*
* @package BakedCarrot
*
*
*
*
*/
// turning on error reporting
error_reporting(E_ALL | E_STRICT);
// loading main library file
require SYSPATH . 'App.php';
// init
App::create(array('config' => APPPATH . 'config.php', 'mode' => App::MODE_DEVELOPMENT));
// default route
Router::add('default', '/', array('controller' => 'index'));
// run the application
App::run();
示例13: error_reporting
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require 'app/helpers.php';
require 'app/core/DB.php';
require 'app/core/Router.php';
require 'app/core/Model.php';
require 'app/core/Controller.php';
require 'app/models/Candidate.php';
require 'app/controllers/CandidatesController.php';
Router::add('GET', 'customers', 'CustomersController');
Router::add('GET', 'companies', 'CompaniesController');
Router::test();
$controllerName = $_GET['controller'] . 'Controller';
$actionName = $_GET['action'];
$controller = new $controllerName();
$controller->{$actionName}();
示例14:
<?php
Router::add('/', DIR_CTRL . '/index.php');
// Router::add('#^/products(/\d+)?/$#', DIR_CTRL.'/products.php', Router::ROUTE_PCRE);
// Router::add('#^/index.php/products(/\d+)?/$#', DIR_CTRL.'/products.php', Router::ROUTE_PCRE);
Router::add('#^/index.php/products/(name|price|latestprice|addedon|lastupdate)(/\\d+)?/$#', DIR_CTRL . '/products.php', Router::ROUTE_PCRE);
Router::add('#^/index.php/products/$#', DIR_CTRL . '/products.php', Router::ROUTE_PCRE);
Router::add('#^/index.php/prcchanged/#', DIR_CTRL . '/prcchanged.php', Router::ROUTE_PCRE);
Router::add('#^/index.php/product(/\\d+)?/$#', DIR_CTRL . '/product.php', Router::ROUTE_PCRE);
Router::add('#^/index.php/settings/$#', DIR_CTRL . '/settings.php', Router::ROUTE_PCRE);
// Router::add('#^/index.php/ajax/$#', DIR_CTRL.'/ajax.php', Router::ROUTE_PCRE);
// Router::add('#^/regex/(test1|test2|test3)/$#', DIR_CTRL.'/regex.php', Router::ROUTE_PCRE);
/**
* Routes are added with the static method Router::add($pattern, $replacement)
* It is processed as preg_replace($pattern, $replace) in the router class, so
* use any style for $pattern. Though it would be best to use # for pattern
* delimiters and ${n} for the replacement string variables. To carry a string
* from the pattern, just put them in parentheses (). These are run in order,
* and first one that matches and has a readable controller file is used.
*
* PHP's preg_replace: http://php.net/preg_replace/
*
* examples:
*
* Router::add('#/#', DIR_CTRL.'index.php', Router::ROUTE_PCRE);
* sends index page to the index.php contoller
*
* Router::add('#/news/(archive|latest)/#', DIR_CTRL.'news.${1}.php', Router::ROUTE_PCRE);
* /news/archive/ goes to news.archive.php
*
* you can also do this
示例15: Router
<?php
$router = new Router();
$router->add('/', 'i want to see the dashboard');
$router->get('/');