0

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( '[email protected]','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 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

My knowledge of OOP & MVC comes from Objective-C and Python so I struggle a bit with the differences between those approaches and PHP. I looked at CakeDC Users a year ago when I started this project but either I couldn't get it working or it wasn't quite ready yet. I know how to get the user ID by calling $this->Auth->user('id'), but this is for an API so the user won't be logged in (user/pass is passed with the Post request).
I realize calling one controller from another is not the right way to do things here. So what would be the right way to respond to a Post call with user/pass, check those values, and give them info from a different model?
@nickv2002 It seems to me userIdFromUsernameAndPassword() should be a method of the User model, e.g. Accessing it should then be something like ClassRegistry::init('Usermgmt.User')->userIdFromUsernameAndPassword('[email protected]','pass'); you should not use another Controller to get data from a Model in MVC. Optionally, wrap if in a Component to provide re-usable code as suggested.
What you have is a stateless auth, this can be still done through the Auth component and an authentication adapter. As I've said before, read the Auth chapter of the book. ;)
Thanks guys, I rewrote it so that most of the action happens in a component instead and now it's working.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.