I have the following problem in Cakephp:
I maded a CMS to edit the content of a website with CakePhp. Now when I change the content in my form and I click Save then he shows again the data from before the form was changed.
In the database everything saved well and if I refresh the page also he shows the right data.
I know I can render a new page or redirect to another page, but I liked to just show a message with setFlash and that's it. Someone can help me out?
Thanks in advance
AƤron
Update : check the Justin T. solution below.
You can just redirect to the same page:
to set your flash message:
$this->setFlash('blablablabla');
and
return $this->redirect($this->here); //cake 1.3
or
return $this->redirect($this->request->here); // cake 2.x
or
return $this->redirect(['controller' => 'MyController', 'action' => 'methodOfController']); // cake3.X
to redirect.
With the new method flash() you set a message and redirect in the same method.
This is the most elegant way I know of, and it goes something like this :
<?php
public function post() {
// process content here...
// redirects the user immediately with a nice message
$this->flash('Your post has been updated, thanks', 'index');
}
Hope this helps !
Related
Since whole a day I have been trying to search for best solution to redirect to same page where it is requested. Means I have a edit action page. I can move on this page from anywhere either it is detail, view, latest, recent or from any other action I can move to edit action. What I want is I want to move to last action again where it is redirected from. While googling I got solution to put
`$this->redirect($this->referer());` //or
`$this->redirect(Controller::referer());`
to use in controller to move to but it reverts to edit page again as it is navigated from that only.
Other solution told me to use hidden variable whose value it
`echo $this->Form->input('Song.referer', array('type' => 'text', "value"=>$this->request->referer()));`
like this and request to this variable on successfull edit that is OK. But it fails in case of validation put on controller as same page is open in case of error so my referer change. What shall be best practise in this case. Should I save session in and every call to main my referrer or something other available with cakephp 2.3
I use this method:
when I create a link to my edit page in my action i add a 'referer' param
$this->Html->link('Add', array('action' => 'add', 'referer' => 'detail'));
in my controller then I read the value of referer and i redirect to the right page after saving the data, even in case of validation error the value of referer remains in the url of the page
It's not a great solution since it's not transparent to the user who see the url and can change the referer changing the url itself. But it works and it's simple to implement
Save the last location in hidden field eg.
$this->request->data["Business"]["referer"] = $this->referer();
Now put optional condition on save function to redirect on the page like this.
if ($this->Business->save($this->request->data)) {
(isset($this->request->data["Business"]["referer"]) && !empty($this->request->data["Business"]["referer"]) ? $this->redirect($this->request->data["Business"]["referer"]) : $this->redirect(array("action" => "index"));
}
I have a view which interacts with more than one controller (post, comments, categories...)
In case the comment is not empty, it would go to the same action (comments/add) but in this case i manually redirect them again to the previous view with something like this:
$this->redirect(array('controller' => 'posts', 'action' => 'view', $this->request->data['Comment']['posts_id'], '#' => $this->Comment->id));
The thing is, when they try to post an empty comment, for example, the validation of Comment redirects it to comments/add (which is the route on the form action) but i don't want the redirection to do this. I want it to come back to the original view and i don't have anyway to change it on the controller or the model (as far as I know)
Is there any way to do this?
Thanks.
The easiest way to do this is to make your form submit via AJAX. So you don't refresh the whole page on submit - you just get the form back, with errors (if there are any).
Obviously, that won't work for users without Javascript. So you can either not worry about those users, or you can make it so the add method of your comments controller uses the view that you want. Something like this:
if ($this->request->is('ajax')) {
$this->render('/Elements/comment_form');
return; // return the form only
} else {
$this->render('/Posts/view'); // Your post view.ctp would have the comment form on it.
}
Here's an example of a page where I've used that method: http://www.lintonmeagher.com/contact Try submitting either one of the forms with no data.
Let me know if you need any more info.
In CakePHP, I want to create a custom URL that points from my site to another site.
Example: example.com/google would redirect to http://www.google.com
I'm a self-taught CakePHP newcomer and just can't figure out the steps. From my homework, I think I can create a route to a controller/action in config/routes.php, but I don't the right terminology to create the action in the controller.
If you want to redirect directly form controller to an external url the we can directly use
$this->redirect('http://www.google.com');
from our controller. It will redirect you to the mentioned address. This works perfectly fine.
You don't want a "redirect", you want to create a hyperlink.
Use Cake's built-in Html helper.
In your controller...
var $helpers = array( 'Html' );
In your view...
echo $this->Html->link( 'Google link!', 'http://www.google.com/' );
A "redirect" is commonly used to refer to redirecting the script on the server side. For example, after a user fills out a Contact form you may want to email yourself the details and then redirect the user to a "Success!" page with the following controller code
$this->redirect( '/contact/success' );
Using CakePHP HTML helper is your best bet.
echo $this->Html->link('Link Text Here', 'http://www.anywebsiteyouwant.com);
If it's simple enough, you could just use straight HTML.
What you need is something like:
Router::redirect('/posts/*', 'http://google.com', array('status' => 302));
This would redirect /posts/* to http://google.com with a HTTP status of 302.
See http://book.cakephp.org/2.0/en/development/routing.html
I am trying to do some custom routing on my site, but have been stuck for 2 days at a very silly issue. I have the following route configuration:
Router::connect('/your-solution/add-comment/*', array('controller' => 'comments', 'action' => 'add'));
Router::connect('/admin/your-solution/add-comment/*', array('controller' => 'comments', 'action' => 'add', 'admin' => true));
The problem is that when I try to load a URL formatted using the second route, it gives me a 404 not found.
The first rule works fine.
For both rules I have a separate element containing a form and pointing to a URL formatted after the respective rule. The only parameter for both actions is the solution id, which is "contained" in the wildcard.
What could possibly be the issue? Thank you very much for your help!
EDIT:
I found out another weird behaviour. When I access /admin/your-solution/add-comment/3, it goes to that action. But if I submit a form to that link, it displays a blank page, with Firebug informing me that the page was not found. Very strange...
Also, I have a similar route for editing comments. Both loading the edit form and saving the form work...
how are you?
In order to see exactly why isn't it working, go to your /app/config/core.php and seek for this line:
Configure::write('debug', 2);
And make sure the value is set to "2". This way, it'll no longer give you a 404 error, but the actual issue, since in production mode (debug set to 0), all errors are masked with a 404 error.
Let me know!
Cheers!
In your core.php be sure
Configure::write('Routing.prefixes', array('admin'));
In your comments controller, be sure you have
function admin_add() {...}
Also try other ways of formatting Routing statement.
Router::connect('/admin/your-solution/add-comment', array('controller' => 'comments', 'action' => 'add', 'admin' => true));
The order of your route is also important. You may want to check that.
For debugging which route you are using when loading the URL, try adding this code to your app_controller.php file.
function __construct() {
$route = Router::currentRoute();
pr($route);
}
These are just some tips to hopefully help you move forward.
Apparently, the problem has been lying in a disabled input. After I've deleted this element, the form submits correctly and the target page is shown.
Just for my knowledge, why didn't the form submit if it had a disabled input in it?
in my application am calling this line in case a particular case (if condition) from ajax call
$this->redirect('/login/login/');
But this was updating the particular div. I want to redirect the whole page.
How to do it?
Thanks,
you may need to do it the old way :)
window.opener.location.href = "new page";
I believe that you can supply a parameter to redirect which will change the controller.
In an application I have written I use:
$this->redirect(array('controller' => 'users', 'action' => 'login');
when I want to prompt the user for a login.
You can find the full documentation for redirect here
http://book.cakephp.org/view/425/redirect