# Controller renders page but the url shows the controller function name (2.0)

**URL:** https://discourse.cakephp.org/t/controller-renders-page-but-the-url-shows-the-controller-function-name-2-0/11340
**Category:** Need Help
**Tags:** release
**Created:** [July 12, 2023, 11:54am UTC](https://discourse.cakephp.org/t/controller-renders-page-but-the-url-shows-the-controller-function-name-2-0/11340 "2023-07-12T11:54:21Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![ericnickus](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@ericnickus](https://discourse.cakephp.org/u/ericnickus)
#### Post date: [July 12, 2023, 11:54am UTC](https://discourse.cakephp.org/t/controller-renders-page-but-the-url-shows-the-controller-function-name-2-0/11340/1 "2023-07-12T11:54:21Z")

</div>

Hi. I’m using Cake PHP 2.0 ,and I did manage to get my controller to re-render the page after a form submits, however , the page the form is on is called contact.ctp, and the url shows /contact at the end. But when I do a $this-\>render(‘/Pages/contact’); in my controller, it does re-render the page so it stays on the same page, but the url shows the controller function at the end and not contact. /leads/add .

Could this be a routing thing I need to do ? Or a better way to stay on the same page after form submit ? Would be greatly appreciated Thanks , have been wrestling with this for a couple week now. Am new to cake but not to php.

Cheers

---

<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: [July 12, 2023, 1:39pm UTC](https://discourse.cakephp.org/t/controller-renders-page-but-the-url-shows-the-controller-function-name-2-0/11340/2 "2023-07-12T13:39:54Z")

</div>

Typically, you wouldn’t _render_ a different template, but rather redirect to the different URL.

---

<div class="post-metadata">

### Author: ![ericnickus](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@ericnickus](https://discourse.cakephp.org/u/ericnickus)
#### Post date: [July 12, 2023, 5:52pm UTC](https://discourse.cakephp.org/t/controller-renders-page-but-the-url-shows-the-controller-function-name-2-0/11340/3 "2023-07-12T17:52:12Z")

</div>

Thanks so much but I don’t want to go to a different url . when the form submits, I just want it to stay on the same page and show the same page again. Everyother thing I have tried it just shows the footer or goes to main index page  
I can post code . the controller:

\<?php error\_reporting(E\_ALL); ini\_set('display\_errors', '1'); App::uses('CakeEmail', 'Network/Email'); class LeadsController extends AppController { public $helpers = array('Html', 'Form', 'Session'); public $components = array('Session', 'Flash', 'Email'); public function add() { if ($this-\>request-\>is('post')) { $email = new CakeEmail('default'); $email-\>from('[noreply@smsold.com](mailto:noreply@smsold.com)') -\>to($this-\>request-\>data['Lead']['email']) -\>subject('Thank you for your submission') -\>emailFormat('html'); $emailContent = '

## Thank you for your submission!

Please visit our website at [www.example.com](http://www.example.com) for more information.

Best regards,

Your Company

'; $email-\>send($emailContent); if ($this-\>Lead-\>save($this-\>request-\>data)) { $this-\>Session-\>setFlash('Message Successfully Sent!.'); } } $this-\>render('/Pages/contact'); } } the form : \<?php echo $this-\>Form-\>create('Lead', array('url' =\> array('controller' =\> 'leads', 'action' =\> 'add'))); ?\>
\<?php echo $this-\>Form-\>input('first\_name', array('class' =\> 'f1')); ?\>

\<?php echo $this-\>Form-\>input('last\_name', array('class' =\> 'f1')); ?\>

\<?php echo $this-\>Form-\>input('email', array('class' =\> 'f1')); ?\>

\<?php echo $this-\>Form-\>input('phone\_number', array('class' =\> 'f1')); ?\>

\<?php echo $this-\>Form-\>input('message', array('class' =\> 'f2', 'label' =\> false, 'placeholder' =\> 'send us a message' )); ?\>
\<?php echo $this-\>Form-\>hidden('lead\_type\_id', array('value' =\> 1)); ?\> \<?php echo $this-\>Form-\>hidden('priority', array('value' =\> 2)); ?\> \<?php echo $this-\>Form-\>hidden('safe', array('value' =\> 1)); ?\> \<?php echo $this-\>Form-\>hidden('spam', array('value' =\> 0)); ?\>
\<?php echo $this-\>Form-\>submit('Submit', array('class' =\> 'btn btn-primary btn1')); ?\>

\<?php echo $this-\>Session-\>flash(); ?\>
\<?php echo $this-\>Form-\>end(); ?\>

THANKS SO MUCH A MILLION THANKS

---

<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: [July 12, 2023, 6:30pm UTC](https://discourse.cakephp.org/t/controller-renders-page-but-the-url-shows-the-controller-function-name-2-0/11340/4 "2023-07-12T18:30:44Z")

</div>

You are saying you don’t want to redirect to a different page, but you want the URL to indicate that you did, in fact, redirect to a different page. Why not redirect instead of render? It’s called “[Post Redirect Get](https://en.wikipedia.org/wiki/Post/Redirect/Get)”, and it’s a very standard programming paradigm that solves a lot of problems you’re going to run into with your current structure.

---

<div class="post-metadata">

### Author: ![ericnickus](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@ericnickus](https://discourse.cakephp.org/u/ericnickus)
#### Post date: [July 12, 2023, 7:58pm UTC](https://discourse.cakephp.org/t/controller-renders-page-but-the-url-shows-the-controller-function-name-2-0/11340/5 "2023-07-12T19:58:21Z")

</div>

no I start out from /contact. there is a form that is submitted and saves some data to database. I want to stay on page and show flash message , which it does in this code, however, when it’s all done the url says /leads/add. instead of. /contact  
thjanks so much

---

<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: [July 12, 2023, 8:06pm UTC](https://discourse.cakephp.org/t/controller-renders-page-but-the-url-shows-the-controller-function-name-2-0/11340/6 "2023-07-12T20:06:43Z")

</div>

But, you are NOT staying on the same page. You are posting that data to the `add` function. The general recommendation is that you Post to `add`, then that function Redirects to `contact`. If you want to _never leave_ the `contact` page, then you need to use Ajax instead of a standard POST.

None of this is in any way Cake specific, it’s how web applications work no matter what language they’re implemented in. You can post with HTTP (as you’re doing), which loads a new page, or you can post with Ajax, which does not, but comes with other complexities.

---

<div class="post-metadata">

### Author: ![ericnickus](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@ericnickus](https://discourse.cakephp.org/u/ericnickus)
#### Post date: [July 12, 2023, 10:21pm UTC](https://discourse.cakephp.org/t/controller-renders-page-but-the-url-shows-the-controller-function-name-2-0/11340/7 "2023-07-12T22:21:58Z")

</div>

ok what is the right way to do it then , I already tried ajax, it was a hassle. So once I sav e my data I am on leads/add, then I would do what to redirect back to /contact? and display the flash message this way seems to work and all other ways just ended up showing me the feooter only. What is the recommended way?please sir. And I have another situation. I have email code and config that has no errors at at biut doesnt send the email… Should I post the code with you or po=st it on the forum?

---

<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: [July 12, 2023, 11:07pm UTC](https://discourse.cakephp.org/t/controller-renders-page-but-the-url-shows-the-controller-function-name-2-0/11340/8 "2023-07-12T23:07:41Z")

</div>

You just [return a redirect](https://book.cakephp.org/2/en/controllers.html#flow-control). The flash message should be saved and show up in the next page that’s actually rendered, which will be when the user gets back to the `contact` page.

Any issue with emailing is an entirely separate question, and should be posted as a new topic.

---

<div class="post-metadata">

### Author: ![ericnickus](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@ericnickus](https://discourse.cakephp.org/u/ericnickus)
#### Post date: [July 18, 2023, 8:46pm UTC](https://discourse.cakephp.org/t/controller-renders-page-but-the-url-shows-the-controller-function-name-2-0/11340/9 "2023-07-18T20:46:00Z")

</div>

your absolutely right I changed the word from render to redirect and it still works the same but shows the right page. Thanks
