ふたりはララベル (Laravel PHP Framework)

PHPフレームワークのLaravelの体験記を書いていきます。こんなタイトルのブログですが萌え系アニメは一秒たりとも観たことがありません。

エクセルで作成したCSVファイルをLaravelに読み込み

Excelで作成したCSVファイルをLaravelに読み込んでみた。

$buf = mb_convert_encoding(file_get_contents(
  Input::file('csv')->getRealPath()),
  'utf-8',
  'sjis-win');

$lines = explode("\n", $buf);

array_pop($lines);

array_shift($lines);

最後のarray_popとarray_shiftは最初の一行と、最後の空行を取り除いている。

Laravel5でartisanコマンドを自作

artisan用のコマンドは名前が変わって「コンソール」になった。なのでmake:consoleでコマンドを作る。

php artisan make:console FutariUpdate --command=futari:update

app\Console\Kernel.phpにコマンドを登録する。

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel {

  protected $commands = [
    'Shoek\Console\Commands\Inspire',
    'Shoek\Console\Commands\FutariUpdate',
  ];

Laravelで定数を使う

前も書いた気がするけど、例として「constants.php」を「app/start」内に置く。

#app/start/constants.php
<?php

define('ADMIN', 'administrator');

「app/start/global.php」の末尾で「constants.php」を読み込む。

require app_path().'/filters.php';
require_once 'constants.php'; # ←追記

Sentryでnone givenのエラーが出る

LaravelのSeederを使ってSentry用のユーザを一気に追加しようとしたら、以下のエラーメッセージが出た。

  [Cartalyst\Sentry\Users\LoginRequiredException]
  A login is required for a user, none given.

そのときのSeederはこちら。

<?php

use League\Csv\Reader;

class UserSeeder extends Seeder {

  public function run()
  {
    $reader = Reader::createFromPath('./import/user.csv');

    $csvs = $reader->setDelimiter("\t")->fetchAssoc($reader->fetchOne());

    array_shift($csvs);

    foreach ($csvs as $csv) {

      Sentry::createUser($csv);
    }

  }

}
<||

CSVファイルの中身はこちら。
>||
username	email	password
MyUserName1	MyEmailAddress1 MyPassword1
MyUserName2	MyEmailAddress2 MyPassword2

原因はSentry::createUserにactivatedの情報が無かったから。そこでCSVファイルの1行目にactivatedを足して、その値を1にした。

username	email	password	activated
MyUserName1	MyEmailAddress1 MyPassword1	1
MyUserName2	MyEmailAddress2 MyPassword2	1

migrateにどうしても失敗する

migrateにどうしても失敗する場合がある。以下みたいなメッセージ。

php artisan migrate
Do you really wish to run this command? yes
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException",
  "message":"Class 'CreateUsersTable' not found",
  "file":"vendor\/laravel\/framework\/src\/Illuminate\/Database\/Migrations\/Migrator.php",
  "line":301}}

上記の場合だと「CreateUsersTable」が「vendor\composer」フォルダ内に登録されてないのが原因。

composer dump-autoload

で直る。

Laravelの自作コマンドで引数を取得する

自作コマンドを作成してみた。

php artisan command:make testFutari --command=test:futari


上記コマンドで生成されたソースにfire()とgetArguments()とgetOptions()の中身を追記した。

<?php

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class testFutari extends Command {

  /**
   * The console command name.
   *
   * @var string
   */
  protected $name = 'test:futari';

  /**
   * The console command description.
   *
   * @var string
   */
  protected $description = 'Command description.';

  /**
   * Create a new command instance.
   *
   * @return void
   */
  public function __construct()
  {
    parent::__construct();
  }

  /**
   * Execute the console command.
   *
   * @return mixed
   */
  public function fire()
  {
    print 'example = ' . $this->argument('example') . "\n";
    print 'optional = ' . $this->option('optional') . "\n";
    print 'none = ' . $this->option('none') . "\n";
  }

  /**
   * Get the console command arguments.
   *
   * @return array
   */
  protected function getArguments()
  {
    return array(
      array('example', InputArgument::REQUIRED, 'An example argument.'),
    );
  }

  /**
   * Get the console command options.
   *
   * @return array
   */
  protected function getOptions()
  {
    return array(
      array('optional', '-o', InputOption::VALUE_OPTIONAL, 'An example option.', null),
      array('none', null, InputOption::VALUE_NONE, 'An example option.', null),
    );
  }

}


そしてコマンドを実行したらこんな結果になった。

$ php artisan test:futari aaa -o bbb --none
example = aaa
optional = bbb
none = 1

ちなみにソース上でコマンドを実行する時は以下のように書く。

Artisan::call('test:futari', [
  'example' => 'aaa',
  '-o' => 'bbb',
  '--none' => true
]);

Laravelでデータを削除するときのForm文

Laravelでデータを削除するときのForm::openの書き方

{{ Form::open([
  'route' => ['comments.destroy',$comment->id],
  'method' => 'delete',
  'class'=>'form-horizontal'
]) }}
  <p><button type="submit" class="btn-u btn-u-blue">DELETE</button></p>
{{ Form::close() }}

Laravelのソース内でArtisanコマンドを使う

Laravelのソース内でArtisanコマンドを使う。

Artisan::call('mybench:mycommand');

引数付きで使う例。

Artisan::call('mybench:mycommand', ['year' => 2014]);

引数付きで使う場合、自作コマンド側の引数受け取り部分はこうなる。

/**
 * Get the console command arguments.
 *
 * @return array
 */
protected function getArguments()
{
  return array(
    ['year', InputArgument::OPTIONAL, '年']
  );
}

Laravelでラジオボックス

Laravelでラジオボックスの情報を取得したときのメモ。

Blade側

@foreach($comments as $comment)
<div class="inline-group">
<label class="radio">
  <input type="radio" name="priority_{{ $comment->id }}" value='abc'>
  <i class="rounded-x"></i>abc
</label>
<label class="radio">
  <input type="radio" name="priority_{{ $comment->id }}" value='efg'>
  <i class="rounded-x"></i>efg
</label>
<label class="radio">
  <input type="radio" name="priority_{{ $comment->id }}" value='hij'>
  <i class="rounded-x"></i>hij
</label>
</div>
@endforeach

コントローラー側。

$inputs = Input::all();

foreach ($inputs as $key => $value) {

  if (strpos($key, 'priority_') !== FALSE) {

    $id = str_replace('priority_', '', $key);

    $comment = Comment::find($id);

    $comment->priority = $value;

    $comment->save();
  
  }

}

WindowsにComposerをインストールする

以前手動インストールの方法を書いたので今度は自動インストール。


ComposerをWindowsに手動インストール - ふたりはララベル (Laravel PHP Framework)

Composer-Setupをダウンロードして実行する。以下の画面でphp.exeのパスを指定するだけ。
f:id:laravel:20150102111503p:plain

これで完了。以下のコマンドでバージョンが出てきたら成功している。

composer -V

Laravel5のスタート画面の格言たち

Laravel5のスタート画面では、格言がランダムで表示されるようになった。↓白くて薄い!
f:id:laravel:20141217004523p:plain

どうやって表示しているのだろう?と思ってtemplateの内容を見てみたら

Inspiring::quote()

というメソッドが格言を表示していた。ソースを追ってIlluminate\FoundationのInspiring.phpを開いたら「ユングフラウヨッホ山(標高3500M)にいるときにコミットしたわー」とLaravel作者のコメントが書かれていた。コミットした物理的な場所を書くのは珍しいな。

ちなみに表示される格言は以下のとおり。

When there is no desire, all things are at peace. - Laozi

「欲を持たなければ、全ては安らかとなる - 老子」 中国語だと「不欲以静 天下将自定」なので英語とはちょっと意味が違いますね。

Simplicity is the ultimate sophistication. - Leonardo da Vinci

「単純さは洗練の極みである レオナルド・ダ・ヴィンチ」 これは日本でも有名な格言です。

Simplicity is the essence of happiness. - Cedric Bledsoe

「単純さは幸福の本質である セドリック・ブレッドソー」 セドリック・ブレッドソーって誰?

Smile, breathe, and go slowly. - Thich Nhat Hanh

「笑い、呼吸をすることで、穏やかに生きる ティク・ナット・ハン」 ベトナムの坊さんの格言。この場合の「呼吸」は生理現象じゃなくて瞑想するときなんかの精神的な呼吸ですね。

Simplicity is an acquired taste. - Katharine Gerould

「単純さは獲得されるものである キャサリン・ジェロールド」 エッセイストらしい。この「単純」は「複雑さの対義語」です。

注目すべきなのは、半分以上の格言の主語が「Simplicity(簡素、単純)」であること。日本語の「単純」はネガティブなイメージがあるけど、Inspiring::quote()の「単純」はとてもポシティブだ。

私も仕事で「これ、プログラムで何とかなりませんか?」としょっちゅう依頼される。殆どの場合、依頼者たちは私が呆れるほど仕様を複雑に考えている。私が問題を解決すると「さすが!」と評価されるけど、私の頭が良いわけじゃない。プログラミングしているうちに物事をシンプルに捉えるクセが自然に身についただけだ。逆にネット上には私が悩んでいたプログラミング上の問題をシンプルに解決する解答ばかりだ。
単純であればあるほど論理的になっていく感覚はもっと広まっていいと思う。

そしてSimplicityを目指しているというLaravel5が楽しみだ。

ところでInspiring::quote()はネタにできそうな機能だ。フレームワークに疲れた心情を表現してみた。
f:id:laravel:20141217005511p:plain