3

I am developing an application using CakePHP 2.6 and having issues with passing a string containing url characters as a parameter through to a controller method.

In my view I have a chunk of code which echoes out a series of table rows containing data and passes through the page id, unit id and the id of the link which can sometimes contain a url.

<?php foreach($linklist as $l) { ?>
    <tr id="Link_<?php echo $l['ID']; ?>">
        <td><?php echo $l['Title']; ?></td>
        <td class="buttontd"><?php echo $this->Form->postlink('Delete', array('action' => 'deletelink', $this->request->params['pass'][0], $results[0]['PageUnitTypeID'], $l['ID']), array('class' => 'button delete')); ?></td>
    </tr>
<?php } ?>

When the postlink button passes the information over to the 'deletelink' action in the controller the url looks like this:

http://mydomainname.com/webpages/deletelink/239/7/urlhttp%3A%2F%2Fwww.google.co.uk%2F

Which shows that the url has been passed as the string but then in the action when I try to just var_dump() the third parameter it returns a string of www.google.co.uk and nothing more which is preventing me from doing a substr() call on the parameter to check if the first 3 characters are equal to url or not.

I have tried to wrap the parameter in the postlink call inside serialize() and urlencode() but neither has had the desired effect of returning the full string as

urlhttp%3A%2F%2Fwww.google.co.uk%2F

Does anyone know of a successful way to pass through a parameter like this without losing important characters?

Update 1: Deletelink action

public function deletelink($pid = null, $uid = null, $lid = null) {
    $this->autoRender = false;

    if (!is_null($pid) && is_numeric($pid) && !is_null($uid) && is_numeric($uid)) {

        if (!is_null($lid)) {

            if (substr($lid, 0, 3) == 'url') {
                echo substr($lid, 0, 3);
            } else {
                echo substr($lid, 0, 3);
            }

        } else {
            $this->Session->setFlash('It is unknown which link you wish to delete from the webpage', 'flash_message_bar', array('class' => 'error'));
            return $this->redirect(array('action' => 'edit', $pid));
        }

    } else {
        $this->Session->setFlash('It is unknown which on which webpage you wish to delete a link', 'flash_message_bar', array('class' => 'error'));
        return $this->redirect(array('action' => 'index'));
    }
}
Inigo Flores
  • 4,461
  • 1
  • 15
  • 36
Daniel
  • 189
  • 9
  • Do you want to pass 3 parameters to `deletelink` action? How does your delete action looks like? – skywalker Dec 11 '15 at 10:45
  • @skywalker yes I want to pass all 3 parameters to the `deletelink` action. The concept is to do stuff with the third parameter and then redirect back to another action using the first 2 parameters. I have updated my original post to include the `deletelink` controller code. – Daniel Dec 11 '15 at 10:55
  • Did you try [this](http://stackoverflow.com/questions/4750989/passing-an-urlencoded-url-as-parameter-to-a-controller-action-at-cakephp)? – skywalker Dec 11 '15 at 11:48
  • @skywalker Just tried the `base64` encode and decode. It allows me to get the value at the other end which is the most important thing although I think I will still look for a better solution in the future. Thanks. – Daniel Dec 11 '15 at 12:34

1 Answers1

1

CakePHP (or mod_rewrite I would say) is getting confused with the way your URL is formed.

Your safest option is to base64_encode the url parameter in the view, which will result in call similar to:

http://mydomainname.com/webpages/deletelink/239/7/dXJsaHR0cDovL3d3dy5nb29nbGUuY28udWsv

and base64_decode it later in the action, which will transform

dXJsaHR0cDovL3d3dy5nb29nbGUuY28udWsv

into

urlhttp://www.google.co.uk/
Inigo Flores
  • 4,461
  • 1
  • 15
  • 36