1

How do I call this function in a view (.ctp file)

The actual function is defined in the UserController class

 function verbose_log($msg) {
    date_default_timezone_set('Asia/Calcutta');
    $today = date("Ymd");
    $timestamp = time();
    $filename = "errorlog";
    if (!file_exists($filename)) { 
        echo "The file $filename exists";
        $ourFileHandle = touch($filename) or die("can't open file");    
    } 
    $fd = fopen($filename, "a");
    $str = "${today}|${timestamp}|${msg}";
    fwrite($fd, $str . PHP_EOL);
    $timestamp ='';
    fclose($fd); 
}
4
  • 4
    You don't call the function in the view, you call it in the controller and pass the results to the view. Could you give an example of what you're trying to do? Commented Dec 2, 2011 at 16:34
  • If you want to call a function from the view, it needs to be in bootstrap.php. But you'll lose any kind of object functionality. Commented Dec 2, 2011 at 16:35
  • in controller i written log function, am doing payment process, when the payment process is success am redirecting page to .com/paymentresp, i have called paymentresp, in this i want to call the log function Commented Dec 2, 2011 at 16:37
  • You should elaborate on the question or post some code. I'm willing to bet you want to do this because you think it is the only way to accomplish something that shouldn't be accomplished by calling a controller function from a view. you will most likely end up in a redirect loop when trying to do that. Commented Dec 2, 2011 at 16:37

3 Answers 3

1

As stated by others, you should call another controller method from your controller action:

class UsersController extends AppController {

    public function paymentresp() {
        // do stuff
        $this->_verbose_log($logMessage);
        // do more stuff
    }

    protected function _verbose_log($message) {
        // log stuff
    }
}

(By prefixing the method name with an underscore [the convention for protected methods], people won't be able to run this as a controller action by visiting http://example.com/controller/verbose_log)

Also, all CakePHP objects inherit a log method which calls CakeLog internally. You could use this existing functionality instead of implementing it yourself:

class UsersController extends AppController {

    public function paymentresp() {
        // do stuff
        $this->log($logMessage, 'error');
        // or
        CakeLog::write('error', $logMessage);
        // do more stuff
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Move your function to bootstrap.php, and you can run it from anywhere.

1 Comment

This is technically correct, but most of the time bootstrap.php is the worst place to put functions as it breaks the MVC pattern.
0

Using requestAction you can call a controller method in to your view page.

e.g.

$this->requestAction('/ControllerName/MethodName/');

2 Comments

but not in this case. its just utterly wrong to do anything like this in the above use case. nothing helps against bad design. the method should be in a lib class - or some static method class etc. PS: also, if you need to use requestAction in 99% of all cases it is also bad design.
Yes, I agree with you. It is a bad design but this is the only handy technique to do so and top of that it provided the the Cakephp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.