1.TwigをFuelPHPで利用できるようにする
1-1.Vagrant再起動
まずはVagrantの起動から、、、
$ vagrant up
1-2.Vagrant再ログイン
そして、sshでサーバーへログインする
1-3.composer.jsonの編集
$ vi composer.json
開いたらその中に以下のようにrequireの記述があるので、そこへ「"twig/twig": "1.*"」の1行を追加する。
"require": {
"php": ">=5.3.3",
"composer/installers": "~1.0",
"fuel/docs": "dev-1.7/master",
"fuel/core": "dev-1.7/master",
"fuel/auth": "dev-1.7/master",
"fuel/email": "dev-1.7/master",
"fuel/oil": "dev-1.7/master",
"fuel/orm": "dev-1.7/master",
"fuel/parser": "dev-1.7/master",
"fuelphp/upload": "2.0.2",
"monolog/monolog": "1.5.*",
"twig/twig": "1.*"
},
記述が完了したらcomposer.jsonを保存して以下のコマンドを実行する
$ php compser.phar update
1-4.composerの更新
もし、以下のエラーが発生した場合は、composer自体を更新する
Could not parse version constraint ^2.0.0: Invalid version string "^2.0.0"
composer自体を更新コマンド
$ php composer.phar self-update
再度実行!
今度は正常に更新処理が完了しました。
※コンソールメッセージ
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing twig/twig (v1.33.2): Downloading (100%)
Writing lock file
Generating autoload files
1-5.config.phpの編集
次にfuel/app/configへ移動し、config.phpを開く
$ vi config.php
/**************************************************************************/
/* Always Load */
/**************************************************************************/
'always_load' => array(
/**
* These packages are loaded on Fuel's startup.
* You can specify them in the following manner:
*
* array('auth'); // This will assume the packages are in PKGPATH
*
* // Use this format to specify the path to the package explicitly
* array(
* array('auth' => PKGPATH.'auth/')
* );
*/
'packages' => array(
//'orm',
'parser', ←ここに追加
),
/**
* These modules are always loaded on Fuel's startup. You can specify them
* in the following manner:
*
* array('module_name');
*
* A path must be set in module_paths for this to work.
*/
// 'modules' => array(),
/**
* Classes to autoload & initialize even when not used
*/
// 'classes' => array(),
/**
* Configs to autoload
*
* Examples: if you want to load 'session' config into a group 'session' you only have to
* add 'session'. If you want to add it to another group (example: 'auth') you have to
* add it like 'session' => 'auth'.
* If you don't want the config in a group use null as groupname.
*/
// 'config' => array(),
/**
* Language files to autoload
*
* Examples: if you want to load 'validation' lang into a group 'validation' you only have to
* add 'validation'. If you want to add it to another group (example: 'forms') you have to
* add it like 'validation' => 'forms'.
* If you don't want the lang in a group use null as groupname.
*/
// 'language' => array(),
),
2.TwigでHTMLを書いてみる
2-1.welcome.phpの編集
まずは、app/classes/controller内のwelcome.phpを修正する
welcome.php
public function action_index()
{
//return Response::forge(View::forge('welcome/index'));
return Response::forge(View_Twig::forge('welcome/index'));
}
※「return Response::forge(View::forge('welcome/index'));」のままでも良いのだが、そのまま使用する場合は以下の用に修正する。
return Response::forge(View::forge('welcome/index.twig'));
2-2.index.twigの編集
そして、次にapp/views/welcome内に以下のhtmlを作成する。
index.twig
3.TwigをFuelPHPでレンダリングしてみる
上記HTML作成や修正が終了したら、アクセスしてみましょう
そしたら以下のようなエラーが、、、、
Fatal Error!
ErrorException [ Fatal Error ]:
Uncaught exception 'Fuel\Core\FuelException' with message 'Unable to create or write to the log file. Please check the permissions on /vagrant/project/fuelphp/fuelphp-1.7.3/fuel/app/logs/. (fopen(/vagrant/project/fuelphp/fuelphp-1.7.3/fuel/app/logs/2017/04/22.php): failed to open stream: Permission denied)' in /vagrant/project/fuelphp/fuelphp-1.7.3/fuel/core/classes/log.php:106 Stack trace: #0 /vagrant/project/fuelphp/fuelphp-1.7.3/fuel/core/classes/log.php(48): Fuel\Core\Log::initialize() #1 [internal function]: Fuel\Core\Log::_init() #2 /vagrant/project/fuelphp/fuelphp-1.7.3/fuel/core/classes/autoloader.php(375): call_user_func('Log::_init') #3 /vagrant/project/fuelphp/fuelphp-1.7.3/fuel/core/classes/autoloader.php(249): Fuel\Core\Autoloader::init_class('Log') #4 [internal function]: Fuel\Core\Autoloader::load('Log') #5 /vagrant/project/fuelphp/fuelphp-1.7.3/fuel/core/base.php(102): spl_autoload_call('Log') #6 /vagrant/project/fuelphp/fuelphp-1.7.3/fuel/core/classes/error.php(123): logger(400, 'Error - The
調べてみると
ドキュメントルートをvagrantの共有フォルダにしているからだそう。
再度アクセス、、、
直ってました!