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

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

报告相同问题?