1 Star 6 Fork 1

DennisRitche / laravel-route-register

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Route.php 1.83 KB
一键复制 编辑 原始数据 按行查看 历史
13249808562 提交于 2019-11-27 23:24 . initial commit
<?php
class Route
{
private $rule;
private $rule_parsed = [];
private $request_method;
private $alias;
private $constraint = [];
private $action;
private $match_all = false;
private $params = [];
/**
* Route constructor.
* @param $method string
* @param $rule string
* @param $action callable|string
*/
public function __construct($method, $rule, $action)
{
$this->rule = $rule;
$this->request_method = $method;
$this->action = $action;
$this->parseRule();
}
private function parseRule()
{
$parts = explode("/", $this->rule);
if (count($parts) == 0) {
$this->match_all = true;
} else {
foreach ($parts as $part) {
if (preg_match('/^{([a-zA-Z]+)(\??)}$/', $part, $match) == 0) {
$this->rule_parsed[] = ['is_plain' => true, 'value' => $part];
} else {
$this->rule_parsed[] = ['is_plain' => false, 'is_optional' => isset($match[2]) && !empty($match[2]), 'param_name' => $match[1]];
}
}
}
}
public function where($name, $value = null)
{
if (is_array($name)) {
foreach ($name as $key => $value) {
$this->constraint[$key] = $value;
}
} else {
$this->constraint[$name] = $value;
}
}
public function name($alias)
{
$this->alias = $alias;
}
public function getParam($name, $default = null)
{
return $this->params[$name] ?? $default;
}
/**
* @return string
*/
public function getRule(): string
{
return $this->rule;
}
/**
* @return callable|string
*/
public function getAction()
{
return $this->action;
}
}
PHP
1
https://gitee.com/obamajs/laravel-route-register.git
git@gitee.com:obamajs/laravel-route-register.git
obamajs
laravel-route-register
laravel-route-register
master

搜索帮助