ルビ振りAPIを使ってみる
最近PHPを勉強している。どうせなら何か作ってみたいなと思っていたところ、ルビ振りAPIhttps://developer.yahoo.co.jp/webapi/jlp/furigana/v1/furigana.htmlというものを見つけたのでこれを用いてMVCモデルを作成した。
C:\xampp\htdocs\laravelapp>php --version PHP 7.1.9 (cli) (built: Aug 30 2017 18:37:35) ( ZTS MSVC14 (Visual C++ 2015) x86 ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies C:\xampp\htdocs\laravelapp>php artisan --version Laravel Framework 5.5.12
RubihuriController8.php <?php //RubihuriController7.phpのクエリビルダ版 namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests\RubihuriRequest; use Validator; use Illuminate\Support\Facades\DB; class RubihuriController8 extends Controller { public function rubihuri_index(Request $request) { $items = DB::table('rubihuri')->get(); return view('yahoo.rubihuri_index',['items'=>$items]); } public function rubihuri_post(Request $request) { $items = DB::table('rubihuri')->get(); return view('yahoo.rubihuri_index',['items'=>$items]); } public function add(RubihuriRequest $request) { return view('yahoo.rubihuri7'); //$items = DB::table('rubihuri')->get(); //return view('yahoo.rubihuri5',['items' => $items]); } public function create(RubihuriRequest $request) { $api = 'http://jlp.yahooapis.jp/FuriganaService/V1/furigana'; $appid = '*****************************'; $msg = $request->input_value; $params = array( 'sentence' => $msg ); $ch = curl_init($api.'?'.http_build_query($params)); //curl_init() cURL セッションを初期化する curl_setopt_array($ch, array( CURLOPT_RETURNTRANSFER => true, CURLOPT_USERAGENT => "Yahoo AppID: $appid" )); //CURL 転送用の複数のオプションを設定する $result = curl_exec($ch); curl_close($ch); $obj = simplexml_load_string($result); $json = json_encode($obj,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); //取得したxmlをjsonに変換している $array = json_decode($json ,true); $string1 = $array["Result"]["WordList"]["Word"][0]["Furigana"]; $string2 = $array["Result"]["WordList"]["Word"][1]["Furigana"]; $string_linking = $string1 . $string2; $param = [ 'input_value' => $request->input_value, 'string_linking' => $string_linking, ]; DB::table('rubihuri')->insert($param); return redirect('/rubihuri8'); } public function edit(Request $request) { $item = DB::table('rubihuri')->where('id',$request->id)->first(); return view('yahoo.edit2',['form'=>$item]); } public function update(Request $request) { $param = [ 'string_linking' => $request->string_linking ]; DB::table('rubihuri')->where('id',$request->id)->update($param); return redirect('/rubihuri8'); } }
rubihuri_index.blade.php @extends('layouts.helloapp2') @section('title','ルビ振りAPI -test') @section('menubar') @parent インデックスページ @endsection @section('content') <table border="0"> <tr><th>input_value</th><th>string_linking</th></tr> @foreach ($items as $item) <tr> <td>{{$item->input_value}}</td> <td>{{$item->string_linking}}</td> </tr> @endforeach </table> @endsection @section('content2') @component('components.message') @slot('msg_title') mainページ @endslot @slot('msg_content') ここはデータ全体を表示するページです。 @endslot @endcomponent @endsection @section('footer') copyright 2019 ma_swan @endsection
helloapp2.blade.php <!DOCTYPE html> <html lang="ja"> <head> <title>@yield('title')</title> <link rel="stylesheet" href="css/styles.css"> </head> <body> <h1>@yield('title')</h1> @section('menubar') <ul> <p class="menutitle">helloapp2.blade.php(親)/rubihuri_index.blade.php(子)使用</p> <li>@show</li> </ul> <hr size="1"> <div class="content" align="center"> @yield('content') </div> <div class="content2"> @yield('content2') </div> <div class="footer"> @yield('footer') </div> </body> </html>
rubihuri7.blade.php @extends('layouts.helloapp3') @section('title','ルビ振りAPI -test') @section('menubar') @parent 新規フィールド追加ページ @endsection @section('content') <table border="0"> <form action="/rubihuri8/add" method="POST"> {{csrf_field()}} @if($errors->has('input_value')) <tr><th>ERROR</th><td>{{$errors->first('input_value')}}</td></tr> @endif <tr><th>input_value: </th><td><input type="text" name="input_value"></td></tr> <tr><th></th><td><input type="submit" value="send"></td></tr> </form> </table> @endsection @section('content2') @component('components.message') @slot('msg_title') Addページ @endslot @slot('msg_content') ここは新規フィールド追加のページです。 @endslot @endcomponent @endsection @section('footer') copyright 2019 ma_swan @endsection
helloapp3.blade.php <!DOCTYPE html> <html lang="ja"> <head> <title>@yield('title')</title> <link rel="stylesheet" href="../css/styles.css"> </head> <body> <h1>@yield('title')</h1> @section('menubar') <ul> <p class="menutitle">helloapp3.blade.php(親)/rubihuri7.blade.php(子)使用</p> <li>@show</li> </ul> <hr size="1"> <div class="content" align="center"> @yield('content') </div> <div class="content2"> @yield('content2') </div> <div class="footer"> @yield('footer') </div> </body> </html>
edit2.blade.php @extends('layouts.helloapp4') @section('title','Edit') @section('menubar') @parent 更新ページ @endsection @section('content') <table border="0"> <form action="/rubihuri8/edit" method="post"> {{ csrf_field() }} <input type="hidden" name="id" value="{{$form->id}}"> <tr> <th>string_linking: </th> <td><input type="text" name="string_linking" value="{{$form->string_linking}}"></td> </tr> <tr> <th></th> <td><input type="submit" value="send"></td> </tr> </form> </table> @endsection @section('content2') @component('components.message') @slot('msg_title') Editページ @endslot @slot('msg_content') ここは入力値の修正用のページです。 @endslot @endcomponent @endsection @section('footer') copyright 2019 ma_swan @endsection
helloapp4.blade.php <!DOCTYPE html> <html lang="ja"> <head> <title>@yield('title')</title> <link rel="stylesheet" href="../css/styles.css"> </head> <body> <h1>@yield('title')</h1> @section('menubar') <ul> <p class="menutitle">helloapp4.blade.php(親)/edit2.blade.php(子)使用</p> <li>@show</li> </ul> <hr size="1"> <div class="content"> @yield('content') </div> <div class="content2"> @yield('content2') </div> <div class="footer"> @yield('footer') </div> </body> </html>
RubihuriValidator.php <?php namespace App\Http\Validators; use Illuminate\Validation\Validator; class RubihuriValidator extends Validator { //このメソッドではマッチしなかったら1を返す。 public function validateRubihuri($attribute,$value,$parameters) { if(preg_match("/^[a-zA-Z0-9]+$/", $value)===0) return 1; } }
message.blade.php <style> .message{ border: double 4px #ccc; margin: 10px; padding:10px; background-color:#fafafa; } .msg_title{ margin: 10px 20px; color:#333; font-size:16pt; font-weight: bold; } .msg_content{ margin: 10px 20px; color:#333; font-size: 12pt; } </style> <div class="message"> <p class="msg_title">{{$msg_title}}</p> <p class="msg_content">{{$msg_content}}</p> </div>
styles.css body{ font-size: 16pt; font-family: Verdana,sans-serif; /*color:#999; */ /*color: rgb(0,255,0);*/ color:#333; margin:5px; } h1{ /*ここは「ルビ振りAPIてすと」のところ*/ font-size: 30pt; /*text-align: right; */ text-align: center; /*color: #f6f6f6; */ /*color: rgb(0,255,0);*/ color: #333; /*margin:-20px 0px -30px 0px; */ margin:0px 0px 0px 0px; letter-spacing: -4pt; } ul{ /*ここは「インデックスページ」のところ。*/ font-size: 20pt; text-align: center; list-style: none; } hr{ margin: 25px 100px; border-top:1px dashed #ddd; } .menutitle{/*※メニューのところ。classには.で対応する。*/ font-size: 20pt; font-weight: bold; margin: 0px; color: #333; } .content{ /*content全体の設定*/ margin:0px; /*text-align: right;*/ } table{/*テーブルを表示しているところ*/ margin:0px; } .footer{ text-align: right; font-size: 10pt; margin: 10px; border-bottom: solid 1px #ccc; color: #ccc; } th{ background-color:#999; color:#fff; padding:5px 10px; } td{ border: solid 1px #aaa; color:#999; color:#333; padding:5px 10px; font-size: 15px; } td:hover{ color:crimson; } input:hover{ color:royalblue; }
RubihuriServiceProvider.php <?php namespace App\Providers; use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; use Validator; use App\Http\Validators\RubihuriValidator; class RubihuriServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { $validator = $this->app['validator']; $validator->resolver(function($translator, $data, $rules, $messages)//オリジナルバリデータを追加 { return new RubihuriValidator($translator, $data, $rules, $messages); }); } /** * Register the application services. * * @return void */ public function register() { // } }
RubihuriRequest.php <?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class RubihuriRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { if($this->path() == 'rubihuri8/add') { return true; }else{ return false; } } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'input_value'=>'rubihuri', ]; } public function messages() { return [ 'input_value.rubihuri'=>'日本語で入力してね。', ]; } }
web.php <?php use App\Http\Middleware\HelloMiddleware; Route::get('rubihuri8','RubihuriController8@rubihuri_index'); Route::post('rubihuri8','RubihuriController8@rubihuri_post'); Route::get('rubihuri8/add','RubihuriController8@add'); Route::post('rubihuri8/add','RubihuriController8@create'); Route::get('rubihuri8/edit','RubihuriController8@edit'); Route::post('rubihuri8/edit','RubihuriController8@update');