问题
I am new in cakephp and I want to import controller in my controller so i use the below syntax. This is my controller in which I import user controller:
users_controller.php
function api_work(){ $data = $this->User->find('all'); $this->set('data' , $data); }
plays_controller.php
function api_show()
{
$this->layout= false;
App::import('Controller', 'Users');
$Users = new UsersController;
$Users->constructClasses();
$data = $Users->api_work();
pr($data); //it not display anything and shows error like undefined varia
ble
}
Controller is successfully imported. Question is how can I data return from api_work() function?
回答1:
CakePHP is a MVC (Model View Controller). The api_work function in users_controller should instead be in the User model (User.php). Alternatively you could do $this->User->find('all');
in the plays_controller.php instead of $Users->api_work()
.
Above suggestion means you would need to remove all these lines:
App::import('Controller', 'Users');
$Users = new UsersController;
$Users->constructClasses();
You will also need to make sure that you add the following at the top of users_controller.php (just under the class declaration).
$uses = array([...], 'User'); OR $uses = array('User');
I really recommend reading the CakePHP book ( http://book.cakephp.org/ ).
来源:https://stackoverflow.com/questions/10958727/cakephp-import-controller