Jelajahi Sumber

+ RouteBase::link static method

Piotr Labudda 6 tahun lalu
induk
melakukan
11cef9c454
1 mengubah file dengan 25 tambahan dan 2 penghapusan
  1. 25 2
      SE/se-lib/RouteBase.php

+ 25 - 2
SE/se-lib/RouteBase.php

@@ -23,13 +23,36 @@ class RouteBase {
 		die("default task not implemented");
 	}
 
-	public function getUrlRouteName() {
+
+	static function link($task = '', $args = null) {
+		$routeName = self::urlRouteName();
+		if (empty($args)) return Request::getPathUri() . "index.php?_route={$routeName}" . (!empty($task) ? "&_task={$task}" : "");
+		if (is_string($args)) {
+			return Request::getPathUri() . "index.php?_route={$routeName}" . (!empty($task) ? "&_task={$task}" : "") . ltrim($args, '&');
+		}
+		if (is_array($args)) {
+			$urlArgs = [];
+			$uniqArgs = [];
+			if (!empty($task)) $uniqArgs['_task'] = $task;
+			foreach ($args as $name => $val) $uniqArgs[$name] = $val;
+			foreach ($uniqArgs as $name => $val) $urlArgs[] = "{$name}={$val}";
+			return Request::getPathUri() . "index.php?_route={$routeName}" . (!empty($urlArgs) ? '&' . implode('&', $urlArgs) : '');
+		}
+		throw new Exception("Not Implemented args type", 501);
+	}
+	static function urlRouteName() {
+		$clsName = get_called_class(); // get_class($this);
+		if ('Route_' != substr($clsName, 0, strlen('Route_'))) throw new Exception("Wrong route class name '{$clsName}'");
+		return substr($clsName, strlen('Route_'));
+	}
+
+	public function getUrlRouteName() { // TODO: return self::urlRouteName()
 		$clsName = get_class($this);
 		if ('Route_' != substr($clsName, 0, strlen('Route_'))) throw new Exception("Wrong route class name '{$clsName}'");
 		return substr($clsName, strlen('Route_'));
 	}
 
-	public function getLink($task = '', $args = null) {
+	function getLink($task = '', $args = null) {
 		$routeName = $this->getUrlRouteName();
 		if (empty($args)) return Request::getPathUri() . "index.php?_route={$routeName}" . (!empty($task) ? "&_task={$task}" : "");
 		if (is_string($args)) {