From ed092bf6cd6d12980b310fc36b466c7ccf6ba861 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 29 Nov 2011 11:14:55 -0600 Subject: [PATCH] Added URL::to_action and URL::to_secure_action methods for conveniently generating URLs to controllers. --- laravel/url.php | 56 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/laravel/url.php b/laravel/url.php index 11004b03..43e809fe 100644 --- a/laravel/url.php +++ b/laravel/url.php @@ -5,10 +5,6 @@ class URL { /** * Get the base URL of the application. * - * If the application URL is set in the application configuration file, that - * URL will be returned. Otherwise, the URL will be guessed based on the - * server variables available to the script in the $_SERVER array. - * * @return string */ public static function base() @@ -79,9 +75,6 @@ public static function to_secure($url = '') /** * Generate an application URL to an asset. * - * The index file will not be added to asset URLs. If the HTTPS option is not - * specified, HTTPS will be used when the active request is also using HTTPS. - * * @param string $url * @param bool $https * @return string @@ -106,9 +99,9 @@ public static function to_asset($url, $https = null) /** * Generate a URL from a route name. * - * For routes that have wildcard parameters, an array may be passed as the second - * parameter to the method. The values of this array will be used to fill the - * wildcard segments of the route URI. + * For routes that have wildcard parameters, an array may be passed as the + * second parameter to the method. The values of this array will be used to + * fill the wildcard segments of the route URI. * * * // Create a URL to the "profile" named route @@ -146,7 +139,7 @@ public static function to_route($name, $parameters = array(), $https = false) return static::to(str_replace(array('/(:any?)', '/(:num?)'), '', $uri), $https); } - throw new \OutOfBoundsException("Error getting URL for route [$name]. Route is not defined."); + throw new \OutOfBoundsException("Error creating URL for undefined route [$name]."); } /** @@ -161,6 +154,45 @@ public static function to_secure_route($name, $parameters = array()) return static::to_route($name, $parameters, true); } + /** + * Generate a URL to a controller action. + * + * + * // Generate a URL to the "index" method of the "user" controller + * $url = URL::to_action('user@index'); + * + * // Generate a URL to http://example.com/user/profile/taylor + * $url = URL::to_action('user@profile', array('taylor')); + * + * + * @param string $action + * @param array $parameters + * @param bool $https + * @return string + */ + public static function to_action($action, $parameters = array(), $https = false) + { + return static::to(str_replace(array('.', '@'), '/', $action).'/'.implode('/', $parameters), $https); + } + + /** + * Generate a HTTPS URL to a controller action. + * + * + * // Generate a HTTPS URL to the "index" method of the "user" controller + * $url = URL::to_action('user@index'); + * + * + * @param string $action + * @param array $parameters + * @param bool $https + * @return string + */ + public static function to_secure_action($action, $parameters = array()) + { + return static::to_action($action, $parameters, true); + } + /** * Generate a URL friendly "slug". * @@ -220,4 +252,4 @@ public static function __callStatic($method, $parameters) throw new \BadMethodCallException("Method [$method] is not defined on the URL class."); } -} +} \ No newline at end of file