How cakephp2 call method another helper to current helper? - cakephp-2.0

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');

Related

Cakephp3.0 I am calling Postcategories controller into Appcontroller and it is error " Call to undefined method Cake\Core\App::import() "

My Code tries to fetch all Main categories of the posts into Appcontroller to show on the homepage:
namespace App\Controller;
use Cake\Core\App;
use Cake\Controller\Controller;
class AppController extends Controller
{
public $helpers = ['Html', 'Form', 'Session','Time','Tree'];
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->maincategories();
}
function maincategories(){
App::import('Controller','Postcategories');
$postcates = new PostcategoriesController;
$postcates = $postcategory->find('threaded');
}
}
Your maincategories() method is wrong. You need the model, not the controller to retrieve the data from. You need to use TableRegistry::get('Postcategories') to get the Postcategories model and then call the find on that:-
public function maincategories()
{
$Postcategories = TableRegistry::get('Postcategories');
$this->set('postcategories', $Postcategories->find('threaded'));
}
$this->set() is setting the categories as a view variable ($postcategories). You will need to make sure you include use Cake\ORM\TableRegistry; at the top of your AppController file.
Make sure you've fully read the docs on retrieving data.

CakePHP + nuSOAP - exposing a global function as a closure or using models outside of the controller?

I need to implement a SOAP service in a CakePHP controller. The exposed function names need to be in a global namespace (as in "Authenticate", not "SOAPController.Authenticate"). This forces me to implement Authenticate in the global namespace, since nuSOAP will deduce where to look for the function from its name. On the other hand, on the example below, I cannot use CakePHP's API, because I have no access to SOAPController's this. That is why I thought I should somehow declare some kind of a closure, that would resolve to 'Authenticate' in the global scope, while keeping a reference to this through bound parameter. Is that possible? Below is the erroneous code:
<?php
App::uses('AppController', 'Controller');
App::import('Vendor', 'nusoap',
array('file' => 'nusoap'.DS.'lib'.DS.'nusoap.php')
);
/**
* SOAP Controller
*
*/
class SOAPController extends AppController {
public function index()
{
$namespace = '';
$server = new soap_server();
$server->debug_flag = false;
$server->configureWSDL("PAI2WSDL", $namespace,
"http://localhost/pai2/SOAP/?wsdl");
$server->wsdl->schemaTargetNamespace = $namespace;
function Authenticate($login,$haslo){
$this->log("TEST"); //won't work!
return true;
}
$server->register('Authenticate',
array('ContractorId' => 'xsd:int', 'Password'=>'xsd:string'),
array('return' => 'xsd:boolean'),
$namespace,
$namespace . '#Authenticate',
'rpc',
'encoded'
);
$HTTP_RAW_POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA'])
? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
$server->service($HTTP_RAW_POST_DATA);
$this->autoRender = false;
exit();
}
}

Cakephp country helper

I have a problem with this helper : https://github.com/kshakirov/cakephp-lang-helper
This helper give me this error and I don't find why :
Fatal Error
Error: Call to a member function input() on a non-object
File: \app\View\Helper\LangHelper.php
Line: 670
I am guessing the problem is that LangHelper overrides the parent __construct method, preventing Cake from setting up the Helper correctly. Change LangHelper's __construct() to the following:
public function __construct(View $View, $settings = array()) {
parent::__construct($View, $settings);
$this->mapper = $this->parseLangHeaders();
$this->langCode = $this->findLangCode();
$this->countryCode = $this->findCountryCode();
}
Did you activate the FormHelper in the AppController?
App::uses('FormHelper', 'View/Helper'); // Don't forget this one in Cake 2.x
class AppController extends Controller
{
public
$helpers = array('Form');
}

cakephp call a function of another model error

I have a site develop in cakephp 2.x
I want into my controller call a function of another controller like this:
class ProductsController extends AppController {
public $name = 'Products';
public $scaffold;
public $uses = array('Product','Unit');
public function testFunction(){
$this->loadModel('Unit');
$this->Unit->test();
}
}
The function test into UintController.php is this:
public function test(){
echo("test");
}
My model name are Product and Unit.
When I call the function test give me this error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'prova' at line 1
In the function now is empty but give me this error.
I have tried with:
public $uses = array('Unit');
and to cancel the line with $uses.
How can I solve it?
To call a function from another controller you can use the requestAction:
Definition
"This function calls a controller’s action from any location and returns data from the action. The $url passed is a CakePHP-relative URL (/controllername/actionname/params). To pass extra data to the receiving controller action add to the $options array".
Usage
This is what your code would looks like:
class ProductsController extends AppController
{
public $name = 'Products';
public $scaffold;
public $uses = array('Product','Unit');
public function testFunction() {
// Calls the action from another controller
echo $this->requestAction('/unit/test');
}
}
And then in the UnitController:
class UnitController extends AppController
{
public function test()
{
return 'Hello, I came from another controller.';
}
}
Warning
As said in the CakePHP Cookbook:
"If used without caching requestAction can lead to poor performance. It is rarely appropriate to use in a controller or model".
Best solution for you
But, the best solution for you, would be to create a function inside a model and then call from your controller, like this:
class ProductsController extends AppController {
public $name = 'Products';
public $scaffold;
public $uses = array('Product','Unit');
public function testFunction() {
echo $this->Unit->test();
}
}
And in the Unit model:
class Unit extends AppModel
{
public function test(){
return 'Hello, I came from a model!';
}
}

Define global variable for Models and Controllers at CakePHP 2.2

Currently i am using something like this:
//at bootstrap.php file
Configure::write('from', 'mymail#mydomain.com')
//at controllers or models files
$var = Configure::read('from')
The thing is, i would like to manage that variable through the database to be able to modify it in a simpler way.
I was thinking about doing it with AppModel but then it would only be accessible for Models and not controllers.
What should I do in this case?
Thanks.
You can create a separate model / plugin which will be mapped to a configuration table in your database. Then load it through $uses statement for controllers and App::import() for models.
class SystemSetting extends AppModel {
/**
* Return a list of all settings
*
* #access public
* #return array
*/
public function getSettings() {
return $this->find('all');
}
}
Then, in your controller:
class SomeController extends AppController {
var $uses = array('SystemSetting');
public function displaySettings() {
$settings = $this->SystemSetting->getSettings();
// .. your code
}
}
In model:
App::import('Model', 'SystemSettings.SystemSetting');
$settings = new SystemSetting();
$mySettings = $settings->getSettings();
This works just fine. Of course, you might also load settings in both AppController and AppModel to follow the DRY rule.
create the getSettings in your AppModel
in AppController you can write this method:
public function getSettings() {
return $this->{$this->modelClass}->getSettings();
}
this way the getSettings() method is available in any model and any controller
any model call:
$mysettings = $this->getSettings();
any controller call:
$mysettings = $this->MODELNAME->getSettings();

Resources