cakePHP : x_with in action / function causes errors... repeatable? - cakephp

In a controller, make a dummy action, name it like stackoverflow_with_something()//the WITH part is important
Create the view stackoverflow_with_something
Load up the page in a browser. Get a 'missing view error'
Take out the _with, try again. No error.
I havent seen any documentation on this, whats the deal?
short version : Why does xxxxx_with_xxxxx crash cakePHP, but xxxxx_xxxxx dosent?

No problems here, tried with the following:
/app/controllers/foos_controller.php:
<?php
class FoosController extends AppController {
var $uses = null;
function bar_with_yadda() {
}
}
?>
/app/views/foos/bar_with_yadda.ctp:
Hello
requesting /foos/bar_with_yadda returns no error.

Related

Error: Call to undefined function recaptcha_check_answer() in CakePHP 2.5.1 (PHP 5.3.28)

I am trying to add google's reCAPTCHA in my CakePHP application's login and registration pages. But it showing following error:
Fatal Error
Error: Call to undefined function recaptcha_check_answer()
File: C:...\app\Controller\UsersController.php
in the UsersController , i have following line which is indicated in the error message.
$recaptchaResp = recaptcha_check_answer(Configure::read("Recaptcha.privateKey"),
$_SERVER["REMOTE_ADDR"],
$this->params['form']["recaptcha_challenge_field"],
$this->params['form']["recaptcha_response_field"]);
Could anyone tell me please what 's wrong in this code ? is it missing any file like recaptchalib ? if so, where can i get this library for CakePHP 2.5.1 ?
There is a plugin for google reCaptcha in cakephp. You can download it from Here
To use the recaptcha plugin its required to include the following two lines in your /app/Config/bootstrap.php file.
Configure::write('Recaptcha.publicKey', 'public-api-key');
Configure::write('Recaptcha.privateKey', 'private-api-key');
Controllers that will be using recaptcha require the Recaptcha Component to be included. Through inclusion of the component, the helper is automatically made available to your views.
public $components = array('Recaptcha.Recaptcha');
In the view simply call the helpers display() method to render the recaptcha input:
echo $this->Recaptcha->display();
To check the result simply do something like this in your controller:
if ($this->request->is('post')) {
if ($this->Recaptcha->verify()) {
// verified
} else {
// display the error
}
}

How to properly exit from a custom blackhole handler?

My problem is fairly basic: whenever an action in CakePHP is blackholed, I want to display a custom error page; the default behaviour of Cake to display a "File not found" message confuses the hell out of users (not to mention developers). So I came up with this, by searching the docs and StackOverflow:
class TestsController extends AppController
{
public $components = array ('Security');
public function beforeFilter ()
{
parent::beforeFilter ();
$this->Security->blackHoleCallback = 'blackhole';
$this->Security->csrfExpires = '+5 seconds'; // for testing
}
public function index ()
{
}
public function doit ()
{
$this->log ('Performing request; entry = ' . $this->data['foo'], 'tests');
$this->set ('foo', $this->data['foo']);
}
public function blackhole ($type)
{
$this->log ('Request has been blackholed: ' . $type, 'tests');
$this->render ('/Errors/blackhole');
$this->response->send ();
exit ();
}
}
In index.ctp there is a simple form with a single textbox, that commits to doit (excluded for brevity). This works, but I have one major issue: the exit() in the blackhole() function. The problem is, if I do not exit here doit() is still called, even if the request is blackholed, as evidenced by the log:
2013-01-30 15:37:21 Tests: Request has been blackholed: csrf
2013-01-30 15:37:21 Tests: Performing request; entry = kfkfkfkf
This is clearly not what you expect. The Cake documentation hints at using (custom) exceptions to stop processing in blackhole(), but that:
completely beats the purpose of using a custom handler;
adds another layer of complexity to something that should be simple.
My question is: is there a proper way to do an "exit" from blackhole() so that Cake does all the rendering/cleanup/etc. that it usually does; I already had to add $this->response->send() to force output to the browser. Or, althernatively, a way to tell Cake to skip calling doit() after blackhole().
My suggestion would be to redirect in your blackhole.
This is done e.g. here in the cookbook http://book.cakephp.org/2.0/en/core-libraries/components/security-component.html#usage
$this->redirect(array('controller' => 'test', 'action' => 'index'));
A redirect will issue an exit (http://book.cakephp.org/2.0/en/controllers.html#flow-control).
You can also send something nice to the user if you want, before the redirect:
$this->Session->setFlash('What are you doing!?');
In your blackhole callback you could just throw an exception with required message. That would render the proper error page and get logged too (assuming you have turned on logging for errors in core.php).

Stop execution after render in beforeFilter of CakePHP

In my CakePHP 2 application i have a problem with beforeFilter.
In this thread it worked well. Because of old version of CakePHP.
In my code, If user is not authorized, I want to show him "anotherview.ctp".
I don't want to redirect visitor to another page. (because of adsense issues)
When i use "this->render" in beforeFilter, the code in my "index" action is also run.
I want to stop the execution after the last line of "beforeFilter".
When I add "exit()" to beforeFilter, it broke my code.
How can I stop execution in beforeFilter without breaking code?
class MyController extends AppController {
function beforeFilter() {
if ( $authorization == false ) {
$this->render('anotherview');
//exit();
}
}
}
function index() {
// show authorized staff
}
}
Try:
$this->response->send();
$this->_stop();
I stumbled across this thread while trying to do the same thing. The accepted answer works but omits an important detail. I'm trying to render a layout (not a view) and I had to add an additional line to prevent the original request's view from throwing errors.
Inside AppController::beforeFilter():
$this->render(FALSE, 'maintenance'); //I needed to add this
$this->response->send();
$this->_stop();
or alternatively - redirect to another view:
if ( $authorization == false ) {
$this->redirect('/users/not_authorized');
}
For CakePHP 3.5 this is what worked for me:
$event->setResult($this->render('anotherview'));
This will also enable you to use the debugger. CakePHP Debugger stopped working when I used the exit; statement.

cakephp REST problem

** SOLUTION **
I am using cakephp 1.3. I did not have to add any routes. All i did is after adding the action to the controller, I went to the url
/controller/action/id
in my case it was
/test/bravo/123
and that did the trick
** End **
I am using cakephp 1.3 and I am trying to add webservice. I heard its very easy to add restful. What I want to do is to have one of the actions in my controller provoked and log into a logfile as a success. Lets say
function bravo(){ $this->log("success", "Testlog");
So far I went through documentation and added the following to my routes.php
Router::mapResources('tests');
Router::parseExtensions();
then in test controller I have this
<?php
class TestsController extends AppController {
var $name = 'Tests';
var $components = array('RequestHandler');
.....
.....
function bravo(){
$this->log("success", "Testlog");
}
?>
Now when I go to
/tests.bravo
, its not logging the success in the log file. What am I doing wrong. Thanks

Can I make CakePHP return a suitable status code based on certain conditions?

This question is slightly related to my old post Dealing with Alias URLs in CakePHP
After much thought, I am exploring the option of having a custom 404 script in my Cake App, that is reached when a URL does not map to any controllers/actions. This script would check $this->here and look it up in a database of redirects. If a match is found it would track a specific 'promo' code and redirect.
I'm thinking status codes. Can I make my script return a suitable status code based on certain conditions? For example:
URL matches a redirect - return a 301
URL really does not have a destination - return a 404.
Can I do this?
EDIT:
What about this? Anyone see any problems with it? I put it in app_controller.
function appError($method, $params) {
//do clever stuff here
}
This should work. Assuming you redirect 404's at a LegacyUrls::map() controller action. The code needs to be stored in app/app_error.php:
<?php
class AppError extends ErrorHandler{
function error404($params) {
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch('/legacy_urls/map', array('broken-url' => '/'.$params['url']));
exit;
}
function missingController($params) {
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch('/legacy_urls/map', array('broken-url' => '/'.$params['url']));
exit;
}
}
?>
Good luck!
I've always created app\views\errors\missing_action.ctp and app\views\errors\missing_controller.ctp
Cake will automatically display one of those views when a url does not map out to a controller or its methods.
Unless there is a certain need for the error codes that you did not give, this would work perfectly!
I'd like to augment felixge's answer.
This version outputs a 404 error to the browser:
class AppError extends ErrorHandler
{
function _outputMessage($template)
{
if ($template === 'error404') {
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch('legacy_urls/map', array('broken-url' => '/'.$params['url']));
return;
}
parent::_outputMessage($template);
}
}

Resources