#前提・実現したいこと
laravel
を使って 囲碁のQ&Aサイトを作りたいと思っています。
その導入として laravel
の基本的な掲示板の作り方を学ばなければいけないと思ってます。(と言うくらいのlaravel
入門者です)
それと並行して、PHP
で書かれた囲碁の碁盤表示 javascirpt
ツール「maxiGos
」を使いたいと思っています。
maxiGos
は
http://jeudego.org/maxiGos/
で入手でき、zipをダウンロードして解凍し、htdocs
などのドキュメントルート以下に配置することで該当ページに碁盤を表示することが出来ます。
#出来てること
MAMP
(apacheで動いている) の htdocs
に maxiGos
ディレクトリを作り、ダウンロードして出来た _maxiGos
ディレクトリ以下をコピペします。index.html
を
html
1<!DOCTYPE html> 2<html> 3<head> 4</head> 5<body> 6<h1>maxiGos</h1> 7<script src="_maxigos/_mgos/sgfplayer.php"></script> 8</body> 9</html>
として
http://localhost/maxiGos/
にアクセスすると、碁盤が表示されます。つかみはOK。
#やってみたこと
同様に、laravel (nginxで稼働) 配下で作成した
/Users/taro/development/laravel/maxiGos
(http://maxigos.dev/
でアクセス可能)
ディレクトリのpublic
に_maxiGos
を置いて、
route/web.php
に
php
1Route::get('maxiGos', function () { 2 return view('index'); 3});
を追加し、resources/view/
にindex.blade.php
を
php
1<!DOCTYPE html> 2<html> 3<head> 4</head> 5<body> 6<h1>maxiGos</h1> 7<script src="_maxigos/_mgos/sgfplayer.php"></script> 8</body> 9</html>
を作成します。この状態でブラウザでhttp://maxigos.dev/maxiGos
を見てもmaxiGos
と表示されるだけで碁盤は表示されません。laravel
には_maxigos/_mgos/sgfplayer.php
がどこにあるのか分からないからではないかと思っています。
そこで、laravel
に教えてあげるために、route/web.php
に
php
1Route::get('_maxigos/_mgos/sgfplayer.php', function() { 2 ob_start(); 3 require(public_path("_maxigos/_mgos/sgfplayer.php")); 4 $jsCode = ob_get_contents(); 5 ob_end_clear(); 6 return response($jsCode); 7 // ->header('Content-Type', 'text/javascript'); 8});
を追加してみました。
ちなみにsgfplayer.php
の中身は
php
1<?php 2// maxiGos v6.57 > sgfplayer.php 3header("content-type:application/x-javascript;charset=UTF-8"); 4include_once("../_php/gosLib.php"); 5$sgf=(isset($_GET["sgf"])?$_GET["sgf"]:(isset($_POST['sgf'])?$_POST['sgf']:"")); 6$cfg=(isset($_GET["cfg"])?$_GET["cfg"]:(isset($_POST['cfg'])?$_POST['cfg']:"")); 7$mxL=(isset($_GET["mxL"])?$_GET["mxL"]:(isset($_POST['mxL'])?$_POST['mxL']:"")); 8$plc=(isset($_GET["plc"])?$_GET["plc"]:(isset($_POST['plc'])?$_POST['plc']:"")); 9$inc=(isset($_GET["inc"])?$_GET["inc"]:(isset($_POST['inc'])?$_POST['inc']:"")); 10print gosStart($sgf,$cfg,$mxL,$plc,$inc);
となっているので、route/web.php
の中のheader
関数はコメントアウトしてみました。
#発生している問題・エラーメッセージ
この状態でブラウザをリロードしてもやはりmaxiGos
と表示されるだけです。
試しに直接http://maxigos.dev/_maxigos/_mgos/sgfplayer.php
を打ってみたところ、
Whoops, looks like something went wrong. (1/1) ErrorException include_once(../_php/gosLib.php): failed to open stream: No such file or directory in sgfplayer.php (line 4)
#試したこと
laravel
がgosLib.php
の場所が分からないのかと思い、route/we.php
に
php
1Route::get('_maxigos/_php/gosLib.php', function() { 2 ob_start(); 3 require(public_path("_maxigos/_php/gosLib.php")); 4 $phpCode = ob_get_contents(); 5 ob_end_clear(); 6 return response($phpCode); 7});
などと勝算もなく試してみましたがエラーは変わりませんでした。
#お聞きしたいこと
碁盤が表示されないのはgosLib.php
がどこにあるのかlaravel
が分からないからという憶測は正しいのでしょうか?もしそうであるのなら、教えてあげ方を教えてください。
そうでなく他に問題があるのでしたらそちらも教えて下さい。
#補足情報(言語/FW/ツール等のバージョンなど)
macOS Sierra バージョン10.12.5
Laravel 5.4
$ php -v
PHP 7.1.1 (cli) (built: Jan 23 2017 15:09:57) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
$ valet -v
Laravel Valet 2.0.4
$ nginx -v
nginx version: nginx/1.12.0
#蛇足
laravel
についてはlaracast
の英語のチュートリアル動画を頑張って観てみてます。それだけだと苦しいのでlaravel 5.1
の分厚い本を買いましたが、バージョンの違いで割りと役に立たず、「出版されてる一番マシだと思われる本でもこの状態か」と納得することで逆に精神安定剤になっています。後はググるかteratail
先生に頼るかなので、今にも心折れそうになりながら勉強してます。応援よろしくお願いします。
追記 (2017.7.16 18:17)
まず、上手く行ってる MAMP
でどう見えるかを確認しておきたいと思います。
ブラウザでhttp://localhost/maxiGos/
を表示させると、
こんな感じに表示されます。デフォルトで選ばれる木目の碁盤が表示されることが分かると思います。
とにかく laravel で碁盤を表示させる
次の目標はhttp://maxigos.dev/maxiGos
で同様に碁盤が表示されることです。
laravel
がgosLib.php
の場所を知らないのかと思い、route/web.php
を色々と試していたのですがそれは辞め、sgfplayer.php
の4行目の
php
1include_once("../_php/gosLib.php");
を
php
1include_once("_maxiGos/_php/gosLib.php");
と書き換えてあげました。
これでも碁盤は表示されないのですが、
http://maxigos.dev/_maxigos/_mgos/sgfplayer.php
を見ると、
Whoops, looks like something went wrong. (1/1) ErrorException include(_sample/neo-classic/_cfg/basic.cfg): failed to open stream: No such file or directory in gosLib.php (line 60)
とエラーが表示されました。これでgosLib.php
が見えたことが確認できました。
その60行目というのが
php
1 else include($gosRootRelativePath."_sample/neo-classic/_cfg/basic.cfg");
であったので、試しにその前に
php
1 dd($gosRootRelativePath);
を挿入してみると、長いjavascript
と思われるソースのようなものの最後の方に
html
1<span class=sf-dump-const>null</span>
と記述されていました。そこで、上のdd
の前に
php
1 $gosRootRelativePath = '_maxiGos/';
と明示的に代入してやったところ、
html
1<span class=sf-dump-str title="9 characters">_maxiGos/</span>
に変わりました。
この状態でブラウザをリロードすると、ついに
と碁盤が表示されました。
大進歩です。
Q1) sgfplayer.php
の4行目、_maxiGox/
が../
ではいけないのは何故でしょう?
碁盤が白黒(木目じゃない)のは?
見てお分かりのように、MAMP
だと木目調なのですが、laravel
では白黒です。
http://maxigos.dev/_maxigos/_mgos/sgfplayer.php
のはき出すjavascript
の中に
javascript
1mxG.AddCss("_sample/neo-classic/_css/basic.css");
とあるので、
http://maxigos.dev/_maxigos/_sample/neo-classic/_css/_common.css
を見てみると、
css
1div.mxNeoClassicGlobalBoxDiv div.mxGobanDiv canvas 2{ 3 background-image:url(../../../_img/bk/beech.jpg);background-size:cover; 4}
という一節が出てきます。「そうか」と、要はlaravel
は相対親ディレクトリ../
が苦手なのかと思い、ってことは、
http://maxigos.dev/_maxigos/_sample/neo-classic/_css/../../../_img/bk/beech.jpg
をみようとしてもダメなのねと思いながら見てみたら、なんと木目の画像が表示されました。
ちゃんと
http://maxigos.dev/_maxigos/_img/bk/beech.jpg
にリダイレクト(?)されていました。
とても意外でした。
Q2) laravel
では相対親ディレクトリ指定が難しいという解釈は間違ってるのでしょうか?
Q3) 順番に見ていったら画像が表示されたのに、何故http://maxigos.dev/_maxigos/
では碁盤の背景に木目画像が表示されないのでしょうか?
話は前後しますが、インクルードされる path.php というファイルについて
http://maxigos.dev/_maxigos/_mgos/sgfplayer.php
を見ると、
Whoops, looks like something went wrong. (1/1) ErrorException include(_sample/neo-classic/_cfg/basic.cfg): failed to open stream: No such file or directory in gosLib.php (line 60)
となるのは、gosLib.php
が最初にinclude_once
してるpath.php
というファイルの終わりで
php
1// 1) relative path (in theory, used in .php files) 2 3if (!isset($gosRootRelativePath)) $gosRootRelativePath=gosGetRootRelativePath(); 4 5// 2) absolute path (in theory, used in .js files) 6 7if (!isset($gosRootAbsolutePath)) $gosRootAbsolutePath=gosGetRootAbsolutePath();
とているのですが、それに続いて
php
1dd($gosRootRelativePath);
とすると、
"../../Users/taro/development/laravel/maxiGos/public/_maxiGos/"
php
1dd($gosRootAbsolutePath);
とすると、
"http://maxigos.dev/Users/taro/development/laravel/maxiGos/public/_maxiGos/"
と、明らかに意図しない値が帰って来てます。
(見易さのため、一旦sgfplayer.php
のheader()
関数はコメントアウトしてあります)
Q4) MAMP
では上手く動いてるようにみえるこのファイルがlaravel
では意図せぬ動きに見えるのはなぜでしょう?
一応path.php
の全文を載せておきます。
php
1<?php 2// maxiGos v6.57 > path.php 3// must be in the same folder as aloneLib.php and gosLib.php 4 5// $gosRootRelativePath is a relative path from php script that includes this script 6// to "_maxigos" folder (must end by "_maxigos/" or be "/") 7// $gosRootRelativePath can also be set in the calling php script of the web page that uses maxiGos 8// better to use a relative path (to the calling php scripts) to avoid access file problem 9// sometimes, it may be necessary to set it by hand in the php calling scripts 10// when gosGetRootPath() doesn't work (for instance when $_SERVER['DOCUMENT_ROOT'] is unkown) 11 12// $gosRootAbsolutePath is an absolute web path to "_maxigos" folder (ends by "_maxigos/") 13// it is used in maxiGos ".js" code to retrieve external resources such as css files, images, ... 14// warning: some servers seem to add "/" to the end of $_SERVER['DOCUMENT_ROOT'], some others don't 15 16function gosGetRootRelativePath() 17{ 18 $a=$_SERVER['PHP_SELF']; 19 $thisDir=substr(dirname(__FILE__),strlen($_SERVER['DOCUMENT_ROOT'])); 20 $len=strlen($thisDir); 21 $gosDir=substr($thisDir,0,$len-5); // -5 to remove "/_php" 22 $b=$gosDir."/"; 23 $k=0; 24 $c=""; 25 $km=substr_count($a,"/")-1; 26 while($k<$km) 27 { 28 $c=$c."../"; 29 $k++; 30 } 31 return $c.preg_replace("#^[/]+?#","",$b); 32} 33 34function gosGetRootAbsolutePath() 35{ 36 $thisDir=substr(dirname(__FILE__),strlen($_SERVER['DOCUMENT_ROOT'])); 37 $len=strlen($thisDir); 38 $gosDir=substr($thisDir,0,$len-5); // -5 to remove "/_php" 39 $gosDir="/".preg_replace("#^[/]+?#","",$gosDir); 40 $gosDir=preg_replace("#[/]+?$#","",$gosDir)."/"; 41 if (isset($_SERVER['HTTP_HOST'])) 42 { 43 // add protocol and host name 44 // useful when maxiGos is on a server different from the calling page one 45 $protocol=stripos($_SERVER['SERVER_PROTOCOL'],'https')===true ?'https://':'http://'; 46 $gosDir=$protocol.$_SERVER['HTTP_HOST'].$gosDir; 47 } 48 return $gosDir; 49} 50 51// 1) relative path (in theory, used in .php files) 52 53if (!isset($gosRootRelativePath)) $gosRootRelativePath=gosGetRootRelativePath(); 54 55// 2) absolute path (in theory, used in .js files) 56 57if (!isset($gosRootAbsolutePath)) $gosRootAbsolutePath=gosGetRootAbsolutePath();
以上、よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/16 08:54
2017/07/18 15:53
2017/07/18 22:57
2017/07/19 06:59
2017/07/19 07:08
2017/07/19 09:59
2017/07/19 10:57