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

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

Pythonのsubprocessの標準出力を入手する

Pythonのsubprocessを使って標準出力を入手したい。

import subprocess

cmdline = 'echo test'

p = subprocess.Popen(cmdline, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

while True:
  line = p.stdout.readline()

  if not line:
    break

  print(line.strip())

ところが、この場合「test」ではなく「b'test'」と表示される。

b'test'

デコードすれば結果だけを表示できる。

print(line.strip().decode('ascii'))

Python3とdecode

Python3で作ったスクリプトWindowsコマンドプロンプト上で実行したら、日本語部分のdecodeが失敗した。

この部分が

line.strip().decode('ascii')

こういうエラーを出す。

UnicodeDecodeError: 'ascii' codec can't decode byte 0x82 in position 28:
 invalid start byte

shift-JISにしたら直った。

line.strip().decode('shift-JIS')

エクセルで作成した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で自作メソッドにidを渡す

Laravel4で自作メソッドにidを渡す。

Viewの書き方

{{ Form::open(['action' => ['JisakuController@insert',$jisaku->id]) }}

routes.phpの書き方

Route::post('jisakus/{id}/insert', 'JisakuController@insert')

コントローラーの書き方

public function insert($id)
{

}

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, '年']
  );
}