Writing A User Module - How To Architect?

Hey guys,

I’m in the process of writing a user module for my app. I was thinking of making it somewhat generic so that I could reuse/share it, but I wasn’t sure how to architect it.

Specifically, I was thinking of trying to make it into a composer package to fit the new Yii2 architecture. The problem with that is that user authentication always varies from app to app (ie business logic), so I can’t really make a composer package unless I put in numerous flags to try to account for every scenario. Realistically, developers will need to dive into the code and make modifications as needed for business logic, views, db columns, etc.

Additionally, I’m not sure how to transition from package -> module. Would developers just copy/paste the code from the vendor dir into their modules folder? But then what’s the point of using a composer package when a composer update would effectively do nothing?

With that in mind, it seems like it would just be better to package a zip file for developers to extract and modify - no composer at all. What do you think?

A module does not have to be in a special module folder. You can put it anywhere you like e.g. in vendors/…

See how it is done with gii and debug module. They are already composer packages. You install them by just adding


'gii' => 'yii/gii/Module'

to your config.

Ok, I created a composer package module. It works as a good base, but how can I start "extending" the package?

Let’s say the module is 95% good and that I just need to change a few functions in the User model. It seems like there’s no easy way to extend just that one class; I would need to extend it all. And if that’s the case, then it seems like I would need to either copy the entire package or fork it (+change the namespaces).

Or is there a better way?

I will be porting my yii-usr module to Yii 2.0 soon. In this module I’ve created a set of different interfaces that must be implemented in the UserIdentity class, which is provided by the dev, not by the module. This allows to implement business logic matching a specific project.

In Yii 2.0 there is no UserIdentity class no more, but the design of my module can stay the same.

Hmm that’s an interesting way to go about it. I’ll check out your project to see how you implemented it exactly, thanks.

There is IdentityInterface.

Depends on the list of features/functionality you plan to bundle. I would strongly suggest - as a safe approach to use the composer route. The reason being you can bundle all the functionality of third parties without you regularly needing to update your code version and pushing it.

With regards the user module - I was planning one as well (more for a wider use with social connectivity theme). But I may delay it until Yii2 is stabler or wait for all similar extensions from folks like you and then decide. In this process I have built some reusable extensions (and working on a few more) that can be used across custom modules or your custom app. Some of these could be useful for your user module as well:

[list=1]

[*]yii2-widgets

[*]yii2-helpers

[*]yii2-icons

[*]yii2-password

[/list]

Wishing the best for your module.

I’ve already ported my module to yii2 as yii2-usr. The only thing left to do is porting the HybridAuth support for social sites login.

While porting, I took the opportunity to do some major refactoring to make it’s components loosely coupled.

If anybody is interested, I invite you to do a review and/or post feature requests (on Github).

Thanks, will check this and update.