0
0

More than 5 years have passed since last update.

FuelPHP memo

Posted at

1.5系統

インストール

公式からダウンロード+php composer.phar self-update && php composer.phar install && php composer.php update

中身がこんな感じになる。

.
├── CHANGELOG.md
├── CONTRIBUTING.md
├── README.md
├── TESTING.md
├── composer.json
├── composer.lock
├── composer.phar
├── docs
│   ├── assets
│   ├── classes
│   ├── contribute.html
│   ├── credits.html
│   ├── favicon.ico
│   ├── favicon.png
│   ├── general
│   ├── index.html
│   ├── installation
│   ├── license.html
│   ├── packages
│   ├── requirements.html
│   ├── templates
│   ├── toc.html
│   └── vendor
├── fuel
│   ├── LICENSE
│   ├── app
│   ├── core
│   ├── packages
│   └── vendor
├── oil
└── public
    ├── assets
    └── index.php
  1. fuel: メイン
    1. fuel/app: アプリケーション使う
    2. fuel/core: FuelPHP 本体
    3. fuel/packages: FuelPHP パッケージ
  2. public: 公開ディレクトリ
  3. docs: ドキュメント

Routing

例: http://example.com/example/index/$id

<?php
class Controller_Example extends Controller {
  public function action_index ($id) {  }
}

規約:ファイル名やフォルダー名は全て小文字(FuelPHPの規約)

クラス名の_はディレクトリ区切りを意味する

優先順位

  1. Controller_Time_Message
  2. Controller_Time { function action_index () {} }

http://example.com/time/message の場合、2が優先されて実行される

View

fuel/views に置く。

例: このプログラムの時、 fuel/views/time/time_view.php を見る

<?php 
class Controller_Time extends Controller {
    public function action_index () {
        $data['title'] = "時刻めっせーじ";
        $data["now_time"] = date("H:i:s");
        return View::forge(
            'time/time_view' /* View ファイルを指定 */,
            $data
        );
    }
}

ORM

  1. config.php
<?php
return array(
    'always_load'  => array(
        'packages'  => array(
            'orm',
        ),
    );

see: http://fuelphp.com/docs/packages/orm/creating_models.html

Find

// find all articles
$entry = Model_Article::find('all');

// find all articles from category 1 order descending by date
$entry = Model_Article::find('all', array(
    'where' => array(
        array('category_id', 1),
    ),
    'order_by' => array('date' => 'desc'),
));

// find all articles from category 1 or category 2
$entry = Model_Article::find('all', array(
    'where' => array(
        array('category_id', 1),
        'or' => array(
            array('category_id', 2),
        ),
    ),
));

Migration && Model

なんかoilコマンドで作るのが普通っぽいけど無視。

fuel/app/classes/model/poster.php
<?php

class Model_Poster extends \Orm\Model
{
    protected static $_properties = array(
        'id',
        'title' => array('validation' => array('required', 'min_length' => array(3), 'max_length' => array(20)),
                         'data_type' => 'varchar'),
        'body' => array('data_type' => 'text'),
        'created_at',
        'updated_at',
    );

    protected static $_observers = array(
        'Orm\Observer_CreatedAt' => array(
            'events' => array('before_insert'),
            'mysql_timestamp' => false,
        ),
        'Orm\Observer_UpdatedAt' => array(
            'events' => array('before_update'),
            'mysql_timestamp' => false,
        ),
    );
    protected static $_table_name = 'poster';

}
fuel/app/migrations/001_poster.php
<?php

namespace Fuel\Migrations;

class Poster
{

    function up()
    {
        \DBUtil::create_table('posterp', array(
            'id' => array('type' => 'int', 'constraint' => 5),
            'title' => array('type' => 'varchar', 'constraint' => 100),
            'body' => array('type' => 'text'),
            'created_at' => array('type' => 'datetime'),
            'updated_at' => array('type' => 'timestamp'),
        ), array('id'));
    }

    function down()
    {
       \DBUtil::drop_table('poster');
    }
}

(ヽ´゜ω゜) < テーブル名に三単現のsが入るの気づかずはまってたでござる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0