at_yasu's blog

ロード的なことを

Laravel のメモ書き

メモ書き Laravel PHP


1. 基本的にMVC
2. 少しいじったけど、突貫物から中規模物に向いてる感じっぽい
3. /controller/action/arg1/arg2 とかできるけど、ルータで自分で独自のを仕向ける事が前提、最悪ルータに無名関数でアクションを書くことも出来るのがミソ。
4. routes.php には、配列などに納めて書くのではなく、ベタ書き。

routes.php の感じ

<?php
Route::get('/', function() {
	return View::make('home.index');
});

ルート(/)アクセスは、applications/views/home/index.blade.php を見る。home.index でそういう意味になるっぽい

<?php
Route::get('t/(:any?)', array('uses' => 'item@top'));

applications/controllers/item.php にある Item_Controller の action_top を呼び出す。

呼び出された item.php は View のインスタンスを返したらいいっぽい。

<?php /* applications/controllers/item.php */

class Item_Controller extends Base_Controller {
    public function action_top(){
        $query = Item::where("name", "=", URI::segment(2));
        if ((int)($query->count()) === 0)
            return Response::error('404');
        $books = $query->get();
        
        // name にはpath_info を、items にはDBにある情報を Item/top.blade.php に渡す
        $result = View::make('Item.top')
                    ->with("name", URI::segment(2))
                    ->with("items", $items);
        return $result;
    }
}


Item クラスは applications/models/Item.php にある。これでDBの情報取れる。楽。対多など関連性を持たせるには、関数作って、中で return $this->belong_to("hoge"); とかやるっぽい。

<?php /* applications/models/Item.php */
class Item extends Eloquent {
    public static $table = 'items';
}

テンプレートタグは blade と言われているのを使うっぽい。独自+PHPを実行する、みたいな?