I have a simple class that I've added to the components folder called mixpanel.php. Within the file is:
<?php
class MetricsTracker {
public $token;
public $host = 'http://api.mixpanel.com/';
public function __construct($token_string) {
$this->token = $token_string;
}
function track($event, $properties=array()) {
$params = array(
'event' => $event,
'properties' => $properties
);
if (!isset($params['properties']['token'])){
$params['properties']['token'] = $this->token;
}
$url = $this->host . 'track/?data=' . base64_encode(json_encode($params));
//you still need to run as a background process
exec("curl '" . $url . "' >/dev/null 2>&1 &");
}
}
?>
In users_controller.php I do:
require 'components/mixpanel.php';
however I'm getting an error:
Fatal error: Cannot redeclare class MetricsTracker in /Users/Hooman/Sites/askedout/app/controllers/components/mixpanel.php on line 11
Why is this happening? I do the same thing with a different php class and it works fine. This is very odd to me as I am not repeating the require definition anywhere. Please help, thanks.
In your component code, please change the code from
class MetricsTracker to
class MetricsTracker extends Object
In your controller code , please add the following code :
var $components=array('MetricsTracker');
instead of using require() function
Related
I am using cakephp 2.0.4 (Doing Changes in existing project)
My controller function is..
class PagesController extends AppController {
public function getlocations($string = ''){
$this->autoRender = false;
$aResult = array(0=>'Florida', 1=>'London');
echo json_encode($aResult);
}
}
And also I have try $this->autoLayout = $this->autoRender = false;
When I am calling this action directly in browser mysite/app/pages/getlocations it will give following error
View file "/home/mysite/public_html/testing/app/View/Pages/g.ctp" is missing.
Create a /View/Ajax/json.ctp view:
<?php
if(!empty($data)) echo json_encode($data);
Then in the action:
$this->set('data', array(0=>'Florida', 1=>'London'));
$this->layout = false;
$this->render('/Ajax/json');
You could also make it work following the Cake way.
First you have to add the following code to your routes file:
Router::parseExtensions('json');
Next, in the controller add the 'RequestHandler' to your components array and serialize your result in your getlocations function:
public $components = array('RequestHandler');
public function getlocations($string = ''){
$this->set('aResult', array(0=>'Florida', 1=>'London'));
$this->set('_serialize', 'aResult');
}
Finally, you can see the results in your browser mysite/app/pages/getlocations.json
Doing this way CakePHP will add the application/json headers automatically.
More info: http://book.cakephp.org/2.0/en/views/json-and-xml-views.html
I have created helper name TicketHelper (has TicketsController), CommonHelper (no controller) and ExcelHelper (no controller). Inside TicketHelper it work fine when called any function of CommonHelper. Here example TicketHelper:
<?php
class TicketHelper extends AppHelper {
public $helpers = array('Session','Common');
public function myFunction(){
echo $this->Common->workfine();
}
?>
By the same action i called CommonHelper to ExcelHelper it produces error: Fatal error: Call to a member function workfine() on a non-object in
After many hours check, i found mistake problem with constructor
<?php
class TicketHelper extends AppHelper {
public $helpers = array('Session','Common');
public function myFunction(){
echo $this->Common->workfine();
}
//Here my problem constructor Note: function name and class name are the same
function TicketHelper (){
//My code here
}
/**
* It should be follow cakephp doc
* function __construct($id = false, $table = null, $ds = null) {
* parent::__construct($id, $table, $ds);
*
* }
*
*/
?>
You should not use a helper inside a controller. A helper is used for logic in the view. It is included in the controller but accessible in the view. For logic shared between controllers, use a component instead http://book.cakephp.org/2.0/en/controllers/components.html#creating-a-component.
If you still want to use a helper in the controller (not recommended):
$view = new View($this);
$myHelper = $view->loadHelper('MyHelper');
I am trying to modify FormHelper's behaviour to meet my application requirements. I would like to use native FormHelper but for all input I need to add some short message providing help to the user and describing the particular field.
My idea is to create my own helper and pass a help message as an argument. This function will modify form's inputDefaults setting and call a native FormHelper input function.
For example:
class MsgFormHelper extends AppHelper {
public function input($name, $message, $options) {
$this->_View->Form->_inputDefaults['after'] .= '<div>'.$message.'</div>';
return $this->_View->Form->input($name, $options);
}
}
But this solution notices this error:
Notice (8): Indirect modification of overloaded property
FormHelper::$_inputDefaults has no effect...
Is there some way how to modify "after" value in form's inputDefaults setting?
I probably found a solution (it works but I am not sure it doesn't violate some CakePHP's principles). Thanks for your opinions.
class MsgFormHelper extends AppHelper {
public function __construct(View $view, $settings = array()) {
parent::__construct($view, $settings);
}
public function input($name, $message, $options) {
$add_message = true;
if (isset($options['after'])) {
$options['after'] = trim($options['after']);
$add_message = empty($options['after']);
}
if ($add_message) {
$options['after'] = '<div class="input-help">' . $message . '</div>' . $this->_View->Form->_inputDefaults['after'];
}
return $this->_View->Form->input($name, $options);
}
}
You should be extending the FormHelper itself not AppHelper. Then in your controller use the aliasing feature so that you still use $this->Form->input() in your views but it would actually refer to your custom helper.
public $helpers = array('Form' => array('className' => 'MsgForm'))
I have this created:
<?php
class AppExceptionHandler
{
public static function handle($error)
{
$this->controller->redirect('www.google.com');
//echo 'Oh noes! ' . $error->getMessage();
// ...
}
// ...
}
?>
The echo will output fine but how do I use a layout or view? I keep getting this erro:
Fatal error: Using $this when not in object context in ...
Try to add the following method in you AppController.php
public function appError($error) {
//Do whatever you want
debug($error);
}
I need to change page title from default "Error" when handling errors like 404. So I need to put my the title in the variable $title_for_layout for my Layout. I tried to create custom error handling function by changing configuration in app/Config/core.php and setting the page title as in controllers
Configure::write('Error.handler', function($code, $description, $file = null, $line = null, $context = null) {
$this->set('title_for_layout', 'Vyskytla sa chyba');
});
As I expected, I got a PHP error (line 59 is the second line in the code sample)
Fatal error: Using $this when not in object context in /var/www/web/app/Config/core.php on line 59
So how I can set the title for my default.ctp layout?
Thanks.
In CakePHP 2.0, you can try the following code to achieve the same you needed.
Try this:
/app/Config/core.php
Exception render need to set as an AppExceptionRender. Example:
Configure::write('Exception', array(
'handler' => 'ErrorHandler::handleException',
'renderer' => 'AppExceptionRenderer',
'log' => true
));
/app/Controller/ErrorsController.php
class ErrorsController extends AppController {
public $name = 'Errors';
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('error404');
}
public function error404() {
//$this->layout = 'default';
$this->set('title_for_layout', 'Vyskytla sa chyba');
}
}
/app/Lib/Error/AppExceptionRenderer.php
App::uses('ExceptionRenderer', 'Error');
class AppExceptionRenderer extends ExceptionRenderer {
public function notFound($error) {
$this->controller->redirect(array('controller' => 'errors', 'action' => 'error404'));
}
}
/app/View/Errors/error404.ctp
<div class="inner404">
<h2>404 Error - Page Not Found</h2>
</div>
Insert it where you need: throw new NotFoundException();
Ref: CakePHP 2.0 - How to make custom error pages?
For < CakePHP 2.x:
If you create a custome error page view in app/views/errors then in a
php section on that error view page you can use:
$this->setLayout("Title for the error page here");
Then when you see the error page, it will have your title. Again, that
is if you set a custom error page.
Here is another way to do the same you needed.
// Create an error.php file in your /app folder with the following code:
<?php
class AppError extends ErrorHandler {
function error404($params) {
$this->controller->layout = "error";
$this->set('title_for_layout', 'Vyskytla sa chyba');
parent::error404($params);
}
}
?>