添加多参数机制

deploy
caiyuzheng 2019-08-26 14:02:29 +08:00
parent 3126a9a8e1
commit 39d74fb001
2 changed files with 8 additions and 5 deletions

View File

@ -76,7 +76,13 @@ class Router
public function match($url)
{
foreach ($this->routes as $route => $params) {
echo "<pre>";
print_r($route);
echo "</pre>";
if (preg_match($route, $url, $matches)) {
echo "<pre>";
print_r($matches);
echo "</pre>";
// Get named capture group values
foreach ($matches as $key => $match) {
if (is_string($key)) {
@ -110,23 +116,18 @@ class Router
*/
public function dispatch($url)
{
$url = $this->removeQueryStringVariables($url);
$url = str_replace($this->prefix,"",$url);
if ($this->match($url)) {
$controller = $this->params['controller'];
$controller = $this->convertToStudlyCaps($controller);
$controller = $this->getNamespace() . $controller;
if (class_exists($controller)) {
$controller_object = new $controller($this->params);
$action = $this->params['action'];
$action = $this->convertToCamelCase($action);
if (preg_match('/action$/i', $action) == 0) {
$controller_object->$action();
} else {
throw new \Exception("Method $action in controller $controller cannot be called directly - remove the Action suffix to call this method");

View File

@ -22,6 +22,8 @@ $router = new Core\Router();
// Add the routes
$router->add('', ['controller' => 'Home', 'action' => 'index']);
$router->add('{controller}/{action}');
$router->add('{controller}/{action}/{paramater}');
$router->AddPrefix(\App\Config::ROUTE_PREFIX);
try{
//for prefight request