I am new to OOP and MVC, but have been programming procedurally with PHP for several years. Now its time to dive into OOP and MVC. I have installed a new web app with Yii and need a bit of direction. My client wants me to fork a website from the procedural PHP I coded to the Yii framework using OOP. The website contains recipes, comments, user registration, submit recipes, comment on them, manage their account, etc. Also, will be adding a new feature of user badges and tags similar to how SO uses them.

My question is how to come up with a plan of attack using OOP. Would it be best to have a controller for each feature of the website, like RecipeController, and TagsController, and ViewRecipeController, BadgesController, AccountController, etc. or is that too simplified?

Sorry, but with this being my first venture into OOP, and a framework, I'm a bit hesitant to start blindly. I have seen plenty of Hello World tuts but they don't reveal much else than the very basics. I'm looking for more information about planning the project and how to break it down into parts so it is logical and I won't have to start over a week from now because I didn't think of something or didn't know at the time. Just trying to get all my ducks in a row first.

Thanks for the assistance!

link|flag

2 Answers

If you are first time with Yii, try to create a one feature with Yii. Just place yii to a subfolder and start implement one option of old site. You learn more in process and will find a future solutions for your site during implementation.

So just start =)

link|flag

The Yii approach to this is to start with data abstraction - as a matter of fact, it is a good starting point for any general OO project.

So first, you'd need to think about what type of objects (i.e. classes) will be the building blocks of your site. Recipe is a natural one. User is another. A comment might be a base class, or maybe you'd want to do a more general apporach and create an annotation class - which could serve as marking all user interaction (comment, like, vote, bookmarks, whatever) on a given piece of content (i.e. recipe). These are design decisions that you'll need to tackle at the very beginning of your project.

If you have your set of classes, think about what properties will they have. A recipe has a title, a long description (directions for preparing), list of ingredients, difficulty, etc. A user has a username, email address, password, profile picture, DOB, location, etc.

By looking at those classes with all the properties, it might be the time for your first refactoring - you might realize that ingredients need their own classes (if you provide detailed information and pictures of different ingredients on your site).

By interactively defining and refining these object types and their properties you'll reach a point where you say you're done for the moment. As a result of this process you have your data model.

In yii, you build your application starting from designing the database schema, so now you have to turn all your object types and their properties into table definitions. The beauty of yii is that from the db schema it will create the appropriate data model classes (the php class wrappers around your database), and the necessary controllers and views for the basic CRUD operations (CRUD stands for create, read, update, delete).

Even though you only defined your database tables so far, you've already reached a point where you can add/list/delete recipes and users, and all this comes with (almost) automatic data validation both at the client and the server side. Pretty neat, I think.

Of course, your real world application will be much more complicated than just the basic CRUD operations, so here is the point where you start to define custom controller methods and views to implement more sophisticated functionality.

As guidlines:

  1. make sure your data model is a good abstraction of the real world problem you're facing
  2. make sure you follow this tutorial about how to create a fully functional bloggin system with yii
  3. make sure you search the yii extension repository to find already written modules that you might need. The whole user registration is right there, for example, you only need to drag and drop it into your installation and you're done.
link|flag

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.