Latest blog post: 10 Epic Dribblers That You Should Be Following →

Posts Tagged ‘methodology’

PHP Project planning for idiots – MVC design pattern

Proper planning is arguably the most important aspect of any project. Without a plan projects will exceed deadlines, coders will create redundant code and many projects won’t even see the light of day. So my goal with this is to help the beginners out there so they don’t have to go through what I did as a novice PHP programmer. Hopefully this will get you on the right track with proper project planning and creating your app using the Model View Control (MVC) design pattern.

Requirements: This tutorial assumes that you are familiar with OOP in PHP as well as Classes and have some basic knowledge of databases.

What is MVC?

MVC stands for Model View Control. It is an architectural pattern that attempts to keep your code more organized and running more smoothly and helps others to pick up where you left off later.

Model

The model is the logic of your program. This is where all of the actual thinking and calculating happens. Let’s say you have a blog that users can post comments on. The actual function that analyzes the new comment data and writes it to the database is in the model.

View

The view is what the user or visitor sees. It is the template that holds the actual design markup that displays and formats the page or section the user is currently viewing. Using the blog example, the view would be the actual html files that make up your blog’s overall design.

Controller

The controller is what determines what needs to be run in the model or what view needs to be displayed. The decision of what is displayed or run is generally determined by some kind of user input or action. It is basically the backbone of your app. Back to the blog example, say the user visits a blog post at mysite.com/blog/?post=12. The controller sees this input and tells the model to grab post id 12 and then sends the data to the view which in turn formats the data so the user can see the page.

Planning your project

Creating a web app in PHP can be a pretty daunting task. Here are the steps I take when planning out my app.

Brainstorm

The best thing to do is to open up a new Google Doc and start jotting down ideas. Try to keep it somewhat organized as this will come in handy later on. I also like to put an objective at the top of the doc just so I have a clear idea what my goals are, usually just a few sentences.

Designing your app

I am a programmer and a designer so I usually have a hand in the design process. Just in case you are like me, here is my advice. Make sure you have a clear idea of what the software is going to look like. This might include designing out the entire thing in Photoshop down to the last detail before you even write a line of code.

If you think a simple wireframe of the design will suffice, checkout a program like Balsamiq. This is an awesome program that lets you easily create a wire-frame of your entire site. As far as sketching software goes, this is the best I’ve found. Did I mention it is easy!?

Balsamiq

A layout created using Balsamiq

Plan out your major functions

Before you begin writing the actual code, it’s a good idea to create a visual representation. This can dramatically cut down on development time and bugs. UML (Unified Modeling Language) is a great tool for creating a wire-frame of your functions. You can checkout a list of programs that use UML for creating function diagrams here. Or you might prefer using mind-mapping software, in which case FreeMind is absolutely awesome!

Using the blog example, at the very least I would create a diagram of the posts class, comments class and user management/login.

Create the Framework

First, think about your directory structure and write it out in a way that is organized and makes the most sense to you. Think about where your controller will reside, where your template files will be stored and where your model files will be stored. It may be beneficial at this point to look into a template engine such as Smarty. Template engines aren’t for everyone but I prefer using them over writing PHP directly into my templates.

Once you have your framework all planned out, implement it one step at a time. Start with some empty views and model skeletons and some test cases in your controller.

Mapping out the database

Although I could write an entire article on database architecture and planning (which I might actually do!), i’ll just try and give you some key points to keep in mind. First off, outline your database tables and relationships with a program like FreeMind or even plain old notepad. Research MySQL Data Types and decide which type is appropriate for each field before you actually create your database. Also, keep your database relational. It’s important not to try to stuff everything in as few tables as possible and it helps to keep your database organized. If you feel like you are missing some things, just do the best you can and move on. Some things will inevitably be thought of and added later on.

Prioritize and take it one step at a time

Once you have the basic framework and skeleton of your app and the database created, decide which section of the app to tackle first. I was once told that a good method is to visualize that you are the user and begin coding in the order of how the user might use your app. Back to the blog example, tackle posts first. Create the posts class and grab the post from the database based on the postid in the URL and output it to the page. From posts move on to the comments system and then finally to user management.

Your app will likely be much more complicated than the basic blog program I have mentioned, but hopefully this can get get you started in the right direction. Feel free to leave any questions or improvements in the comments section. Thanks for reading!

Get Adobe Flash playerPlugin by wpburn.com wordpress themes

The best way to contact me is through one of the networks on the left or by emailing me at
ericbieller [at] gmail [dot] com.