douxian5076 2013-04-20 01:06
浏览 40
已采纳

CakePHP:使用主控制器中插件控制器的功能

This should be simple but I've spent over an hour trying to figure it out so thanks for your help.

I've got a CakePHP plugin, Usermgmt, with a controller located here:

./app/Plugin/Usermgmt/Controller/UsersController.php

I'm trying to call a function, userIdFromUsernameAndPassword(), in that controller from one of my main controllers using something like:

$userID = $this->UsersController->userIdFromUsernameAndPassword( 'user@host.com','pass' );

What do I need to import/include/initialize to be able to get this working?

I've tried various import statements such as App::uses('UsersController', 'Usermgmt.Controller'); at the top of my file, but haven't gotten anywhere.

Thanks!

  • 写回答

1条回答 默认 最新

  • dongzouhe9734 2013-04-20 01:32
    关注

    Short answer: Use OOP and extend the other controller. Also get an understanding of MVC. You are not supposed to use a method of a controller inside another controller, in CakePHP this should be done as a component. They can be shared between controllers. Check the CakePHP Book.

    Also the name of the plugin and the method name indicate that this is a bad plugin. This sounds like somebody did not know about the Auth component of CakePHP. Again, check the book for the AuthComponent. You want a custom authentication adapter.

    If the user is logged in you can get its id by calling $this->Auth->user('id'). Read the chapter about Auth. If you want a properly done user plugin check out: CakeDC Users

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集