Decaflon

Welcome to Decaflon! Where the geeks hang out: Signup or Login Here
Decaflon is proudly hosted by (mt) Media Temple.  We recommend them for your web hosting needs.
Clips: Popular Clips Upcoming Clips Notes: All Notes

When I started web development it was with PHP. However, a little thing called Ruby on Rails came along and has been my focus for the last 3 years.

Well now I've got a few projects that I think Rails would just be overkill for. The projects are fairly simple and do not merit the resources required to run a full Rails app. I have been reading up on PHP again but most of the books I've found have left me still wanting something a little more advanced.

I've been spoiled with MVC and do not want to go back. I know there are MVC frameworks for PHP now, but since I am still going for lightweight, I don't really want to learn another framework. I would much rather begin piecing my own together as I go. There are tons of things in the other frameworks that I will probably never use, so this seems like a waste.

I've picked up nearly every book I've come across lately and haven't been impressed. There doesn't seem to be anything that covers this sort of organization.

I am also looking for some good resources that cover some of the new OOP stuff in PHP, which I figured would go hand in hand with MVC patterns.

Any recommendations on books, online resources, or maybe a good open source project to learn from?

If you want your application to be so lightweight, why make it sit on top of a framework?

You're right when you say you should put your own together. Any other framework (talking any language here,) will be overly general, and have a lot more than you will need, and probably missing things you want.

I'm not sure what you're looking for in a book. You understand the concept, implementation can be done more ways than you can easily imagine. It's going to be up to you to decide what makes best sense for your needs and abilities.

My typical php web app will have the MVC style, I break into 3 directories. Models, Actions, Templates. Models, obviously the objects I'm using. I keep once file per object, a-la java. Actions/Controls I group together contextually, sometimes by user type, sometimes by the area of the app... really depends on the program. Then templates, I sometimes have several per object type. I've got one that's a view of the data object, one that's an editable view, one that's a list view, etc.

Really using a framework is what I want to avoid. I still miss the MVC patterns as they made my code so much easier to debug and helped organize and keep things DRY.

I simply want to learn how I can take these practices that I've learned from using Rails and use them in my PHP apps. When I'm talking about using a "framework" I'm basically just talking about a small lightweight collection of my own objects to speed up my development on various projects.

@Ozone42

Thanks for your response. I guess what I'm looking for is just where to start.

As Rails took care of all of the MVC stuff for me, I wouldn't really know where to start when developing my own app in PHP.

A few questions I might have would be how to decide what files to include on each request, associating controllers with the views... things like that, as well as just a good resource on OOP for PHP.

Aside from that I've downloaded Zend and Cake PHP and am just trying to extract some ideas by looking through the code.

"lightweight" is a single page script. When you start talking about MVC, Rails become a very suitable choice. Besides, RoR isn't the only Ruby framework around either, a couple have recently appeared - Merb for example.

Cake PHP is simply a translation of RoR into PHP. Or at least as close of an attempt as PHP would allow.

If you want lightweight, but still want an MVC architecture, try out CodeIgniter (the framework that powers Expression Engine). The entire framework is built in order to be as lean and fast as possible.

Fuck PHP. You want lightweight - use Merb or Camping.

MVC is only an organizational method. It's not a silver bullet, but it does make good sense as far as maintaining code.

So as far as "where to start." The project determines this, but let's take a simple example. A blog!

  1. What does the program need to do?
    I need to be able to create blog posts and publish them
  2. What data objects does that require?
    I'm not talking object/classes as in object oriented programming here, I'm talking what you're really doing on a high level. In this case the major component will be a blog post.
  3. How do I model the data object into code?
    This is where you'll want to figure out what a post entails. Simplest post will have a title, and a body, and a unique id. You can have a lot more, but let's keep it simple for this.
  4. Build the data object as a model
    You can use OO here, it's nice organizationally, but you don't have to. You could simply have a collection of functions in a file. The key to doing MVC here is to keep things separated. In this object/class (or file) keep everything in it relevant to a blog post, and nothing else.
  5. What views does this data object suggest?
    For a blog you'll want a read only view for visitors, and an editable view for you.
  6. Build the views
  7. Again, you can do this a lot of different ways. Use a template engine like smarty, use your own method, keep these files strictly what you're presenting to the users of the program, the html and css perhaps.

  8. What actions can I take on the data?
    These will be your controls. You can edit a post, save a post, delete a post, view a post, etc. This step may have you going back to your model file and adding functions to help you accomplish these goals

This is very basic and generalized, but it's also what every MVC framework does, one way or another. The key thing to remember is that MVC is just a way of organizing. It works well, and is easy to manage and maintain, which is why it's popular. The end result is the user is interacting with the control level, the models are being loaded according to whatever action the user is taking, and the templates/views are being displayed according to the action and model being interacted with.

Please Login To Leave A Comment

Decaflon Sponsors Get in touch if you want in.

 

Decaflon is part of the Chawlk Network of sites.

9 Great Places To Visit, Hang Out, & Meet New People

What's new and interesting at other Chawlk Network sites: