gyro/core/controller/base/actionmapper.cls.php
Go to the documentation of this file.00001 <?php 00002 /** 00003 * Maps actions to URLs 00004 * 00005 * The ActionMapper allows to retrieve the URL for a given action, including possible 00006 * parameters. 00007 * 00008 * Given a model "blog" and an action blog_view on Controler BlogControler to show a 00009 * blog entry defined as a ParameterizedRoute of 'blog/{id:ui>}'. The url of a given 00010 * blog entry can now be retrieved like this: 00011 * 00012 * @code 00013 * $blog = Blog::get($id); // Get blog entry with given id 00014 * $s_url = ActionMapper::get_url('view', $blog); // Retrieve url to view entry 00015 * @endcode 00016 * 00017 * @author Gerd Riesselmann 00018 * @ingroup Controller 00019 */ 00020 class ActionMapper { 00021 /** 00022 * Array of actions and URLS 00023 */ 00024 private static $actions = array(); 00025 00026 /** 00027 * Registers URL builder for action. 00028 * 00029 * @param string $action Action name 00030 * @param IUrlBuilder $urlbuilder The builder for given action 00031 * 00032 */ 00033 public static function register_url($action, $urlbuilder) { 00034 self::$actions[$action] = $urlbuilder; 00035 } 00036 00037 /** 00038 * Returns path for given action. Path is relative to web root, although an url builder may decide to 00039 * return an absolute url (including "http://"!) nonetheless (e.g. if https is required for action url) 00040 * 00041 * @param string $action Action name 00042 * @param mixed $params Parameters, depend on action 00043 * @return string 00044 */ 00045 public static function get_path($action, $params = null) { 00046 return self::build_url($action, $params, IUrlBuilder::RELATIVE); 00047 } 00048 00049 /** 00050 * Returns path for given action. Path is relative to web root. Even if protocol or host do not match, 00051 * only the path will be returned. Use this for images, e.g. 00052 * 00053 * @param string $action Action name 00054 * @param mixed $params Parameters, depend on action 00055 * @return string 00056 */ 00057 public static function force_path($action, $params = null) { 00058 $url = self::build_url($action, $params, IUrlBuilder::ABSOLUTE); 00059 return '/' . Url::create($url)->get_path(); 00060 } 00061 00062 00063 /** 00064 * Returns url for given action. Path is absolute 00065 * 00066 * @param string $action Action name 00067 * @param mixed $params Parameters, depend on action 00068 * @return string 00069 */ 00070 public static function get_url($action, $params = null) { 00071 return self::build_url($action, $params, IUrlBuilder::ABSOLUTE); 00072 } 00073 00074 /** 00075 * Checks if given action route matches current URL, if not redirects 00076 * 00077 * @param string $action Action name 00078 * @param mixed $params Parameters, depend on action 00079 */ 00080 public static function validate_against_current($action, $params = null) { 00081 $url = Url::create(self::get_url($action, $params)); 00082 if (!Url::current()->equals($url, Url::EQUALS_IGNORE_QUERY)) { 00083 $url->redirect(Url::PERMANENT); 00084 } 00085 } 00086 00087 /** 00088 * Returns true, if the given action route equals current URL 00089 * 00090 * @param string $action Action name 00091 * @param mixed $params Parameters, depend on action 00092 * @return bool 00093 */ 00094 public static function equals_current($action, $params = null) { 00095 return Url::current()->equals(self::get_url($action, $params), Url::EQUALS_IGNORE_QUERY); 00096 } 00097 00098 /** 00099 * Build URL for given action, with given params, either relative or absolute 00100 * 00101 * @param string $action 00102 * @param mixed $params 00103 * @param integer $absolute_or_relative Either IUrlBuilder::ABSOLUTE or IUrlBuilder::RELATIVE 00104 * @return string 00105 */ 00106 private static function build_url($action, $params, $absolute_or_relative) { 00107 $url_builder = Arr::get_item(self::$actions, $action, false); 00108 if (empty($url_builder)) { 00109 if ($params instanceof IActionSource) { 00110 $action = $params->get_action_source_name() . '_' . $action; 00111 $url_builder = Arr::get_item(self::$actions, $action, false); 00112 } 00113 } 00114 if ($url_builder) { 00115 return $url_builder->build_url($absolute_or_relative, $params); 00116 } 00117 return ''; 00118 } 00119 00120 }