# Email address as argument to controller view method

**URL:** <https://discourse.cakephp.org/t/email-address-as-argument-to-controller-view-method/6989>\
**Category:** Need Help\
**Created:** [December 13, 2019, 8:40pm UTC](https://discourse.cakephp.org/t/email-address-as-argument-to-controller-view-method/6989 "2019-12-13T20:40:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertkendrick](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@robertkendrick](https://discourse.cakephp.org/u/robertkendrick)\
**Post date:** [December 13, 2019, 8:40pm UTC](https://discourse.cakephp.org/t/email-address-as-argument-to-controller-view-method/6989/1 "2019-12-13T20:40:50Z")

</div>

Kinda thought I was getting to grips with cakePHP 3.x but how little it turned out I knew.

My index.ctp displays a table of address information.  
One of the columns in this table is an email address.  
I create links in the table which (when clicked) calls this controller’s view method with the email address as argument.  
However CakePHP (or browser) returns error message: Resource cannot be found on the server. The URL is also modified and the email part has the ‘@’ replaced by %40 (not so unexpected)  
The problem is the email address itself, and the fact that it ends in .com or .co.uk.  
If I use a different column (not an email address) in the links it all works as expected.  
With an email address as argument the view method isn’t even called. I just get the error message as shown above.  
I believe this problem to be pretty basic stuff but haven’t be able to find an answer online.  
Any help appreciated.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [December 14, 2019, 12:31am UTC](https://discourse.cakephp.org/t/email-address-as-argument-to-controller-view-method/6989/2 "2019-12-14T00:31:17Z")

</div>

You need to urlencode what you put into the URL, and urldecode what you take out.

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [December 14, 2019, 6:32am UTC](https://discourse.cakephp.org/t/email-address-as-argument-to-controller-view-method/6989/3 "2019-12-14T06:32:45Z")

</div>

With the ‘how’ addressed, I’m scratching my head over the ‘why’.

Assuming a typical index-page to detail/view-page transition; I wonder why you don’t have a nice convenient record id to use as the key-value in your link.

Regards,  
Don

---

<div class="post-metadata">

**Author:** ![robertkendrick](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@robertkendrick](https://discourse.cakephp.org/u/robertkendrick)\
**Post date:** [December 14, 2019, 6:41pm UTC](https://discourse.cakephp.org/t/email-address-as-argument-to-controller-view-method/6989/4 "2019-12-14T18:41:07Z")

</div>

Thanks for the reply dreamingmind.  
If i were coding an application in earnest I would indeed be using ‘id’.  
As I said in my original post all other columns work (including id) but not an email address.  
Because I’m still experimenting I’d love to know why I can’t pass an email address to a controller’s view method (or as I suspect, to any other controller method).  
The link doesn’t even reach the view method when clicked. Just displays 'resource not found on server.

---

<div class="post-metadata">

**Author:** ![robertkendrick](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@robertkendrick](https://discourse.cakephp.org/u/robertkendrick)\
**Post date:** [December 14, 2019, 6:49pm UTC](https://discourse.cakephp.org/t/email-address-as-argument-to-controller-view-method/6989/5 "2019-12-14T18:49:17Z")

</div>

Thanks for your reply Zuluru.  
When I use Html-\>link() to create the link it must use urlencode as the email address is modified by replacing the ‘@’ with %40.  
Problem is that clicking the link does not even get to the controller’s view method. So can’t debug with pr() etc. Craeting the links with id or any other table column work fine.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [December 14, 2019, 7:37pm UTC](https://discourse.cakephp.org/t/email-address-as-argument-to-controller-view-method/6989/6 "2019-12-14T19:37:05Z")

</div>

Hmm, for some reason I thought that `urlencode` would also encode the period, which the `link` function won’t. But I guess not, and now don’t know why I would have thought that… It’s almost certainly the period that’s causing problems, so if you manually escape that, you should be okay. Reason being that it’s going to be seeing “.com” (or whatever) at the end and thinking that this is the file extension, and Cake doesn’t think it should be handling all those “file types”.

---

<div class="post-metadata">

**Author:** ![robertkendrick](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@robertkendrick](https://discourse.cakephp.org/u/robertkendrick)\
**Post date:** [December 16, 2019, 7:04pm UTC](https://discourse.cakephp.org/t/email-address-as-argument-to-controller-view-method/6989/7 "2019-12-16T19:04:06Z")

</div>

Yes, you are spot on with that Zuluru.  
Did a str\_replace() converting ‘.’ to %2e before forming the link and that did the trick.  
Underlying the Html-\>link() I suspect urlencode was called to replace the ‘@’ and the ‘%’ that I had inserted. So the link looked at bit strange but it all worked fine.

Interestly at the controller’s view method (or during the path to the view method by cakephp) urldecode must be automatically called as i’m left with a properly formed emal address in the passed in email argument.  
Thanks for your help…

---

<div class="post-metadata">

**Author:** ![dmuenstermann](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dmuenstermann](https://discourse.cakephp.org/u/dmuenstermann)\
**Post date:** [December 18, 2019, 10:32am UTC](https://discourse.cakephp.org/t/email-address-as-argument-to-controller-view-method/6989/8 "2019-12-18T10:32:54Z")

</div>

Using $this-\>Url-\>build() would also allow passing email addresses as GET param properly. I had a similar problem once and solved it with the following code:

```auto
<a 
	href="<?= $this->Url->build([
		'plugin' => 'XX',
		'controller' => 'XXXX', 
		'action' => 'X', 
		$username, 
		$email
	]) ?>" 
>
	<?= __('Next'); ?>	
</a>

```

---

<div class="post-metadata">

**Author:** ![robertkendrick](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@robertkendrick](https://discourse.cakephp.org/u/robertkendrick)\
**Post date:** [December 19, 2019, 10:18pm UTC](https://discourse.cakephp.org/t/email-address-as-argument-to-controller-view-method/6989/9 "2019-12-19T22:18:12Z")

</div>

Thanks dmuenstermann  
Unfortunately your example code gives me exactly the same issue.  
I added this to the controller index method:  
\<a  
href=“\<?= $this-\>Url-\>build(['action' =\> 'View', 'fb@abc.com']) ?\>”

> 

```
<?= __('Next'); ?>

```

Clicking on this link produces:  
The requested resource `/addresses/view/fb%40abc.com` was not found on this server. So clearly the ‘.’ is still causing problems.

I’m using CakePHP 3.7  
str\_replace(‘.’, ‘0x2e’, $email); // Using this before forming the link provides a fix.

I’d be grateful for any further info.  
Regards

---

<div class="post-metadata">

**Author:** ![dmuenstermann](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@dmuenstermann](https://discourse.cakephp.org/u/dmuenstermann)\
**Post date:** [January 14, 2020, 8:34am UTC](https://discourse.cakephp.org/t/email-address-as-argument-to-controller-view-method/6989/10 "2020-01-14T08:34:39Z")

</div>

I’m wondering about that - as the mentioned code works for me (also using cake 3.7).  
Can you please post your routes.php and controller function?

---

<div class="post-metadata">

**Author:** ![robertkendrick](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@robertkendrick](https://discourse.cakephp.org/u/robertkendrick)\
**Post date:** [January 29, 2020, 8:59pm UTC](https://discourse.cakephp.org/t/email-address-as-argument-to-controller-view-method/6989/11 "2020-01-29T20:59:53Z")

</div>

Hi dmuenstermann  
Sorry for the long delay in replying.

It starts with this link in index.ctp

- \<?= $this-\>Html-\>link('email\_link', ['action'=\>'view', $users-\>first()-\>email]) ?\>

As you know when this link is clicked it should call the controller’s view action with the email as argument.  
But it never gets there and I get the error message: Resource cannot be found on the server  
This is the controller view function:  
public function view($email=null)  
{  
$user = $this-\>Users-\>findByEmail($email)-\>firstOrFail();  
$this-\>set(‘user’, $user);  
}

By changing the index.ctp link code to this - it all works fine.

- \<?= $this-\>Html-\>link('enail\_link', ['action'=\>'view', str\_replace('.', '%2e', $users-\>first()-\>email)]) ?\>

my routes file is the default 3.7 routes.php file. Not changed by me.

regards
