LoginSignup
1
1

More than 5 years have passed since last update.

Laravel4をMacで動かしたときのメモ

Last updated at Posted at 2014-11-13

環境構築

PHPはMAMPものを利用(anyenvからだとハマったので割愛・・・)
/usr/local/binあたりにシンボリックリンク
複数のPHP環境があるとartisan実行時にこける
post-update-cmdにPHPのパスを絶対指定で書く
php artisan serveでローカルサーバ動かす場合はPHP >= 5.4で
Artisanからmigrate

SQLiteでmigrate

app/config/database.php
'default' => 'sqlite'

特に変更しなければ、production.sqliteで作成

$ php artisan migrate

でスキーマができてればSQLiteファイル生成

Seederを使って初期データ作成

モデルに個別のSeederを作成

app/database/seeds/UserTableSeeder.php
class UserTableSeeder extends Seeder {
    public function run()
    {
        DB::table('users')->delete();
        User::create(array(
            'email' => 'info@example.com',
            'name' => 'Test Man'
        ));
    }
}

モデルごとのSeederを呼び出し

app/database/seeds/DatabaseSeeder.php
<?php

class DatabaseSeeder extends Seeder {

    public function run()
    {
        Eloquent::unguard();

        $this->call('UserTableSeeder');
    }

}
$ db:seed

で実行

$ php artisan db:seed

コントローラー周り

JSONを返す

app/routes.php
Route::get('users.json', function()
{
    $users = User::all();

    return Response::json($users);
});

ルーティング

Resource

グループ

バリデーション

宣言的に書ける

public static $rules = array(
    'name' => 'required|max:50',
    'kana' => 'required|katakana|max:50',
    'email' => 'required|email',
    'booking' => 'required|date_format:Y-m-d|after:+3 days|before:+30 days',
    'document' => 'required|mimes:pdf,zip|max:10000',
    'comment' => 'max:65535',
);

afterとbeforeの日付指定が便利。strtotime()の形式で記述できる。
max, min, sizeはコンテクストで変わる。文字列なら文字数、ファイルならファイルサイズ。

日本語化

モデル名のカスタム

カスタムバリデーション

エラーの表示

Sccafold

認証

Vagrant

$ vagrant up
$ vagrant ssh
$ cd /vagrant
$ composer update
$ php artisan

メール

Laravel4では、Swiftを使ってメールを送信する

プレーンテキストで送りたい場合はiso-2022-jpへの変換が必用

start/global.php
\Swift::init(function()
{
    \Swift_DependencyContainer::getInstance()
        ->register('mime.qpheaderencoder')
        ->asAliasOf('mime.base64headerencoder');

    \Swift_Preferences::getInstance()->setCharset('iso-2022-jp');
});
routes.php
Route::get('entries/email', function()
{
    $data = array();
    $data['name'] = '山田 太郎';
    $data['kana'] = 'ヤマダ タロウ';
    $data['email' ] = 'taro@example.com';
    $data['comment'] = 'test';
    // text/plainで送る
    Mail::send(array('text' => 'emails.reply'), $data, function($message)
    {
        $message->to($data['email'])
            ->subject('ご連絡ありがとうございました')
            ->setCharset( 'iso-2022-jp' )
            ->setEncoder(new \Swift_Mime_ContentEncoder_PlainContentEncoder('7bit'));
    });
});

テンプレート側でmessageという変数名を使ってはいけない

Laravelの仕様で、メール送信時のテンプレートには$messageというインスタンスが渡される

views/emails/reply.blade.php
メッセージ: {{ $message }}
// Object of class Illuminate\Mail\Message could not be converted to string
// 上記のエラーが出る

Gmailで送る

config/mail.php
return array(
    'driver' => 'smtp',
    'host' => 'smtp.gmail.com',
    'port' => 465,
    'encryption' => 'ssl',
    'from' => array('address' => 'info@example.com', 'name' => 'Your Name'),
    'username' => 'your_name@gmail.com',
    'password' => 'your_password',

ファイルアップロード

ディレクトリを作る

if(!File::exists($dir))
{
    // intでパーミッション指定を忘れずに
    File::makeDirectory($dir, 707, true);
}

tmp領域からアップロードしたファイルを移動

if(Input::hasFile('file') && Input::file('file')->isValid())
{
    Input::file('file')->move($dir, $file_name);
}

ファイルパーミッションに注意

$ chmod -R 707 app/storage

logsやファイルアップロード時にディレクトリを作成する場合など、パーミッション指定を間違えないよう注意

1
1
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
1
1