This is a response to Josh Rs request for help, posted to the CakePHP Google Group. Somewhere between his last post, and my response, the forum was made read only. So I’m posting it here, in the hope he’ll see it.
The original post was here [https://groups.google.com/forum/#!topic/cake-php/UJEWOQCvFhE]
Hi Josh
I couldn’t see a similar topic on discourse.cakephp, so I’ll reply here [Edit: now here is gone, and we’re at discourse anyway]
It’s been a while since I’ve worked with CakePHP 1.x, so nothing in particular is jumping out at me as being the cause for the 404. However, here are some pointers:
When the new user gets created, before the save() call, do a create(). i.e. $this->User->create();
This makes sure that any data set on the User model is cleared, and doesn’t interfere with the save. Not having it “shouldn’t” cause the 404.
Not sure if it’s a change from Steven Ybarra, but it looks like validation is being suppressed on save, for the the create. Ideally, you should allow the validation, and check the results of the save before continuing. If the save fails, then let the page be rendered, and if CakePHP Forms and Inputs are being used, then validation errors should show.
i.e.
$continueProcessing = true;
if ($new_user) {
$this->User->create();
if ($this->User->save($this->data)) {
$id = $this->User->id;
$this->data['User']['user_id'] = $id;
} else {
$continueProcessing = false;
}
}
if ($continueProcessing) {
// most remaining code
}
// $this->_get_*() calls here so they get called regardless, or your lists won't populate on error
The next thing you might want to do is turn up debugging, to help solve the issue. Sometimes CakePHP will mask an error behind a CakePHP generated 404 (rather than a genuine web server based 404). In the core.php, there will be a debug variable. Set this to 2 (don’t forget to change it back when you’re all done), and try the operation again. You should get increased debugging, and this might reveal the issue. Also check the tmp/logs/debug.log
file and perhaps errors.log
.
I’m not sure that the highlighted redirect is causing the issue. If you’re not getting any data saved, then the saveAll will return false, and you’ve never get to the redirect.
I don’t think I’ve used the crumb option before, but please double check the last $this->set('crumb', ...)
for the posted code. You have a ‘#’ concatenating to an array expression (i.e. '#' . $id => ()
). It doesn’t look right, and the value of the action seems a little hinky if it’s supposed to match the PHP action. Though if it’s a special Ajax provision, then this might be okay.
With regards to the highlighted redirect code, it “should” be okay. When $this->redirect()
is called, CakePHP should default the control, and recognize that it’s in an admin prefix, to produce the correct URL. Again, use the Network table in Chrome to check if the 404 is a web server based 404 (Apache, nginx or IIS), or a CakePHP generated 404. If that gets you no where, perhaps try an IDE (like PHPStorm or Eclipse PDT) that can hook into xdebug for some interactive debugging, and step through the execution of the action. You could also try uncommenting some of those debugging statements, or use good old error_log('Here' . print_r($this->data, true));
to output to the error log (marginally better than a plain print_r()
, but worse than using proper CakePHP debugging and DebugKit.
I’m hoping there’s something in there that will cast a light on the underlying cause.
Regards
Reuben Helms