I recently ran into an issue where I found I was creating a new user profile system for different systems and then it occurred to my: DRY. DON’T REPEAT YOURSELF. Yet, in the bustle of activity I realized there was too much in common with these user profiles to rewrite the code.
So why not extract the user-related code and put it all in its own bundle for easier maintainability? After all, in Symfony, it’s a breeze working between bundles, from one bundle you can call code from other bundles. It was a “duh” moment, to say the least.
So how do you move some code you wrote that works to its own bundle? Well, it took me less than 5 minutes to move 6 entities, 7 controllers and their supporting code like routing and update to the new bundle.
Step #1: Go to your command line and type: “app/console generate:bundle”
I went through the Symfony tool and called it “UserBundle”. Symfony will automatically create all the base and template files and folder structure for you new User Bundle.
Step #2: Now, find all code that you already wrote and move it all to your new bundle following the same folder structure.
Step #3: Do a search and replace all paths to your old bundle where the “namespace” and “use” declarations are and replace with the path to your new User Bundle.
Step #4: Update your routes with the new paths, where applicable to make sure they are pointing to your new bundle’s controllers.
Step #5: Clear your cache with “app/console ca:cl –env=prod” then “app/console ca:cl”
Step #6: Type in the URL that matches the path that will execute the controller code with the “app_dev.php” debug in the URL and test for errors.
Step #7: Congratulate yourself for having the foresight to using a PHP framework, imagine the logistics if you wanted to move your code from some home-grown system what pitfalls you could have encountered. Aren’t Symfony web developers just luckier than the plain old PHP developer variety? Well, we’re luckier because there are tools and helpers already waiting for us so we can focus on writing code that creates solutions, not writing the code that helps us write the code to create solutions. All you companies with your own Web Development department — are you getting this yet?! If not, your loss. Literally….