Another PHP framework
With the new features of PHP 5.3 we are able to get a more stylish look on our PHP code. During the last couple of weeks I have been writing on a very simple PHP framework. I have used the common MVC pattern. Currently there are not a lot of features included. A very simple database and SQL abstraction layer removes any kind of SQL handling and with the updated mySQL driver I’m using prepared SQL statements which helps to develop a secure application. What’s really important for me is to get a fast, clean and good looking code. Most of the times I strive for a Ruby on Rails style of the code.
Today I will not go into details on how everything is implemented or exactly how things work. I will focus on presenting a few of the current working features of the framework.
So, we are still in development mode and we have a database with three tables: blogs, blogs_comments and users. To access and work with these tables we create three models Blog, BlogComment and User and three controllers Blogs, BlogComments and Users. In this example we concentrate on the Blogs controller and model.
The blog model:
class Blog extends Model {
protected $has_one = array('User');
protected $has_many = array('BlogComments');
public $validate = array(
'title' => array('required' => true, 'min_length' => 5, 'format' => 'string'),
'text' => array('required' => true, 'format' => 'string')
);
}
As you can see we can declare a has_one and has_many array to say which types of other models this model works with. When we later on do a find method call we will be looking at these arrays. The $validate array holds information about the fields we want to validate. At the moment attributes such as required, format, min_length, max_length and unique is implemented. These attributes are checked when a model is saved or updated. Basically we have an automatic form validation script built into the framework, validation can be turned off.
Let’s take a look at the Blogs controller:
class Blogs extends Controller {
function index() {
$this->blogs = Blogs::find('all');
}
function show($id) {
$this->blog = Blogs::find($id);
if($this->blog)
$this->render();
else {
response('error', 'Blog id not found', 'Blog');
$this->redirect('/blogs/index);
}
}
function make() {
$this->blog = Blogs::create();
if(is_post() && $this->blog->save($_POST)) {
response('success', 'Blog has been created', 'blogs');
$this->redirect('/blogs/show/' . $this->blog->id);
}
$this->render();
}
}
The Blogs controller just contains three methods right now, index, show and make. The best thing to me is the find and create static methods we are calling with Blogs::find() and Blogs::create(). If we wanted to fetch all blog comments we would simply write BlogComments::find('all', array('where' => 'WHERE blog_id = ' . $this->blog->id)); for example.
The $this->blog->save() method validates the argument sent to it. In our case we are sending the $_POST array. It will validate against the validation rules we applied in the Blog model. If we have specified any one or many relationships and have included these in our form these will also be validated. That is, if we have a one-to-many relation, like our User in the Blog model we will be able to create User via the Blog model and be sure it validates.
So, as I said before, I wanted to get a clean and simple PHP code. With this little framework as it is today we can easily add new models and validation rules and with a couple of lines of PHP code we can validate and save, updated and delete models. Getting data from the database is as easy as Users::find(1); or Blogs::find('all', array('order' => 'ORDER BY created_at DESC'));. In other words we can handle CRUD very easily.
Before posting a lot more details and downloadable code I wanted to hear if anyone would be interested in building this with me? Okay dudes, peace!
About this entry
You’re currently reading “Another PHP framework,” an entry on Is the coffee still warm?
- Published:
- August 14, 2009 / 9:49 pm
- Category:
- June
- Tags:
- find, form validation, framework, php
2 Comments
Jump to comment form | comment rss [?] | trackback uri [?]