Cakephp Import Controller

老子叫甜甜 提交于 2019-12-24 03:22:39

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!