Browse Source

Add Url array methods.

dereuromark 9 years ago
parent
commit
f7a1652877
2 changed files with 41 additions and 10 deletions
  1. 18 1
      docs/Url/Url.md
  2. 23 9
      src/View/Helper/UrlHelper.php

+ 18 - 1
docs/Url/Url.md

@@ -6,7 +6,7 @@ There is both a component and helper that help to work around some URL issues.
 If you need to merge in defaults to your URLs, you can get the information from the `defaults()` method:
 
 ```php
-// From inside a plugin controller
+// From inside a plugin controller action
 $$this->redirect(['controller' => 'Main', 'action' => 'index'] + $this->Url->defaults());
 ```
 It will basically add in `'prefix' => false, 'plugin' => false`.
@@ -17,14 +17,31 @@ You can in that case also just use the convenience method:
 $url = $this->Url->reset(['controller' => 'Main', 'action' => 'overview']);
 ```
 
+In case you just want the array (to pass it on), use:
+```php
+// Inside an action
+$urlArray = $this->Url->resetArray(['controller' => 'Main', 'action' => 'overview']);
+return $this->redirect($urlArray);
+```
+
 Inside `/admin/plugin-name/example/action` the URL to redirect to would normally become `/admin/plugin-name/main/overview`.
 With the reset() method it will become the desired: `/main/overview`.
 
+For the controller
+
 ### Complete
 In both cases, however, the query strings are not passed on. If you want that, you can use the other convenience method:
 ```php
 $url = $this->Url->complete(['controller' => 'Main', 'action' => 'overview']);
 ```
+
+In case you just want the array (to pass it on), use:
+```php
+// Inside an action
+$urlArray = $this->Url->completeArray(['controller' => 'Main', 'action' => 'overview']);
+return $this->redirect($urlArray);
+```
+
 Now if there was a query string `?q=x` on the current action, it would also be passed along as `/main/overview?q=x`.
 
 

+ 23 - 9
src/View/Helper/UrlHelper.php

@@ -19,10 +19,33 @@ use Cake\View\Helper\UrlHelper as CoreUrlHelper;
 
 /**
  * Url Helper class.
+ *
+ * @author Mark Scherer
+ * @license MIT
  */
 class UrlHelper extends CoreUrlHelper {
 
 	/**
+	 * @param array $url
+	 * @return array
+	 */
+	public function resetArray(array $url) {
+		$url += $this->defaults();
+
+		return $url;
+	}
+
+	/**
+	 * @param array $url
+	 * @return array
+	 */
+	public function completeArray(array $url) {
+		$url = $this->addQueryStrings($url);
+
+		return $url;
+	}
+
+	/**
 	 * Creates a reset URL.
 	 * The prefix and plugin params are resetting to default false.
 	 *
@@ -81,13 +104,4 @@ class UrlHelper extends CoreUrlHelper {
 		return $url;
 	}
 
-	/**
-	 * Event listeners.
-	 *
-	 * @return array
-	 */
-	public function implementedEvents() {
-		return [];
-	}
-
 }