Think Like a robot

27Nov/112

FuelPHP- Blog tutorial -part 1

For those of you whom have been living  under a rock some where and haven't heard of or read about FuelPHP I feel sorry for you! FuelPHP is still a relatively new frame work on the scene but I believe it is going to make a huge splash in a already crowded  pond. One of the key features I feel is the  Ruby on Rails type scaffolding.  FuelPHP uses a cli (command line interface) to build the controller, model , and views. The Controller will come with basic CRUD functions , this is great for rapid prototyping  & development.   Enough about the pitch and sell of FuelPHP lets  get down to the brass tacks and build something.

Couple of things  I feel we should address before we start. The directory structure  is a bit different  than what I was use too but make a  lot of sense.  The fuel directory will hold all of your system core and logic while the public directory will be your public facing (and accessible) files.I suggest creating a virtual host inside of apache to get things started.

1
2
3
4
5
6
7
8
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "C:wampwwwblogpublic"
    ServerName blog.com
    ServerAlias www.blog.com
    ErrorLog "C:wampwwwblog-error.log"
    CustomLog "C:wampwwwblog-access.log" common
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "C:wampwwwblogpublic"
    ServerName blog.com
    ServerAlias www.blog.com
    ErrorLog "C:wampwwwblog-error.log"
    CustomLog "C:wampwwwblog-access.log" common
</VirtualHost>

You will also need to edit you host file to point blog.com & www.blog.com to your local machine .  How edit windows host file | How to edit mac host file | if your on *nix you know what to do :)

192.168.**.** test.com
replace *  with your ip address

restart apache and we are good to go. you can now visit http://blog.com and you will see the FuelPHP  "splash page".  This will  give you some basic information about the framework , the version number and some links.

1
2
3
4
5
The controller that is generating this page is located here:
APPPATH/classes/controller/welcome.php
 
This view can be located here:
APPPATH/views/welcome/index.php
The controller that is generating this page is located here:
APPPATH/classes/controller/welcome.php

This view can be located here:
APPPATH/views/welcome/index.php

Lets break down our blog and work our key components for this blog.  We should focus on the core of the blog and as we move forward with the tutorials we will refine and tune the our blog system. For our starting line up  lets use articles and comments.

lets break our articles down and   choose what fields we will be using.

-id
-title
-entry
-author
-created
-updated

** FuelPHP helps us out tons here, FuelPHP's scaffolding  will automatically add the id, createdon and updated fields for us

1
2
3
php oil g scaffold article title:varchar[50] entry:text author:varchar[50]
 
php oil refine migrate
php oil g scaffold article title:varchar[50] entry:text author:varchar[50]

php oil refine migrate

If you ran  the second command you should get an error stating that your data source  is incorrect  open  /fuel/app/config/development/ and edit the db.php file to connect to your database. Now if you  rerun the second command you will be able to  create the new table in your database. If you visit http://blog.com/articles you will see  everything that you need to get started with your articles  created for you. Although very basic it is in place and fully functioning.  Feel free to go ahead ahead and create a few entries , edit and delete them.

Now lets decide on  our comments

-id
- name
-email
-comment

1
2
3
php oil g scaffold comments name:varchar[50] email:string comment:text
 
php oil refine migrate
php oil g scaffold comments name:varchar[50] email:string comment:text

php oil refine migrate

Now you can visit  http://blog.com/commentsand you will have all your lovely crud functions. But wait we  forgot to tie  comments to a post. Luckily FuelPHP's cli  gives us a easy way to add new fields to a table

1
2
3
php oil generate migration add_postid_to_comments postid:int
 
php oil refine migrate
php oil generate migration add_postid_to_comments postid:int

php oil refine migrate

In this tutorial we went over  the basics of setting up your development environment for FuelPHP,  some of  FuelPHP's command line tools, Scaffolding, and how to add a field to a database. In part two we will finish linking the post to the comments  an introduction into routing  and start on auth and  acl.

Posted by Tim

Comments (2) Trackbacks (0)
  1. I’m sorry but i do not understand how to run this:

    php oil g scaffold article title:varchar[50] entry:text author:varchar[50]

    • David, you must add php your Environment variables so that you can run php from the command line. follow these instructions if you are using windows 7

      once you have php enabled in the command line open command promp and type

      php -v

      This will tell you what version of php that you are running and let you know that php is available from the command line.

      Navigate to your to the dir that has your oil file inside of it ( do this in command line) then you will be able to run the php oil commands.


Leave a comment

(required)

No trackbacks yet.