@@ -171,6 +171,27 @@ public static function action($actionName, $controllerName = null, $routeValues
171171 return UrlHelper::action (self ::$ viewContext , $ actionName , $ controllerName , $ routeValues , $ fragment , $ schema , $ host );
172172 }
173173
174+ /**
175+ * Returns a link (<a />) to the specified action.
176+ *
177+ * @param string $linkText The link text.
178+ * @param string $actionName The name of the action.
179+ * @param string $controllerName The name of the controller. Default: current controller.
180+ * @param array $routeValues An array that contains the parameters for a route.
181+ * @param string $fragment The URL fragment name (the anchor name).
182+ * @param string $schema The protocol for the URL, such as "http" or "https".
183+ * @param string $host The host name for the URL.
184+ * @param string $htmlAttributes Additional HTML attributes that will be used when creating the link.
185+ *
186+ * @return string
187+ */
188+ public static function actionLink ($ linkText , $ actionName , $ controllerName = null , $ routeValues = null , $ htmlAttributes = null , $ fragment = null , $ schema = null , $ host = null ) {
189+ $ url = UrlHelper::action (self ::$ viewContext , $ actionName , $ controllerName , $ routeValues , $ fragment , $ schema , $ host );
190+ $ attr = self ::buildAttributes ($ htmlAttributes , array ('href ' ));
191+
192+ return '<a href=" ' . $ url . '" ' . (!empty ($ attr ) ? ' ' : '' ) . $ attr . '> ' . htmlspecialchars ($ linkText ) . '</a> ' ;
193+ }
194+
174195 /**
175196 * Gets model state.
176197 *
@@ -180,4 +201,34 @@ public static function getModelState() {
180201 return View::getModelState ();
181202 }
182203
204+ /**
205+ * Generates HTML-attributes string.
206+ *
207+ * @param array $htmlAttributes Associative array of attributes.
208+ * @param array $ignore List of attributes to ignore.
209+ *
210+ * @return string
211+ */
212+ private static function buildAttributes ($ htmlAttributes , $ ignore = array ())
213+ {
214+ if (empty ($ htmlAttributes ))
215+ {
216+ return '' ;
217+ }
218+
219+ if ($ ignore === null ) {
220+ $ ignore = array ();
221+ }
222+
223+ $ filtered = array_filter ($ htmlAttributes , function ($ key ) use ($ ignore ) {
224+ return !in_array ($ key , $ ignore );
225+ }, \ARRAY_FILTER_USE_KEY );
226+
227+ array_walk ($ filtered , function (&$ value , $ key ) {
228+ $ value = sprintf ('%s="%s" ' , $ key , str_replace ('" ' , '\" ' , $ value ));
229+ });
230+
231+ return implode (' ' , $ filtered );
232+ }
233+
183234}
0 commit comments