using a component in a plugin and calling method - cakephp

I am new to cake php and I have a plugin folder in my application
app/plugins
in plugin folder I have model and vendors folder. (Notice I dont have any view or controller folder)
app/plugins/model
app/plugins/vendors
I wanted to add a component to plugin folder so I added controllers folder to it and place my component file there.
app/plugins/controllers
app/plugins/controllers/myComp.php
The method in myComp.php is
class myCompComponent extends EmailComponent {
function hilpot() {
In my vendors/shells/tasks folder I have a file called my_test.php
In the file I have included the component like
var $components = array('myComp');
in the same file, i have a method that suppose to call the myComp method
function _Maikle()
{
$this->myComp->hilpot() = array(
...................
..................
And it fails at this point with the following error
PHP Fatal error: Call to undefined method stdClass::hilpot() in C:\wamp\www\folder\app\plugins\vendors\shells\my_test.php on line 87
Any help will be appreciated
thanks

your plugin structure seems to be incorrect
/app/plugins/plugin_name/controllers/... etc
you probably didnt read the cookbook at all. otherwise you would have notices this as well as that components are in a sub directory:
/app/plugins/plugin_name/controllers/components/...
also note that you should always include the pluginname:
var $components = array('PluginName.MyComp');
and m should be capitalized: MyComp

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
}
}

CakePHP change view for action in plugin

I'm developing a plugin that has an action which decide the view to render according to data properties:
example:
class ProfilesController extends MyPluginAppController {
public function myaction($id){
//..omitting checks..
$profile = $this->Profile->read(null,$id);
//Stuff
if($this->hasDedicatedViewFor($profile)){
$this->render('profiles'.DS.$this->getDedicatedViewFor($profile));
}
//Else render default action view
}
}
While this controller was inside the APP everything was working right,
after moving into Plugin, cake says:
Error: Confirm you have created the file:
.../app/View/Plugin/MyPlugin/Profiles/myaction_secondary.ctp
While I'd expect to load it from:
.../plugins/MyPlugin/View/Profiles/myaction_secondary.ctp
While I'd expect to load it from:
.../plugins/MyPlugin/View/Profiles/myaction_secondary.ctp
It is attempting to load the default plugin path
If you trace through the code for finding template files it is checking multiple paths for plugin view files:
$paths = $this->_paths($plugin);
$exts = $this->_getExtensions();
foreach ($exts as $ext) {
foreach ($paths as $path) {
if (file_exists($path . $name . $ext)) {
return $path . $name . $ext;
}
}
}
The path in the error message is the last place CakePHP looks for a plugin view path, but it should also be checking the plugins directory. Debug the above code to see where CakePHP is looking for files if in doubt.
The path being wrong in the error message probably indicates using an out of date version of CakePHP; it's always, always a good idea to maintain your applications running the most recent maintenance release for the major version you are using. From the info provided that's 2.7.3 at the time of this answer.

Cakephp custom config file

Basically I want to create file in Config folder, for example app/Config/emailProvider.php where I can save name of the class to instantiate.(Something similar to database.php)
The problem is I don't know how to load file, I've tried with
Configure::config('email', new PhpReader());
Configure::load('emailProvider', 'default')
but I always get an error CORE/Cake/Core/Configure.php line 298 → PhpReader->read(string)
Well, first of all, let's assume that your config file app/Config/emailProvider.php contains the following contents:
<?php
$config['MyTestApp']['email'] = 'bob#gmail.com';
.....
Next thing is that you need to tell your app to load the custom config file by including it in bootstrap.php inside app/Config:
Configure::load('emailProvider');
Then, you can access it from your controller as below:
$email = Configure::read('MyTestApp.email');
Reference link

saving the xml file generated through cakephp 2.3 serialize function

I am using cakephp 2.3 and using the default code from the cookbook. The xml is generated automatically, without having to create any view files.
class PostsController extends AppController {
public function index() {
$this->set(’posts’, $this->paginate());
$this->set(’_serialize’, array(’posts’));
}
}
However, I do not want to display the XML. Instead I want to save the generated XML file in the document root upon click of a button as Posts.xml. How can I do this? Please help.
You might not have look thoroughly enough:
http://book.cakephp.org/2.0/en/controllers/request-response.html#sending-files
It clearly states how to send files with the appropriate headers via response object.
So in your controller action, add:
$this->response->download('filename_for_download.xml');

Cakephp can't locate core helper: JqueryEngineHelper.php when on server

When I run my app on my locale machine it works fine, but when I run it on my sever using cpanel, everything works fine expect when I use js helper using jquery library the following error occurs
Error: jqueryEngineHelper could not be found.
Error: Create the class jqueryEngineHelper below in file:
app/View/Helper/jqueryEngineHelper.php
<?php
class jqueryEngineHelper extends AppHelper {
}
Nb: all the files exists.
It's incorrectly looking for "jqueryEngineHelper" instead of "JqueryEngineHelper" which most likely means you didn't use correct casing when specifying the helper in controller. Make sure you have public $helpers = array('Js' => array('Jquery')); with capital "J" for "Jquery" and not "jquery"
Try with App::uses() to load the helper.
http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#loading-classes
Example from CakePHP documentation:
App::uses('HtmlHelper', 'View/Helper');
You try:
App::uses('jqueryEngineHelper', 'View/Helper');
EDIT:
Try putting that in your AppController.php at your starting lines in your code.

Resources