{"id":281,"date":"2012-08-30T12:07:15","date_gmt":"2012-08-30T16:07:15","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/acts\/?p=281"},"modified":"2012-08-30T13:11:22","modified_gmt":"2012-08-30T17:11:22","slug":"yii-forward-with-params","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/acts\/2012\/08\/30\/yii-forward-with-params\/","title":{"rendered":"Yii forward with params"},"content":{"rendered":"<p>Yii&#8217;s forward doesn&#8217;t work with passing params to actions. I.e. This doesn&#8217;t work: $this->forward(&#8220;\/mycontroller\/myaction\/1\/2&#8221;);<\/p>\n<p>Because of the way urlmanager works I made the decision early on to have all action parameters take on the var names $id and $id2. So in the main config:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&#039;components&#039;=&gt;array(\r\n\t\t\t\/\/ uncomment the following to enable URLs in path-format\r\n\t\t\t&#039;urlManager&#039;=&gt;array(\r\n\t\t\t\t&#039;urlFormat&#039;=&gt;&#039;path&#039;,\r\n\t\t\t\t&#039;rules&#039;=&gt;array(\r\n\t\t\t\t\t&#039;&lt;controller:\\w+&gt;\/&lt;id:\\d+&gt;&#039;=&gt;&#039;&lt;controller&gt;\/view&#039;,\r\n\t\t\t\t\t&#039;&lt;controller:\\w+&gt;\/&lt;action:\\w+&gt;\/&lt;id:\\d+&gt;&#039;=&gt;&#039;&lt;controller&gt;\/&lt;action&gt;&#039;,\r\n\t\t\t\t\t&#039;&lt;controller:\\w+&gt;\/&lt;action:\\w+&gt;\/&lt;id:\\d+&gt;\/&lt;id2:\\d+&gt;&#039;=&gt;&#039;&lt;controller&gt;\/&lt;action&gt;&#039;,\r\n\t\t\t\t\t&#039;&lt;controller:\\w+&gt;\/&lt;action:\\w+&gt;&#039;=&gt;&#039;&lt;controller&gt;\/&lt;action&gt;&#039;,\r\n\t\t\t\t\t&#039;&lt;controller:\\w+&gt;\/&lt;action:\\w+&gt;\/&lt;cact:\\w+&gt;&#039;=&gt;&#039;&lt;controller&gt;\/&lt;cact&gt;&#039;,\r\n\t\t\t\t),\r\n\t\t\t),\r\n<\/pre>\n<p>So I used Controller.php in the components directory to override the forward method like so:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\t \/**\r\n\t * Overloading CController::forward\r\n\t * added the routeArr lines so the ids are passed\r\n\t *\/\r\n\tpublic function forward($route,$exit=true){\r\n\t\t\/\/error_log(&quot;Controller::forward&quot;);\r\n\t\t$routeArr = explode(&quot;\/&quot;, $route);\r\n\t\tif(isset($routeArr&#x5B;3]))\r\n\t\t\t$_GET&#x5B;&#039;id&#039;] = $routeArr&#x5B;3];\r\n\t\tif(isset($routeArr&#x5B;4]))\r\n\t\t\t$_GET&#x5B;&#039;id2&#039;] = $routeArr&#x5B;4];\r\n\r\n\t\tparent::forward($route, $exit);\r\n\r\n\t}\r\n<\/pre>\n<p>And that&#8217;s it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yii&#8217;s forward doesn&#8217;t work with passing params to actions. I.e. This doesn&#8217;t work: $this->forward(&#8220;\/mycontroller\/myaction\/1\/2&#8221;); Because of the way urlmanager works I made the decision early on to have all action parameters take on the var names $id and $id2. So in the main config: &#039;components&#039;=&gt;array( \/\/ uncomment the following to enable URLs in path-format &#039;urlManager&#039;=&gt;array( [&hellip;]<\/p>\n","protected":false},"author":4571,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[63924,63928,63937],"tags":[64006,63943],"class_list":["post-281","post","type-post","status-publish","format-standard","hentry","category-atg","category-quizmo-atg","category-yii","tag-forward","tag-yii-2"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts\/281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/users\/4571"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/comments?post=281"}],"version-history":[{"count":3,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts\/281\/revisions"}],"predecessor-version":[{"id":286,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts\/281\/revisions\/286"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/media?parent=281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/categories?post=281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/tags?post=281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}