How to work out general logic in a plugin


#1

I have made a simple system for a remember-me functionality. Now I want to start using it in other applications as well so turning it into a plugin seems the most appropriate solution. I looked at the documentation about plugin creation and the examples they give are different from what I’m looking for, hence I’d like some advice.

The functionality looks like this right now:

  • In AppController->initialize() a custom method is called (AppController->checkToken()) that logs a user is if it isn’t yet and a valid token is present.
  • In UsersController->login() a custom method is called (UsersController->makeToken()) that checks if the remember-me option was checked and creates a key-token pair if so.

Now where would I put that code in a plugin? Would I create a class for it? But then who would own the class instance? And I would like to avoid having to load models unnecessarily.


#2

if you want to share your code within controllers its component, if its inside tables its behavior, anything else its trait for smal piece of code or fullblown lib if its quite a lot of code


#3

Ah, I completely forgot components exist!
That solved my problem nicely, the checkToken() part I incorporated in the components initialize() and setting the tokens is now a public function of the component.