Programming your own PHP framework Part 2 – MVC

This is part 2 of the programming your own php framework – view part 1. The below article was authored by Terry Smith. Terry is an aspiring entrepreneur and PHP code ninja working for b5media. He spends his “weekends” and “free time” working on all sorts of crazy ideas and one day wants to rule the world (or a small part of it)!

To get things started, you should be familiar with the Model-View-Controller paradigm.  The basics components are models (classes/objects/etc.) that represent items in your application (users, database records, etc.), controllers which do the processing for a page or module and views, which contain the HTML/CSS for your output.  You can read more about it here.

Step 1: Directory Structure

As noted in the last part, I have chosen to structure my URLs in the format[controller]/[module]?vars

In this case, the controller indeed represents the controller in the MVC paradigm.  I will take a moment to explain the directory structure I’ve used; again, note that you can use almost any structure for your own applications.

/config – Basic configuration files (database settings, etc.)
/controllers – Controllers are all in this directory.
/lib – Default library files included with every new site I deploy (database class, URL rewriting, templating and other base classes)
/models – Custom models for each application (users, sessions, etc.)
/views – View files (PHP files), that contain the HTML
/web – The actual web directory we point our web server to.
There are two things of note here.  First, our images, CSS, etc. go into our /web directory, since the web server can’t read anything above the /web folder.
Second, and most important, all of these files should be outside/above the directory you actually point your web server to serve files from.

Programming your own PHP framework, Part 1

building your own framework

The below article was authored by Terry Smith. Terry is an aspiring entrepreneur and PHP code ninja working for b5media. He spends his “weekends” and “free time” working on all sorts of crazy ideas and one day wants to rule the world (or a small part of it)! This is first part of the tutorial post. Second part can be found here.

I’ve always been torn about posting code on this site, since I’ve historically (if I can use that word) wanted to keep it about business ideas.  However, since I’ve started working for b5media, I’ve had a lot less time to work on my own projects; therefore I think I’ll start talking about some code.

These posts are geared towards people who know programming (specifically to people who code in PHP, but if you can read the code, you can port it to most other languages).

As with most engineers, I like to re-create tools I use on a regular basis.  Some I’ve tried and failed at for various reasons (3D game engine), but one of the ones I’ve always wanted to try is a PHP framework of my own.  So last weekend, I sat down and wrote a basic framework that I would enjoy using.  This series of articles will talik about how I did that.  So, without further ado:

There are several components of a web framework: URL rewriting, database abstraction, an MVC framework, a templating system and plug-ins.  All of these are optional and you can pick and choose which you want to include in your framework and how you want to include them.

In this first article, I will cover URL rewriting.

PHP Basics for Designers

The Atlanta Web Design Group recently held a discussion about PHP basics for web designers. Here are the slides from the presentation.

View SlideShare presentation or Upload your own.

Running PHP Scripts with Cron

Lots of programmers like PHP for its ability to code and develop web applications fast. Well, this programming language was built for web. We have recently did post on caching with php to make your web sites faster. Today we want to cover another topic many developers are puzzled about, “How to run PHP Scripts with crontab?”

Cron is normally available on all Unix and Linux distributions; if you cannot access it, contact your root or server administrator. It is a daemon which allows you to schedule a program or script for a specific time of execution. If you want to learn more about cron, click here or type “man crontab” at your command prompt.

I have found myself in the need to run PHP scripts at specific times. For example, to update the content of a website, to remove expired articles, to send out e-mails on a given date and a lot more. While some may think that this is were PHP is doomed, I will show you how it’s done.

A Manual crontab?

The first solution that came to my mind was to run the script directly from my browser (e.g. entering into the web browser).

Since I need to run my script on a regular basis, I squashed that idea. My goodness, all the extra hassle is ridiculous.

Empty() and Isset() in PHP

This is just a quick tutorial regarding the empty() and isset() functions for people that are fairly new to the world of PHP programming. I learned this the hard way a long time ago while I was working on my first few form processors in PHP.