Getting Started with the Fuel PHP Framework

Getting Started with the Fuel PHP Framework

Tutorial Details
  • Program: Fuel PHP Framework
  • Version: 1.0.1
  • Difficulty: Easy
  • Estimated Completion Time: 1 hour

This two-part tutorial will get you up and running with the Fuel PHP framework. We’ll start with the basics, and then move onto some more advanced topics in part two! Let’s get started.


Introduction

Fuel is a new PHP Framework built specifically for PHP 5.3, which uses the tried and tested MVC architecture for logical code separation and combines the best ideas of some existing frameworks with improvements and ideas of its own. A final v1.0 has only recently been released, but, already, the framework has a large following after nine months of heavy development. This article will get you up to speed on how to make sites with Fuel – but first, let’s talk a bit about the architecture.

Understanding MVC

The MVC (Model-View-Controller) architecture is used by many existing frameworks such as CodeIgniter, Zend Framework, Symphony and Ruby on Rails. If you’re familiar with any of these frameworks, you have a head start!

For those who are new to this architecture, MVC is an approach to separating your code depending on what role it plays in your application. In the application flow, it starts with a Controller that is loaded by Fuel. A method is then executed which works out what data to retrieve using a Model. Once that is done, the Controller can decide what View(s) to load (if any). Views contain the output your visitors get to see, AJAX responses or error messages.

A more in depth explanation of MVC from Fuel’s perspective can be found in the Fuel MVC Documentation; so we’ll skip to the good stuff.

We’ll start off with some of the very basics to get brand new users going. If some of this seems obvious then please skip down a bit to get to some of the more exciting features.


Step 1: Installation

Installation is as simple as grabbing a copy from GitHub or downloading a ZIP from the site. You can also use a one-liner installation if you are using a *nix system, such as Linux, Mac OS X, etc which requires Git to run. Installing Git is quite easy, and makes things a lot easier as you develop your applications:

$ curl get.fuelphp.com/oil | sh 

This will install a very limited version of “oil”, which is the name of the command line utility you can use when working with Fuel applications. This stripped down version can be used to create applications:

$ oil create blog

This will create a blog folder in your current directory which will contain the base framework. If you come across any problems then take a look at the more detailed installation instructions.

Assuming you ran this command in your local servers web root, we should be able to browse to http://localhost/test/public and see the Welcome page.

File Structure

The root of your application should contain three main items:

  • fuel/ – Where all of your PHP code is going to live.
  • public/ – Anything you want to be directly accessible in the browser, so JS, CSS, images, etc.
  • oil – An executable, which is a more feature-full version of the oil installed earlier that can run command line tasks such as generating code or interactive debugging within your application. It’s optional, so you can delete it if you don’t like the command line.

Within fuel/ we have some important folders:

  • app/ – All application specific PHP code goes in here, including your Models, Views and Controllers.
  • core/ – This is where Fuel itself lives. If you use Git, this will be a sub-module that can be updated easily.
  • packages/ – Fuel separates out certain logic into packages to avoid bloating the core. By default, Fuel will contain three packages: oil, auth and orm. These packages will not be loaded unless you require them, so we’ll filter through them later on.

The important part here is the app/ folder:

  • config/ – Configuration files for various classes and the general “config.php” file lives here.
  • classes/ – This is where all Controllers, Models, helper classes, business logic libraries, etc will go. If need to write a class to use in your code, it will probably go in here. Names are all lowercase.
  • classes/controller/ – This is where Controllers are placed.
  • classes/model/ – Location for your models, although they are really only just another class.
  • views/ – Put your view files in here in folders or just in the root. There are no specific naming conventions for views.

Before going through any more theory, let’s write some code.


Step 2: Hello World

Let’s delete the fuel/app/classes/controller/welcome.php controller and make our own, called hello.php.

In that file, add the following code:

class Controller_Hello extends Controller {

    public function action_index()
    {
        echo "Hello World!";
    }
}

Now if we browse to http://localhost/test/public/index.php/hello, you should see “Hello World!” output to the browser. The action_ prefix tells us this is a routeable method and not some callback or other shared method, and means you can use method names, like “list,” without PHP getting confused.

If we want this hello controller to be our “root” controller instead of the now gone welcome.php, we only need to open fuel/app/config/routes.php and change the _root_ route like so:

return array(
	'_root_'  => 'hello',  // The default route
);

Your First View

Make a file fuel/app/views/hello.php and add:

<h1>Hello!</h1>
<p>Hey <?php echo $name ?>, how's it going?</p>

Next, modify your controller a bit:

class Controller_Hello extends Controller {

    public function action_index()
    {
        echo "Hello World!";
    }

    public function action_buddy($name = 'buddy')
    {
        $this->response->body = View::factory('hello', array(
            'name' => $name,
        );
    }
}

Now, if you load http://localhost/test/public/index.php/hello/buddy or http://localhost/test/public/index.php/hello/buddy/John , you will see the $name variable being passed through from the method to the view. Essentially extract() is being run on the view.


Step 3: Basic Configuration

As you can see, Fuel can do basic Controller/View stuff out of the box, but if we want to do much more, we’ll need to make some basic configuration changes. Let’s start by opening up fuel/app/config/config.php and setting a few things up:

/**
 * index_file - The name of the main bootstrap file.
 *
 * Set this to false or remove if you using mod_rewrite.
 */
'index_file'	=> 'index.php',

If you have mod_rewrite installed, we can change this value to be an empty string, which will let us remove index.php from our URL’s. There is a .htaccess file in public/ which will support this.

Next we need to set up the database configuration, which, for the sake of this tutorial, we’ll assume is MySQL. Create your database with your desktop GUI, phpMyAdmin, or etc command line:

 mysql> create database blog_example; 

Open up fuel/app/config/db.php and set the Fuel::DEVELOPMENT array, like so:

Fuel::DEVELOPMENT => array(
	'type'			=> 'mysql',
	'connection'	=> array(
		'hostname'   => 'localhost',
		'database'   => 'blog_example',
		'username'   => 'yourmyseluser',
		'password'   => 'yourmysqlpassword',
		'persistent' => false,
	),
	'table_prefix' => '',
	'charset'      => 'utf8',
	'caching'      => false,
	'profiling'    => false,
),

Next, lets enable the orm and auth package by un-commenting the following lines:

/**
 * These packages are loaded on Fuel's startup.  You can specify them in
 * the following manner:
 *
 * array('auth'); // This will assume the packages are in <code>PKGPATH</code>
 *
 * // Use this format to specify the path to the package explicitly
 * array(
 *     array('auth'	=> PKGPATH.'auth/')
 * );
 */
'packages'	=> array(
	'orm',
	'auth',
),

This step is only required if you wish to use them – which in this tutorial we will be.

Optional: Using a Virtual Host

The last step of setup is to create a virtual host. You do not need to do this, but it means you can use a real URL and remove /public/ from your URL’s. If you are using Apache, then a simple chunk like this should do the trick:

<VirtualHost 127.0.0.1>
	DocumentRoot /home/phil/Sites/blog/public
	ServerName local.blog

	<Directory /home/phil/Sites/blog>
		Options All
		AllowOverride All
	</Directory>
</VirtualHost>

If this was a live site, we’d be adding the ServerName as “myawesomeblog.com” instead of “local.blog” but this works for our demo. Once you’ve added “127.0.0.1 local.blog” to your /etc/hosts file you should be ready to go. If you want to skip this step then adjust URL’s in this article.


Step 4: Kick-starting Development

With this basic understanding of how Controllers, Views and Configuration works, you could probably hop into the documentation and get started rather quickly, but to really get going, the best way has to be Scaffolding.

Scaffolding is not a new concept and is best known for its place in the framework Ruby on Rails. It is essentially a very simple way to create code based on a few assumptions through the command line. You want to add, edit and delete an entity which you name and provide fields for. It is done through oil and the sub-command “oil generate scaffold

So if we want to build a basic blog, we only need to write “$ oil g scaffold post title:string summary:varchar[250] body:text“. Oil will be very verbose about what it is doing and tell you all the files created:

	Creating model: /home/phil/Sites/blog/fuel/app/classes/model/post.php
	Creating migration: /home/phil/Sites/blog/fuel/app/migrations/001_create_posts.php
	Creating controller: /home/phil/Sites/blog/fuel/app/classes/controller/posts.php
	Creating view: /home/phil/Sites/blogfuel/app/views/posts/index.php
	Creating view: /home/phil/Sites/blog/fuel/app/views/posts/view.php
	Creating view: /home/phil/Sites/blog/fuel/app/views/posts/create.php
	Creating view: /home/phil/Sites/blog/fuel/app/views/posts/edit.php
	Creating view: /home/phil/Sites/blog/fuel/app/views/posts/_form.php
	Creating view: /home/phil/Sites/blog/fuel/app/views/template.php 

Note: Models generated by scaffolding use the ORM package so make sure it is enabled as described above.

You’ll see here a model named “post“, a migration (more on those later) a controller “posts” and a bunch of views. The fields are all generated based on the arguments provided, which are fieldname:fieldtype[optional-length]. For title, we used “:string” which, as long as you are using MySQL, will alias :varchar[255], but any DB type is supported.

With this command run we need to run our migrations. A migration is a series of changes that need to be made to a database. This becomes useful when multiple developers are working on a project, as each developer can add a migration and you can run a single command to make sure your local copy is up to date. No more “missing field’ or “table does not exist” errors after pulling the latest development copy!

To run this migration, simply type:

$ oil refine migrate
Migrated to latest version: 1.

Now you can view what Oil has made for you by going to http://local.blog/posts

If you want to create controllers, models and migrations separately and not all together like this, you can do so easily with oil g controller, oil g migrate, oil g model, etc.

Templating

You may have noticed, in the step above, that Oil created a file:

	Creating view: /home/phil/Sites/blog/fuel/app/views/template.php 

This will be created when you first run a scaffolding command as all views are wrapped with a “template” or “layout” – which is a header and footer wrapping your content. To change from the default design, all you need to do is edit this template, include your own CSS, add a logo and enter whatever metadata you like.

When you create new controllers manually, you can extend ‘Controller_Template‘ instead of the usual ‘Controller‘ to have this template wrapped around any Views loaded in the controller.

If you wish to use a different template for a controller, you simply change the $template property to something different.

class Users extends Controller_Template {
    public $template = 'alternative';
}

This will use the fuel/app/views/alternative.php view file instead of the usual fuel/app/views/template.php.

Working with Forms

One of the most important aspects of any application is form submission. This is how data is captured from a user; it could be a login, a comment, a shopping cart checkout, etc. This is all done with HTML normally, but Fuel gives you some helpful methods to make this process a lot easier. They are optional, so if you are an HTML fanatic, then carry on, but to speed things up, read on:

<?php echo Form::open(); ?>
	<p>
		<?php echo Form::label('Title', 'title'); ?>
<?php echo Form::input('title', Input::post('title', isset($post) ? $post->title : '')); ?>
	</p>
	<p>
		<?php echo Form::label('Summary', 'summary'); ?>
<?php echo Form::input('summary', Input::post('summary', isset($post) ? $post->summary : '')); ?>
	</p>
	<p>
		<?php echo Form::label('Body', 'body'); ?>
<?php echo Form::textarea('body', Input::post('body', isset($post) ? $post->body : ''), array('cols' => 60, 'rows' => 8)); ?>
	</p>

	<div class="actions">
		<?php echo Form::submit(); ?>
	</div>

<?php echo Form::close(); ?>

This is a very simple form that will work with both create and edit. For each input, if it can find a match in POST it will use it; otherwise, it will look for the $post variable and input the value (good for editing).

The real benefit of these helpers does not come from cleaner syntax as you might think, but in that it allows the framework to programmatically wrap your form. This means Fuel can automatically embed attributes to all forms to make sure data is sending in the right character set and enable CRSF (Cross-Site Request Forgery) automatically.

Validating your Forms

Validation is a simple way to ensure that certain information has been supplied in a form submission in the correct manner. It can match certain patterns, data types or conditions and will help improve the integrity or the data.

By default, validation is not used by Scaffolding, because it’s complicated to make assumptions about what the developer expects to be held in the data. For this reason the validation is optional but is quite easy to add into your generated controllers or work with from scratch.

Lets take a look at how a “Create Post” method may look for our blog:

	public function action_create($id = null)
{
	if (Input::method() == 'POST')
	{
		$val = Validation::factory();

		// Add a field for title, give it the label "Title" and make it required
		$val->add('title', 'Title')
			->add_rule('required');

		// Now add another field for summary, and require it to contain at least 10 and at most 250 characters
		$val->add('summary', 'Summary')
			->add_rule('required')
			->add_rule('min_length', 10)
			->add_rule('max_length', 250);

		$val->add('body', 'Article body')
			->add_rule('required');

		if ($val->run())
		{
			// Make a post based on the input (array)
			$post = Model_Post::factory($val->validated());

			// Try and save it
			if ($post->save())
			{
				Session::set_flash('notice', 'Added post #' . $post->id . '.');
			}
			else
			{
				Session::set_flash('notice', 'Could not save post.');
			}
			
			Response::redirect('posts');
		}
		else
		{
			$this->template->set('error', $val->errors());
		}
	}

	$this->template->title = "Posts";
	$this->template->content = View::factory('posts/create');

}

We can see here that we are telling the Validation class – which is autoloaded like all classes) which fields we care about. We are then assigning rules and giving them labels for humans to read. If $val->run() is true, we make a new Model_Post instance using the factory, and send $val->validated() which contains an array of all the submitted data. With that, we can simply save the instance, which uses ORM to do everything for you.

If any of the validation rules return false, then $val->run() will fail and we are given an array of errors in $val->errors() which we can send back to the user. The default template.php looks for a “notice” piece of flashdata (part of the session class) or just normal view data and can output a string or an array, so this works perfectly.

Using your knowledge of validation and form building, you can start to make any Controller based applications you like.

Working with Tasks

Tasks are similar to controllers, but cannot be accessed via a URL or routed to in any way. Instead, they are run via the “oil refine” sub-command in the terminal. This is great for creating interactive shell scripts that have access to your codebase and makes creating secure cron jobs a breeze.

Some frameworks suggest you use wget, curl or something similar to run a controller to make a cron job, but this can lead to potential security or consistency concerns with cron jobs being run out of time to cause malicious or unexpected results. This way, it is protected from the outside world completely.

For an example of a task, take a look at the provided “robots” task in fuel/app/tasks/robots.php:

class Robots {

	public static function run($speech = null)
	{
		if ( ! isset($speech))
		{
			$speech = 'KILL ALL HUMANS!';
		}

		$eye = \Cli::color("*", 'red');

		return \Cli::color("
					\"{$speech}\"
			          _____     /
			         /_____\\", 'blue')."\n"
.\Cli::color("			    ____[\\", 'blue').$eye.\Cli::color('---', 'blue').$eye.\Cli::color('/]____', 'blue')."\n"
.\Cli::color("			   /\\ #\\ \\_____/ /# /\\
			  /  \\# \\_.---._/ #/  \\
			 /   /|\\  |   |  /|\\   \\
			/___/ | | |   | | | \\___\\
			|  |  | | |---| | |  |  |
			|__|  \\_| |_#_| |_/  |__|
			//\\\\  <\\ _//^\\\\_ />  //\\\\
			\\||/  |\\//// \\\\\\\\/|  \\||/
			      |   |   |   |
			      |---|   |---|
			      |---|   |---|
			      |   |   |   |
			      |___|   |___|
			      /   \\   /   \\
			     |_____| |_____|
			     |HHHHH| |HHHHH|", 'blue');
	}
}

To run this jestful task just type “oil r robots” or “oil r robots 'Kill all Mice' ” to make the robot say something else.


Summary

If you followed each step, you’ll have installed Fuel, learned where the important files go, configured a basic install to run on Apache with mod_rewrite (other servers work fine too), and created simple controllers and views using forms and validation. With scaffolding available to generate code to pick apart, there should be plenty of code to learn from!

At this point, you should have enough knowledge to play around and create some really simple apps – that is, until Part two of this series, where we will go through the process of creating and extending Base Controllers to create your frontend/backend separation. We’ll also review advanced ORM, authentication drivers, and file uploads. Stay tuned!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://montanaflynn.me Montana

    Great tutorial, thanks for taking the time to build and document this awesome framework! Ignore the naysayers, you guys are doing an amazing job!

  • John

    What a waste of PHP.

    • Mark

      Haha, wow! Well, this comment has no case.

    • Ted

      Wow, what a waste of a comment.

  • http://www.microwd.com Liam Gooding

    I’m amazed at the negativity from some developers on here, so wanted to add a minor contribution.

    I’ve worked with Phil on a number of projects in the past, so when I consulted him about which framework to use for a new project I was cooking up, I weighed up his arguments for/against and decided to take a risk with the (then) very premature FuelPHP.

    Since then, the project (Microwd) has required lots of custom packages and highlighted lots of fixes/improvements – all of which have been rolled back into FuelPHP core for the community to enjoy for free.

    Microwd uses MongoDB (now in core) as well as a bunch of other cool things (OmniAuth too as Phil already mentioned). Now acting as our informal lead developer, Phil knows that we’re committed to FuelPHP, both in spirit and financially.

    I’m astounded that code that I’ve funded, and have agreed to been made open source, is being criticized this way… especially when this was a tutorial on HOW to use it.

    Damn guys, stick to CI or Yii or Symphony if you don’t enjoy all the performance and productivity enhancements FuelPHP provides, or your manager is unlikely to listen to your recommendations for change (if thats the case, start looking for a new dev company to work for)

    I’m not saying we’re the only funded startup using FuelPHP, or that we’re the only company paying towards this frameworks development – I’m sure most of the contributors and FuelPHP lead team have similar stories of packages developed for one of their personal projects, that are then rolled into the core.

    My “business case arguments” for using FuelPHP over CI/Lithium/Kohana/Yii were thus:

    - we had an opportunity to contribute to the growth of something that could be big
    - we had the founders of the framework willing to work directly on our app
    - we were assured the performance improvements would result in lower memory usage, and server costs
    - as the framework was created by leading CI devs, I knew it HAD to be better
    - we have no legacy systems or dev teams to worry about backwards compatibility or transition costs
    - I trusted Phil’s judgement

    I’ve since got other PHP developers using FuelPHP on new projects, and admittedly the documentation is behind other frameworks. But that was the whole point of this post, and I hope Nettuts invest a lot more in tutorials on FuelPHP.

    • http://8gramgorilla.com Gordon McLachlan

      I totally agree that people shouldn’t be so negative however it does seem like a lot of these new frameworks are just reinventing the wheel for the sake of it.

      • http://philsturgeon.co.uk Phil Sturgeon
        Author

        Do you really think we’d be writing this code for the sake of it? Writing a framework will not make you famous, get you laid or make you rich. It is done because an upper-barrier is hit with existing systems.

        I remain fully committed to CodeIgniter (I’m writing my Keynote for the CI Conference in New York this weekend as we speak) and will always love it, but worrying about compatibility of apps is always an issue.

        Development of highly adopted frameworks always involves two main options:

        1. Make it the best you can
        2. Make it as backward-compatible as possible

        When you start fresh you get to make the best choice and make it awesome, without worrying about 100,000 applications needing a total re-code. Fuel makes me faster and that is worth the investment of time and money.

  • unix44

    kohana fork )))

    • http://philsturgeon.co.uk Phil Sturgeon
      Author

      The reasons for this common misconception have been answered in full on the first page, but to add one extra comment I missed out first time: The irony of this comment is hilarious. How did Kohana start its life again?

  • http://twitter.com/drale2k Drazen Mokic

    This looks really impressive. I`ve used CI over 2 years but then i moved to WordPress since a CMS was all i needed in the past.

    One reason why i love CI is the awesome documentation and the amount of screencasts you can find to learn about it. I wish this for Fuel too since i think it has great potential and makes use of the terminal, which i missed a little bit in CI.

    I`ve read some of the comments, good and stupid ones, and just want to say something that everyone knows and it`s said again and again but some still don`t get it:

    It is completely o.k. to not like something or to prefer something else over it but you should always have and tell the reason why you think so because nobody can read your mind and you might look like an idiot saying something like “Fuel Framework is waste of time stick to CI or Cake, forget Fuel the codebase is horrible.”

    Thanks for the great work!

  • http://macodev.com Marco Cervellin

    One thing is certain: FuelPHP has attracted great attention and the development will be followed closely, even by those who criticized it so much. This is already a success.

  • http://webking.pl Damain

    It’s a clone of Kohana Framework v3.x + v.2.x…

    • http://fuelphp.com Jelmer Schreuder

      Little known fact: Damain is actually a clone of unix44

      • http://philsturgeon.co.uk Phil Sturgeon
        Author

        This will be the third answer to the same comment, but to keep things interesting I’ll point out the incorrectness of the statement with a third new and different argument.

        Stuff that Kohana has that is the same as Fuel:

        – Database Class
        – Template / Views

        Stuff that Fuel has that Kohana doesn’t:

        – Command Line Tasks
        – Code Generation
        – Migrations
        – Full REST controller (ours does a LOT more)
        – Format control
        – Interactive Debugging
        – Namespace support
        – Package Management

        So, even if Fuel was a clone of Kohana (which it really isn’t) we’ve added in plenty of new features.

        To reiterate my last point: Kohana was a clone of CodeIgniter when it started out too. So let’s drop the “Kohana clone” junk shall we?

  • Bill

    ASP clone

  • http://globalshop.me Tim

    Great framework guys. I’ve sat down with the framework the past two days and have been simply blown away. I was a little fuzzy on namespaces but I can already see an incredible benefit.The framework caters beautifully to projects with a large code base and independent developers.

    To anyone screaming “clone” get a day job. There’s no doubt that Fuel takes a few pages from many of the great frameworks already out there, however, the Fuel team doesn’t deny it! Hell, even their own website states its “based on the best ideas of other frameworks” however with a fresh start and a lot more features under the hood if you even spent a few hours taking a look.

    The only downside is the documentation at this point. There is a ton of great features that are just under-exposed or just buried all together. People fear what they don’t understand and an improved documentation I think would silence a lot of this useless “what a waste of PHP” B.S.

  • http://www.aon-media.com Jon Schuster

    I was wondering if you’d ever write up an article about Fuel, Phil. Great read, and I can’t wait to give this new framework a shot! I checked into it a few months back (I’m an avid follower of the PyroCMS project), but haven’t really gotten around to giving this a test run yet. Thanks for all the great work Phil!

  • http://tedlee.net Ted

    Phil – Keep up the great work, it is definitely appreciated. I’d like to see Nettuts adopt Facebook commenting, so maybe some of these people making snide, stupid comments would think twice before hitting the ‘add comment’ button.

    I see Fuel as natural successor to CodeIgniter, and every time I check in on the project, there’s something cool being added.

    • Aaron

      Ted: I think you said it correctly: “I see Fuel as natural successor to CodeIgniter, and every time I check in on the project…”

      I just can’t wait until Tusplus has a series of these for Fuel like what they did for CodeIgniter. Those tuts helped a lot of people get into CI and would do the same for Fuel.

  • ihti

    Nice to see new framework in PHP community!

  • Chris

    I’m starting a new web site/app, and saw this and how it was made by the CI guys. I don’t know whether to choose this or CI, or the differences between the two.

    • http://fuelphp.com Jelmer Schreuder

      You could try Google… or page 1 of the comments.

  • http://www.zalas.eu Kuba

    What’s symphony? Where can I find it? Do you even know what you’re talking about if you can’t spell the framework name right?

    • http://fuelphp.com Jelmer Schreuder

      Yes, thank you. I was wondering when someone would finally bring that up. In a framework tutorial the difference between “ph” and an “f” in the name of another framework is truely the most important thing, everything else follows from there…

  • WebDevGuy

    Hi, I’m having problems with 404′s after doing the scaffolding. It seems to be related to working with the Controller_Template. I can add new actions to the welcome controller which extends the regular Controller class and I can access these in my URL just fine by going to:

    http://localhost/fuelapp/ or http://localhost/fuelapp/public/welcome

    But after scaffolding the posts when I go to:

    http://localhost/public/posts/ or http://localhost/public/posts/index

    Or any of the other actions of the posts controller I get the 404 page. I’m using PHP 5.3, mod_rewrite, I’ve tried adding the ? after the .index in my .htaccess file. None of this works.

    Does anyone have an idea what might be wrong? Why can I access URL’s of the welcome controller actions but not of any scaffolded ones? I also tried making them by hand and got the same 404.

    Thanks.

  • Thiago A.

    Fuel looks like a nice project, but looking at the code samples it apparently suffers from the same problems of Kohana and CI. However, that’s only my humble opinion.

    I simply can’t cope with the form and the validation stuff. Take the following line of the code:

    summary : ”)); ?>

    Forms are one of the main parts of web applications, so frameworks should make them easier to work out. In that line of code there is a lot to worry about. I have to worry about passing the model to the view ($post), and passing it on to each helper’s method call to fill back the form values (and asking wether it is set or not in each call). I even have to worry about using an input class. When you code big forms, that approach causes a lot of trouble, and lots of copy and paste. I have to worry about filling the values in each field, and that increases the likelihood of errors. It is possible to code a lightweight and simple form class. In my opinion, it should work like the following (these are only raw examples):

    // The form object already knows this is a post request… so I don’t have to worry about the POST array or an Input class, it will fill out the values for me if there is a POST request going on
    $form = Form::factory(Form::POST, ‘/my/submit/url’);
    // Validation rules should be inside the model, which make them more reusable in other controllers
    $model = My_Model::factory();
    $model->populate($form);
    $model->save();

    In the view the approach would be simpler:

    open();
    // In this line I only have to worry about styling. The form object is smart enough to do all the dirty work
    echo $form->input(‘my_field’, array(‘class’ => ‘some_class’));
    // The model feeds the form object with any possible error during the validation
    echo $form->errors();
    echo $form->submit();
    echo $form->close();
    ?>

    And so on.

    • Thiago A.

      Seems like WordPress filtered the PHP tags… but it should still be clear.

    • http://fuelphp.com Jelmer Schreuder

      All of that is possible, Phil just chose a simpler solution for the tutorial.

    • Thiago Araújo Silva

      Alright… sorry for taking the word without having a deeper knowledge about the framework… I judged by the examples here. I will sure take a good look!

      I’m glad it supports HMVC, that’s the reason I am using Kohana today (as a HMVC codebase for my projects). But I’m not using their validation library (which has poor extensibility) or form helper :)

  • http://craigmoss.co.za Craig Moss

    Never argue with Dumb people, it just brings you down to their level… ;)

    Thanks for the great tut, it has opened my eyes to what else is available besides my beloved CI…

    I reckon people are happy in their own little worlds and are so against change because it takes them out their comfort zone, probably why so negative…..

    Change is as good as a holiday….

    • http://philsturgeon.co.uk Phil Sturgeon
      Author

      I DONT LIKE CHANGE!

      Ha I agree. Another element is that people are stuck in an “all or nothing” mentality. I still actively use CI and will most likely never stop, but I love that I have this option too.

      If you have this all or nothing approach then when you see somebody suggest option B then you think “but I don’t want to stop using A. A has been great, why should I use B? You know what, f**k you and your B!”

      The whole thing is silly.

  • http://andy-russell.com Andy Russell

    Looks good guys.
    Really want to get some time to take a proper look into it.

    Keep up the good work! :)

  • steve

    Looks like cheap remake of Kohana framework to me.

    • http://philsturgeon.co.uk/ Phil Sturgeon
      Author

      If you could point out some examples in detail that would be pretty interesting, but make sure those examples are not the same arguments that I have trounced in previous comment threads.

  • merdj

    For all the negative people, you are not forced to use fuelphp, take it or leave it. got it?

  • Daniel B.

    Firstly, great article and can’t wait to see more installments. I agree with an earlier comment that there are a ton of great features that aren’t getting enough light shed on them.

    Second, thank you to the Fuel team for your time and efforts. I know a simple thank you isn’t much of an offering, but it is also probably something you don’t get enough of.

    Thirdly, to the “open mouth insert foot” crowd…. Who really cares if it shares features with some of the other frameworks, isn’t that what OSS is all about? I remember coming across an old Kohana with stray CI comments and references left in. Fuel is exactly what it says, a combination of great features, ideas and concepts from other frameworks with added awesomeness of some really cool features that pick up where other frameworks are slouching.

    If you don’t like it, don’t use it… If you are going to be negative about it, at least have a reason other than “i just don’t like it” Those who are just spouting off “clone” obviously have no merit to their claims, because it shows they haven’t taken the time to look under the hood. Bad programmers… Shame on you.

    I use Kohana, CI, Sym, and Cake, my own custom framework and now Fuel… It just depends on what the job requires. They all are strong and good at what they do.

    Kohana was my choice above the rest, but I find their documentation extremely lacking, the community activity is poor, the Kohana Team (not all of you) are extremely rude at times.

    That was a long post and turned into more of a rant I guess, but it bothers me when people rudely down those like the Fuel team who are out there putting in their time for the community and all you can do is complain. What have you contributed lately?

    Again, thank you Phil and the rest of the team, and to the community out there who is actively engaged. To the rest, shame on you…

  • http://xmory.com xmory

    Thanks! I like it very much.

  • AEJNSN

    I’ve not used Kohana or Sym, I’ve always been with Cake and CI. I’ve been watching Fuel for quite some time, and I am now using it in development of a SaaS application. It’s nice, and mainly for one reason, the REST controller. Even though Phil did an awesome job with the CI REST server library, it’s better in Fuel, because it’s in the box already. Plus there’s HMVC. This makes life easier…

    Nice work Phil and Team.

  • Kirn

    Thanks for taking your time in putting this together. I’m astounded that people can be so callous with their comments. I’m a fairly new developer looking to sink my teeth in a few MVC web frameworks and this seems like a good place to start. I can’t wait for the second part, very good work! Thank you!

  • Nico

    Loved the introduction to Fuel. I had heard of Fuel before, but didn’t really go far with it. This was about a year ago, but Fuel has come long way since. So it’s definitely something I will be looking more into after this short introduction. For most projects I do, I don’t really need the massive stack Rails has(nor the automagic it has), but yet I really like having some framework to work on top of. It just keeps things cleaner. So, Fuel might just fit the bill there.

    I love Rails, but Fuel seems to be something I could love too :)

    Keep up the good effort! The atmosphere here in the comment section has been rather negative, and for the most part for all the wrong reasons.

  • cpreid

    This framework is awesome – very similar to a framework built in-house that I use at work.

  • http://www.zufrizalyordan.com zufrizalyordan

    i’ve been following FuelPHP for a few months now, stumbled FuelPHP when i researched about REST for CI.
    i think its a great framework, because it takes the best from other framework and isn’t a fork from anything.
    if there is any chance that i get to use FuelPHP for development, i will use it.
    i developed websites with frameworks such as Rails, Cake, Symfony, even built my own MVC framework.
    but in the end, its a personal preference for each developer to use which tools/framework to get the job done.
    for the time being,mine is still CI+HMVC, because it’s easier for me to develop with.

    thank you phil & team for FuelPHP, the tutorial, and for the many codes you guys write.

  • http://test.com Mark Cruz

    Nice framework but similar with CI. Still with CI, the best in the world.

  • http://c0ding.ro hpaul

    This framework is what I need but it have lot of folders, you can keep it more sample.

    I came across a problem, when I try to return to the view the loaded css file it show html encoded. I tried some method but didn’t work, can display them in view but I need in controller.

    Sorry for my bad english.

  • http://partialbits.com Krishna

    I have been using Fuel since some days . The OIL makes me feel like I am using ror ( mainly scaffolding , migrations ). But rails magic is like a mystery for me where as in fuel I am knowing how it does the stuff in background ..
    Good job !

  • Phicheth K.

    I have been using ZF, SF2, CI2, Yii and even build my own php framework. and start to learn FuelPHP now.

    Q: Why?

    A:

    SF2 there seem to be the best of PHP framework with really great documents.. But not really. I have been building a web project with SF2 since RC 3 until 2.0.1 and have been hacked some code to make easy way to run SF2 under share hosting. doSymfony 2.0.0 ready edition you can download and test from google code. I fund lot of problem while playing with SF2 have to say that not a community enough for opensource. they may need to premium user to get support. I am stop to play with Symfony2.0 anymore because of my last project was stop working with no reason, error and bugs.. try to fixed many ways , and even clone another project .. looks a round on all code of both. there is clone project for me .. but one working and another one is broken.. So better to stop with SF2 for me.

    CI2, is not what i am looking for.. you should to try and i don’t need to comment with CI2 because not my interesting at all.

    Yii, there is very poor document and not friendly for new developer. very difficult to understand just detail of class without example. quickstart project are using memory 1.8Mb

    ZF is a nice PHP framework but i am interesting only library and i am using Zend library + Smarty3 to build my own PHP framework and still going well ;-) but looking for FuelPHP for more professional system in future application.

    what’s interesting in FuelPHP?
    - PHP5.3 + framework is a web framework you can use for future not just for now.
    - 60+ developers in team better than my own framework for sure. SF2 there have much more developers but i am not care because they will not come to answer your questions in Forums and IRC. but with FuelPHP you got there help from developers come to answer you on forum and even on IRC.
    - Future up to date system. keep my web app up to date .
    - and lot more and don’t know what to saying here.

    I have been try FuelPHP and seem what i am looking for. just wait to see more tutorial and take some more time with documents and learn.

    FuelPHP is a free application an opensource and non-profit software. There system may looks like CI or Kohana clone but there is something similar because of PHP code and concept. there should happen with many kind of a PHP framework. but a point is who can do better jobs, know issue and done fixed it.

    FuelPHP seem to be more friendly with easy and understand documents, there is a real community framework application that developer team come to join in forum and IRC to answer your question as well. you will not met this with SF2, ZF and Yii for sure.

    What to do now? I’ll try to learn FuelPHP harder, give then a question and wait for there friendly help. and enjoy to build my app.

    I have been use many PHP framework and FuelPHP now.. if you don’t like or not use FuelPHP. I don’t care.. because it’s much better than another PHP framework that even met.

    NOTE: My English grammar is not good, please ignore mistake from me. I am try my best to wrote here.

    • Phicheth K.

      this is update from me. after been leave a comment from last night. I am taken a few hours to read FuelPHP docs and tried to transfer my project from SF2 that have a problem with asset without error or bugs from my one project. and having done without any problem it’s fast and easy to learn for new developer.

      in SF2 I love there docs that have very great detail with example .. but when i am face with FuelPHP docs there is more friendly and so very easy to learn with useful and customize class. if you wanna create your own class on FuelPHP there have detail and example to use and create class that fit you.

      I love that asset and html class in FuelPHP there is very easy and useful class..

      At first face with FuelPHP to be honest all looks like CI.. but after try FuelPHP to transfer my website to FuelPHP there is not CI anymore. if you still said that FuelPHP is clone from Kohana and CI.. it mean you still not even try FuelPHP.

      FuelPHP is freindly PHP framework that eve met. Customizable to make your web application that fit your style and you can transfer your existing website from another framework pretty easy.

      If you worry about Layout & View. you’ll love fuelphp that pretty easy to render view you may imagine that how easy to use include file in PHP there is another way in FuelPHP to make you understand and easy to manage Layout & View

      Layout & View Compare with another framework.
      - Symfony2, can given you to render view from block in twig template seem fast and easy to use but asset is a problem ( on my project )
      - Yii, there is no way for you to make it easy to render layout and yii given you to render view with widget models that I still not understand how it’s work in easy ways with less detail and example in docs.

      Yii framework is a fashion of PHP framework that promote web 2.0 with Ajax integrate. When i am try to learn and use Yii there is difficult way to adding your own javascript into your project. so you need to install yii extension if you need more.

      All you need more easy to learn and understanding system, friendly docs, customizable, true community and completely help from there own developers..

      Wanna say thank you for all FuelPHP developers team that build a nice php framework for us. FuelPHP is once that fit my project after tried many framework.

      Thank you so much ” FuelPHP ” that stop me to searching another framework. FuelPHP fit me as well. given +150 ;-) plus plus plus

      NOTE: If you read my comment so do not believe until you even try by your self. After you even try just proof my comment is right that cool ;-)

      • http://philsturgeon.co.uk Phil Sturgeon
        Author

        Thanks for the thoughtful feedback! The grammar is not so bad ;-)

  • http://www.nukleo.fr Nukleo

    I just don’t get it when hateful people just smash in a quick resentful sentence (or two) what others are trying to offer freely to the community and bring us new options or ideas. You should just dig a hole, crall in it and shoot yourselves in the head.

  • http://www.andrecatita.com André Catita

    My life has been, i got my own Framework, who needs other frameworks.
    After a while.. Damn, sure keeping up a frame, even a little personal one is tiring, puff.

    Oh look Code Igniter is pretty.
    1 month later, yeah.. ok

    Oh look Kohana seems to have what it was lacking.. cool
    several months later.. damn, i’m getting pretty annoyed at “Ooops, new version, here we go again”

    Oh look Fuel, a Kohana fork.. oh wait
    wait… it’s the best so far, I’m quite surprised.

    Just keep on improving, I have faith in you guys.
    Simplicity, documentation and friendliness keep following CI style, and you’ll be the big boys around the block in no time. And i mean that as a good thing.

  • http://www.silicongulf.com Philippines IT School

    From a CI developer for several years, I found FUEL easy to migrate to.

    I did our site (continuously being updated now by students) entirely with FuelPHP to see if how far I can go with it.

    If I’m not mistaken, it’s the only MVC framework that starts of with PHP 5.3 good for teaching OOP to my students.

    Keep it up guys :)

  • http://www.51nodejs.com Lun

    it’s a little like nodejs framework~

  • http://brianswebdesign.com Brian Temecula

    I started with Kohana, but moved to CodeIgniter and have used it for the last couple of years. Lately I’ve been desiring and searching for a new framework, and some of the comments from others are helpful. I think I’ll be playing around with it on my next day off.

  • http://rommelcastro.me Rommel Castro A

    nice work Phil, ill check the repo and try to help in anything i can!!!! keep the good work

  • Ben

    Waiting eagerly for part 2. I’m learning some ORM stuff right now but a few simple paths through the maze would be very helpful.

    Thanks for all the work so far.

  • liam young

    Yes, Fuel is one good alternative … but when I see DataMapper ORM, Tank Auth and HMVC Codeigniter extensions works only with patches with ci 2.0.3… that is not so good… we lost focus.

  • Brian Brown

    Awesome tutorial. I read it with pleasure. I wait second part.

  • http://www.phpbbireland.com Mike

    I have downloaded, installed and tested most of the main frameworks available… I joined the forums, read hundreds of posts and built all the samples I could get my hands on… All of this in effort to learn the ins and outs of each framework with a view to picking one and using it for my next project… If I was going to have to go back to school and learn this stuff, I wanted to make damn sure I ended up with the best…

    The Installation:
    When I installed Fuel I was presented with a basic page with a few links, nothing out of the ordinary, better than some, not as good as others. At least it installed without me having to fix something (more that I could say about several others contenders).

    The Documentation:
    I could only find one document, so I assume this relates to the lack of documentation mentioned previously, I also note no one mentioned quality… for my part, I would gladly forego volume for quality.

    This excellent document is the main reason I choose Fuel… It not only provided the technical information I required, it also provided insight into the fundamentals (ethos) behind Fuel…

    I don’t know enough about frameworks to say Fuel is the best, I only know I prefer it over all the others I have tested…

    Mike

    • http://varemenos.com/ Varemenos

      Im not sure why you say Fuel’s docs are good.
      Maybe they are if you dont compare them to other frameworks like for example CodeIgniter’s docs.

      Still Fuel is awesome

  • http://varemenos.com/ Varemenos

    Any plans for the part 2 mentioned in the post?

    • http://philsturgeon.co.uk/ Phil Sturgeon
      Author

      Writing it now!

  • Aaron

    The proper spelling is Symfony, not Symphony.

    http://symfony.com

  • Alan

    Great tutorial, it’s strange reading so many pointlessly negative comments. I’m more of a frontend developer, but over the past two years I’ve been learning server side development. I started with rails, then I learnt about Django, and then due to the nature of my work I needed to use php so I learned Codeigniter. The key thing I’ve found is how close all the frameworks are to each other, and also to JS, Actioncript, and processing MVC frameworks, each tries to solve the same set of problems, but approaches it in different ways. Each time I learn a different approach it gives me a new perspective and this helps me to understand the other frameworks better.

    Thanks for giving me new things to learn.
    Fuel looks great, im really looking forward to learning it it.

  • Jorge

    I’ve been looking to some frameworks to choose from, like CI, Zend, Kohana, Yii and I have to say I’m impressed about FuelPHP. It looks so promising. I don’t understand such amount of negative comments. It’s great to count on a bunch of smart developers to help us doing a better programming job …jeez, this is not a religion! we are just talking about technology and getting things done better! Thanks so much Phil, just waiting for the second part :)

  • ChrisR

    Been looking into fuel for my next project I am liking it. Can not wait for part 2.

  • http://elainevellacatalano.com Elaine

    I currently use CI as I love working on the MVC architecture, but will most definitely give this a try. This should easily get me started :)

  • Guillaume Lambert

    Any updates on Part 2 ? Looking to implement Fuel in my agency, but waiting to learn more before anything ;)

    Thanks for all the work Phil & Co.!

  • Alfonso Alvarez

    Great job and tutorial!!, fuelphp its a great framework, fast, light and easy, keep the good work!

  • http://philsturgeon.co.uk Phil Sturgeon
    Author

    Sorry about the delay on part two guys. I was just busy for a long time then every time I picked the article back up I’d think of a great new feature to make it easier to write and that stopped me writing.

    I’ve got a finished draft of the article and I’m marking it up now, so we should see it on here in a week or two.

    • Guillaume

      Awesome, thanks man !

    • http://www.twitter.com/jvalecillos José Valecilllos

      I’ve been using CI for 2 years and was good for a few projects. But last year I switched to CakePHP and I think that cake has a better architecture. It supports console task, scaffolding is better (at least better than CI 1.7.x), template are properly managed (in CodeIgniter requires a lot of custom code). What does this framework have that no other offer?.

      On the other hand, as somebody said: “There is no bad programming language, only bad programmers”, the same concept apply to FrameWorks..