date: 2013/12/21
Laravel4はほとんどさわったことない。ので、適当なBlogアプリケーションを作ってやってみるテスト
(15:02) Laravel 4.0 のドキュメントを見ながら、Laravel 4.1 をさわってる。
(16:23) Laravel 4.1 の日本語訳あるじゃん。つーかなにこの人すごい http://laravel4.kore1server.com
(17:14) なにやったかわすれた…とりあえず、認証まわりを作成できた
(17:51) 全部Push https://github.com/a-yasui/Laravel4PHPKansai
Composer でインストールして、プロジェクトを生成する.
- install
$ ./composer.phar create-project laravel/laravel myblog --prefer-dist
artisan コマンドを使って、DBのMigrationやアプリケーションを作成していく。
公開ディレクトリ
myblog/public
Database
簡単な概要
-
User
-
name
-
email
-
password
-
Blog
-
user_id
-
title
-
description
-
Post
-
blog_id
-
title
-
story
-
Comment
-
post_id
-
name
-
email
-
comment
Migrate
DB のテーブルを全部 Migrate に任せる。
-
myblog/app/config/database.php
の使用するDBの設定をする -
php artisan migrate:make initialSchema
を実行して、app/database/migrations/<今日の日付と時間?>_ initialSchema.php
ができる - <今日の日付と時間?>_ initialSchema.php の中にSchemaを書いていく。
- 注意点:
php artisan migrate:make schema
として Schemaクラスを生成した場合、内部で使用するクラス名のブッキングが起きて動かなくなるので注意
<?php
use Illuminate\Database\Migrations\Migration;
class initialSchema extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function($table) {
$table->increments('id');
$table->string('email')->unique();
$table->string('name');
$table->timestamps();
});
Schema::create("blogs", function($table) {
$table->increments('id');
$table->unsignedInteger("user_id");
$table->string("title");
$table->mediumText('description');
$table->foreign("user_id")->references('id')->on('users')->onDelete("cascade");
});
Schema::create("posts", function($table) {
$table->increments("id");
$table->unsignedInteger("blog_id");
$table->string("title");
$table->text('story');
$table->foreign("blog_id")->references('id')->on('blogs')->onDelete("cascade");
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table("posts", function($table){
$table->dropForeign("blog_id"); // ここよくわからん、index だから posts_blog_id_index になるかもしれない
});
Schema::table("blogs", function($table){
$table->dropForeign("user_id"); // 同上
});
Schema::drop("posts");
Schema::drop("blogs");
Schema::drop('users');
}
}
- Schema は、
up
メソッドでアップグレードの実行をして、down
でダウングレードの実行をする。 - Schema::create の無名関数の引数
$table
はIlluminate/Database/Schema/Blueprint
クラス。 - RDBのRelationがはれる。(これが嬉しい)
O/R Mapper
O/RMapperを作る。app/models
にクラスファイルを書く。User は元から(なぜか)あるので、それをコピー&ペーストして、他のテーブルのクラスを作っていく。
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class Post extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'posts';
/**
* 公開プロパティ?
*
* @var array
*/
protected $fillable = array('title', 'story');
/**
*
* @var Blog
*/
public function blog() {
return $this->belongsTo('Blog');
}
}
Routing and/or Controller
myblog/app/routes.php
にルーターを書いて、myblog/app/controllers/
にコントローラクラスを置いていく。
コントローラクラスはなくても、ルーターに直接、無名関数で処理を書く方法もある。
Auth
php artisan auth:reminders
php artisan auth:reminders-controller
<?php
// Login画面
Route::get('login', function () {
if (Auth::check())
return Redirect::to("blog");
return View::make("login");
});
// ログアウトさせる
Route::get("logout", function() {
Auth::logout();
return Redirect::to("/");
});
// ログインするアクション
Route::post('login', function () {
if (Auth::attempt(array('email' => Input::get("email"), 'password' => Input::get("password")), true))
{
return Redirect::to("blog");
}
return Redirect::to("login");
});
// 登録画面
Route::get('register', function()
{
return View::make('user.register');
});
// 登録処理
Route::post('register', function()
{
if (Auth::check())
return Redirect::to("blog");
// Validation を作成
$rules = array(
'name' => 'required',
'password' => 'required|min:8',
'email' => 'required|email|unique:users'
);
// Validation のエラーメッセージ
// app/lang/jp でやるのがいい?
$message = array(
'name.required' => "ユーザ名を入力してください",
"password.required" => "パスワードを入力してください",
"password.min" => "パスワードは8文字以上記入してください",
"email.required" => "E-Mailアドレスは必須",
"email.email" => "E-Mailアドレスは、半角英数で記入してくだし",
"email.unique" => "他のメールアドレスを指定してください。メールアドレスは既に登録されています。"
);
$validator = Validator::make(Input::all(), $rules, $message);
if ($validator->fails()) {
return Redirect::to('register')->withErrors($validator);
}
// 作成
$user = new User();
$user->name = Input::get("name");
$user->password = Hash::make(Input::get("password"));
$user->email = Input::get("email");
$user->save();
login::login($user);
return Redirect::to("/");
});
// 認証したページは下のグループで振り分ける
Route::group(array('before'=>'auth'), function() {
Route::any("blog", "BlogController@index");
Route::get("user", function(){});
});
template
普通のPHPとBladeが使える。
Blade
拡張子 .blade.php
Blade メモ
<form action="{{ action('RemindersController@postReset') }}" method="POST">
<input type="email" name="email">
<input type="submit" value="Send Reminder">
</form>
CSRF
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
Route::post('register', array('before' => 'csrf', function()
{
return 'You gave a valid CSRF token!';
}));
普通のPHP
CakePHPのように使うことができる。
拡張子 .php