I am developing web application using the laravel. I would like to 'package' some of the functions, which is re-usable. For example, I got a UserController
, with database setup, and view, etc. The UserController
is expected to re-use in most of the web development in the future, but some UserController
may have different behavior, for example, some application's user may have a field rewardPoint
, some may not. But they all have some similar behaviors, for example: register
, login
, forgetPassword
, etc. So, can I package out a common UserController
with all the basic database setup into one file or script to reduce the development work load? Thanks.
Asked
Active
Viewed 1,215 times
2
-
What do you mean by _re-usable_: Re-usable within your current project or across many sites? – Adrenaxus Apr 05 '13 at 12:42
1 Answers
3
You've hit the nail on the head already - since your requirements are likely to change between projects you will struggle to create a single solution that works everywhere. You have a few options, including:
- Copy the useful classes from your last project and update them to fit.
- Create a package or bundle of base classes that include common and reusable code, then extend these into your project and modify the extensions as needed.
- Attempt to create a "one solution for all" that you can drop in to all of your projects.
I would steer away from 3; it's likely to add the most bloat for the least gain. 1 is useful whilst you get used to the framework, especially if you tend not to revisit old projects; over time you will refine your code and develop patterns of work. Eventually you will probably end up doing 2; you can store these in git, use bundles or composer packages, and you can continue to improve them over time.

Phill Sparks
- 20,000
- 3
- 33
- 46