Note that there are some explanatory texts on larger screens.

plurals
  1. PORouting configuration and controller action parameters for simple URL redirector
    text
    copied!<p>I am trying to make a simple redirector controller in CakePHP. I'd like the URL to be of the form:</p> <pre><code>http://example.com/redirector/&lt;numeric id&gt;/&lt;url to redirect to&gt; </code></pre> <p>For example,</p> <pre><code>http://example.com/redirector/1/http://www.google.com </code></pre> <p>The URL that I need to redirect could be more complex, of course, including slashes, parameters and anchors.</p> <p>I can't seem to be able to figure out how to write the route configuration so that my action would look something like:</p> <pre><code>class RedirectsController extends AppController { function myredirectaction($id, $url) { $this-&gt;autoRender = false; $this-&gt;redirect($url); } </code></pre> <p>It seems like whatever I try, the "/"'s in the url-to-redirect-to confuses my route attempt and splits the URL into pieces, and this no longer matches my action definition. What should I do?</p> <p>I am new to PHP and CakePHP, so any advice you can give is appreciated.</p> <p><strong>Update:</strong></p> <p>So instead of the example URL above, it has been URL-escaped to look like this:</p> <pre><code>http://example.com/redirector/1/http%3A%2F%2Fwww.google.com </code></pre> <p>However, my routing is still not working. Here's what I have in routes.php:</p> <pre><code>Router::connect( '/redirector/:id/:url', array('controller' =&gt; 'redirects', 'action' =&gt; 'myredirectaction'), array( 'id' =&gt; '[0-9]+', 'url' =&gt; 'http.*' ) ); </code></pre> <p>This is what I get when I try that URL:</p> <pre><code>Warning (2): array_merge() [function.array-merge]: Argument #1 is not an array [CORE/cake/dispatcher.php, line 301] Code | Context $fromUrl = "redirector/1/http://www.google.com" $params = array( "pass" =&gt; array(), "named" =&gt; array(), "id" =&gt; "1", "url" =&gt; "http://www.google.com", "plugin" =&gt; null, "controller" =&gt; "redirects", "action" =&gt; "myredirectaction", "form" =&gt; array() ) $namedExpressions = array( "Action" =&gt; "index|show|add|create|edit|update|remove|del|delete|view|item", "Year" =&gt; "[12][0-9]{3}", "Month" =&gt; "0[1-9]|1[012]", "Day" =&gt; "0[1-9]|[12][0-9]|3[01]", "ID" =&gt; "[0-9]+", "UUID" =&gt; "[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}" ) $Action = "index|show|add|create|edit|update|remove|del|delete|view|item" $Year = "[12][0-9]{3}" $Month = "0[1-9]|1[012]" $Day = "0[1-9]|[12][0-9]|3[01]" $ID = "[0-9]+" $UUID = "[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}" $url = array( "url" =&gt; "/redirector/1/http://www.google.com" ) array_merge - [internal], line ?? Dispatcher::parseParams() - CORE/cake/dispatcher.php, line 301 Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 118 [main] - APP/webroot/index.php, line 88 </code></pre> <p>And more warnings from my action since it did not get the two expected arguments.</p> <p>Of course, I've changed my action to urldecode($url) before using the $url.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload