Oil utility
The Oil utility is a special package command can be used in several ways to facilitate quick development, help with testing your application and for running Tasks.
Generate
Code Generation can be used to speed up development times by building much of the repetitive code for you. This is entirely optionally - like all of oil - and all code can be edited however you like afterwards. You can generate the following items:
Controllers
To generate a skeleton Controller with actions and views predefined, use the following command:
$ php oil g controller posts action1 action2 action3 Created view: APPPATH/views/posts/action1.php Created view: APPPATH/views/posts/action2.php Created view: APPPATH/views/posts/action3.php Created controller: APPPATH/classes/controller/posts.php
This will produce a controller that looks like this:
class Controller_Posts extends Controller_Template {
public function action_action1()
{
$this->template->title = 'Posts » Action1';
$this->template->content = View::factory('posts/action1');
}
public function action_action2()
{
$this->template->title = 'Posts » Action2';
$this->template->content = View::factory('posts/action2');
}
public function action_action3()
{
$this->template->title = 'Posts » Action3';
$this->template->content = View::factory('posts/action3');
}
}
/* End of file posts.php */
Models
Generate a simple Model by listing fields and have the Migration automatically created for you to match:
$ php oil g model post title:varchar[50] body:text user_id:int created_at:datetime Created model: APPPATH/classes/model/post.php Created migration: APPPATH/migrations/001_create_posts.php
That will create a simple Model that uses Orm, so make sure the package is enabled in your config file. It will look like this:
class Model_Post extends Orm\Model { }
/* End of file post.php */
Not very exciting, but the migration is the useful part here:
namespace Fuel\Migrations;
class Create_posts {
function up()
{
\DBUtil::create_table('posts', array(
'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true),
'title' => array('constraint' => 50, 'type' => 'varchar'),
'body' => array('type' => 'text'),
'user_id' => array('constraint' => 11, 'type' => 'int'),
'created_at' => array('type' => 'datetime'),
), array('id'));
}
function down()
{
\DBUtil::drop_table('posts');
}
}
Migrations
You can generate migrations without creating a model. This could be used to rename a table, or add fields to a table in a way that is easy to deploy in other environments.
$ php oil generate migration rename_table_users_to_accounts Building magic migration: rename_table Created migration: APPPATH/migrations/002_rename_table_users_to_accounts.php
Magic Migrations
There are a number of "magic" migrations which automatically build you a migration based on a prefix to your migration name.
$ php oil generate migration create_users name:text email:string[50] password:string[125] $ php oil generate migration rename_table_users_to_accounts $ php oil generate migration add_bio_to_accounts bio:text $ php oil generate migration rename_field_name_to_username $ php oil generate migration drop_accounts
Note: Be careful when naming your migrations that you don't begin with any keywords by accident.
Scaffolding
Scaffolding is the really exciting part of Oil's code generation. This approach is heavily borrowed from Rails who have done a great job with it. The idea is that you create not only the MVC skeletons and migrations, but populate them with default CRUD code so the code will actually work after writing the command.
$ php oil g scaffold monkey name:string description:text Created model: APPPATH/classes/model/monkey.php Created migration: APPPATH/migrations/003_create_monkeys.php Created controller: APPPATH/classes/controller/monkeys.php Created view: APPPATH/views/monkeys/index.php Created view: APPPATH/views/monkeys/view.php Created view: APPPATH/views/monkeys/create.php Created view: APPPATH/views/monkeys/edit.php Created view: APPPATH/views/monkeys/_form.php $ php oil refine migrate Migrated to latest version: 3.
As you can see lots of code is generated by this command including a command that is executed in the second command. The controller looks like this:
class Controller_Monkeys extends Controller_Template {
public function action_index()
{
$data['monkeys'] = Model_Monkey::find('all');
$this->template->title = "Monkeys";
$this->template->content = View::factory('monkeys/index', $data);
}
public function action_view($id = null)
{
$data['monkey'] = Model_Monkey::find($id);
$this->template->title = "Monkey";
$this->template->content = View::factory('monkeys/view', $data);
}
public function action_create($id = null)
{
if ($_POST)
{
$monkey = Model_Monkey::factory(array(
'name' => Input::post('name'),
'description' => Input::post('description'),
));
if ($monkey and $monkey->save())
{
Session::set_flash('notice', 'Added ' . $monkey . ' #' . $monkey->id);
Response::redirect('monkeys');
}
else
{
Session::set_flash('notice', 'Could not save ' . $monkey . ' #' . $id);
}
}
$this->template->title = "Monkeys";
$this->template->content = View::factory('monkeys/create');
}
public function action_edit($id = null)
{
$monkey = Model_Monkey::find($id);
if ($_POST)
{
$monkey->name = Input::post('name');
$monkey->description = Input::post('description');
if ($monkey->save())
{
Session::set_flash('notice', 'Updated ' . $monkey . ' #' . $monkey->id);
Response::redirect('monkeys');
}
else
{
Session::set_flash('notice', 'Could not update ' . $monkey . ' #' . $id);
}
}
else
{
$this->template->set_global('monkey', $monkey);
}
$this->template->title = "Monkeys";
$this->template->content = View::factory('monkeys/edit');
}
public function action_delete($id = null)
{
$monkey = Model_Monkey::find($id);
if ($monkey and $monkey->delete())
{
Session::set_flash('notice', 'Deleted ' . $monkey . ' #' . $id);
}
else
{
Session::set_flash('notice', 'Could not delete ' . $monkey . ' #' . $id);
}
Response::redirect('monkeys');
}
}
/* End of file monkeys.php */