Cakephp country helper - cakephp

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

Related

missing method public function index()

Missing Method in BusinessController
Error: The action index is not defined in controller BusinessController
Error: Create BusinessController::index() in file: app/Controller/BusinessController.php.
If I add this method, it brings me to the homepage. I want to go business_index.ctp
Here is my controller
App::uses('Controller', 'Controller');
class BusinessController extends AppController {
public $name = 'Business';
public $components = array('Auth','RequestHandler','Cache');
public $uses = array('User','CreditHistory');
public function beforeFilter(){
parent::beforeFilter();
}
function Sync(){
$this->layout = $this->autoRender = false;
}
//Admin Dashboard
public function business_index(){
$this->set('title','Dashboard');
$uid = $this->Auth->User('id');
$this->set('user_id',$uid);
}
I have a view name business_index.ctp inside a folder name Business
Not sure what I am doing wrong.
You could either do redirect $ this-> redirect ('business_index'); in your index method or add router rule in config/routes.php. See http://book.cakephp.org/2.0/en/development/routing.html

Cakephp 1.3 helper not found in default.ctp

The controller
class PagesController extends AppController
{
public $helpers = array('Formatacao');
.
.
.
}
The helper
class FormatacaoHelper extends AppHelper
{
var $name = 'Formatacao';
.
.
.
}
I'm getting this error in layout:
Undefined property: View::$Formatacao [APP\views\layouts\default.ctp, line 51]
Trying to use like this: $this->Formatacao->get_clean_base_url(false);
What i'm missing?
Best regards.
You are not supposed to add the helpers to CakePHP's PagesController.
Do that in AppController instead:
class AppController extends Controller {
public $helpers = array('Formatacao');
}

How cakephp2 call method another helper to current helper?

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

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!';
}
}

CakePHP: Find if is mobile browser in a helper (no access to request handler)

I need to know in a helper in a CakePHP application if the device is mobile, I would love to use $this->RequestHandler->isMobile(), but the request handler component is not available in helpers. Any ideas?
Thanks!
You can import the class and use it anywhere in the framework like so:
App::import('Component', 'RequestHandler'); // import class
$requestHandler = new RequestHandlerComponent(); // instantiate class
$isMobile = $requestHandler->isMobile(); // call method
var_dump($isMobile); // output: bool(true) or bool(false)
(Tested from helper and gives correct results for Firefox and iPhone)
Also, any options you set in the Controller::helpers property will be passed to the helper:
class AppController extends Controller {
public $components = array(/*...*/, 'RequestHandler');
public $helpers = array(/*...*/, 'MyHelper');
public function beforeFilter() {
$this->helpers['MyHelper']['mobile'] = $this->RequestHandler->isMobile();
}
}
You can catch the options array in your helper's constructor:
class MyHelper extends AppHelper {
protected $_defaultOptions = array('mobile' => false);
public function __construct($options) {
$this->options = array_merge($this->_defaultOptions, $options);
}
}
The accepted answer suggests using a component inside a helper which should be avoided as components are for use solely in controllers and will result in errors as mentioned by Anupal.
The simple solution is to use the CakeRequest class that RequestHandlerComponent uses. So in your helper you can do:-
App::uses('CakeRequest', 'Utility');
$isMobile = (new CakeRequest())->is('mobile');

Resources