Cakephp导入控制器 [英] Cakephp Import Controller
问题描述
我是cakephp的新手,我想在控制器中导入控制器,因此我使用以下语法。这是我导入用户控制器的控制器:
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
}
控制器已成功导入。
问题是如何从api_work()函数返回数据?
Controller is successfully imported. Question is how can I data return from api_work() function?
推荐答案
CakePHP是MVC(模型视图控制器)。 users_controller中的api_work函数应该位于用户模型(User.php)中。或者,您可以在plays_controller.php中执行 $ this-> User-> find('all');
而不是 $ Users-> ; api_work()
。
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();
您还需要确保将以下内容添加到users_controller.php的顶部(
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');
我真的建议您阅读CakePHP书( http://book.cakephp.org/ )。
I really recommend reading the CakePHP book ( http://book.cakephp.org/ ).
这篇关于Cakephp导入控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!