missing method public function index() - cakephp-2.0

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

Related

How to get variable from AppController beforeFilter in Other Controllers action in cakePHP 2.0

I have queried on the User table inside the AppController as below
<?php
class AppController extends Controller {
public function beforeFilter() {
function beforeFilter() {
parent::beforeFilter();
if ($this->Session->read('Auth.User.id')) {
$userLoginInfo = $this->User->findByUserId($this->Session->read('Auth.User.id'));
$this->set('userLoginInfo', !empty($userLoginInfo) ? $userLoginInfo : NULL);
}
}
}
}
?>
The $userLoginInfo is available in all ctp files, but I want to access it in all other controller actions as well.
now you put this code in AppController
function beforeFilter(){
$this->set(‘accesstest’ , ‘abc’);
}
And We have to use it in Other controller file say anotherController.php
then we will use $this->viewVars.
here we will be used
$test = $this->viewVars[‘accesstest’];
$this->set('test',$test);
Your own answer is applicable if wanting to set a variable for all Views, but this was not what you were asking in your question.
If you are extending AppController correctly then you can create a property of the AppController class that would then be accessible from any controller that extends it:-
class AppController extends Controller {
public $accesstest = 'abc';
}
Then in any controller that extends AppController you can use:-
$test = $this->accesstest;
echo $test; // 'abc'
However, if you want to share a variable that you want accessible from all controllers that can be changed and you want the change remembering then use the Session:-
$this->Session->write('accesstest', 'abc');
$test = $this->Session->read('accesstest');
$userLoginInfo = $this->viewVars['userLoginInfo'];

How to implement a default view in CakePHP?

In CakePHP each method of a Controller has its own View and the view template file is the name of the method.
class DataController extends AppController
{
public function one()
{
// will render one.ctp
}
public function two()
{
// will render two.ctp
}
}
Accourding to the API documentation there is a $view property of the Controller that specifies the view to render. So I should have the ability to specify a default view file, say all.ctp, for all methods of a controller
class DataController extends AppController
{
public $view = 'all';
public function one()
{
// should render all.ctp
}
public function two()
{
// should render all.ctp
}
}
However this does not work and CakePHP ignores the $view property and continues to look for the template file of the same name as the method.
Is there a way to have a default view without having to insert $this->render('all'); in each of the Controller's methods?
The value is going to be overridden in Controller::setRequest() which is being called in the controllers class constructor.
You could use your controllers beforeFilter() callback instead to set the value:
public function beforeFilter()
{
parent::beforeFilter();
$this->view = 'all';
}

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

Resources