| [[PHP]] | [[XAMPP]] |
==FuelPHP==
{{amazon|4899774222}}
----
[[PHP|PHPUnitのインストール]]
=====起動=====
-----
<pre>
ubuntu@puli-sub1:/opt/lampp$ sudo ./lampp start
Starting XAMPP for Linux 8.0.2-0...
XAMPP: Starting Apache...ok.
XAMPP: Starting MySQL...ok.
XAMPP: Starting ProFTPD...ok.
</pre>
===ライブラリ===
*fuel/app/config のサブフォルダ
====ログ====
----
*fuel/app/logs に日付別で出力される
<pre>
Log::info('hoge');
</pre>
=====出力レベル=====
*出力レベルは、config.php に指定
<pre>
'log_threshold' => Fuel::L_ALL,
</pre>
=====オブジェクトをログに出力=====
*[https://www.php.net/manual/ja/function.print-r.php print_r]を利用
<pre>
Log::info(print_r($result, true));
</pre>
*[https://www.php.net/manual/ja/function.var-dump.php var_dump]
===oil===
----
<pre>
$ php oil console
PHP Fatal error: Array and string offset access syntax with curly braces is no longer supported in /home/piroto/workspaces/sample2/fuel/packages/oil/classes/console.php on line 176
</pre>
*[https://qiita.com/rana_kualu/items/6ac293f238c4e6000720 中括弧による文字列配列アクセス削除]
*$c = $line{$i}; を以下に修正
*$c = $line[$i];
<pre>
piroto@puli-sub1:~/workspaces/sample2$ php oil console
Fuel 1.8.2 - PHP 8.0.2 (cli) (Feb 5 2021 21:42:11) [Linux]
>>>
</pre>
!内容
|-
|[http://fuelphp.jp/docs/1.6/classes/database/db.html DB]
|DB操作
|-
|-
|}
===[[Database]]===
----
*提供される操作方法
{|class="wikitable"
!操作方法
!内容
|-
|[http://fuelphp.jp/docs/1.6/classes/database/db.html DB::query()]
|SQLをそのまま
|-
|クエリビルダー
|SQLをメソッドで組み立て
|-
|Model_Crudクラス
|テーブルCRUD操作のメソッドが実装されている
|-
|ORMパッケージ
|ORMツールを使用
|-
|}
====[[Databse]]の作成====
<pre>
piroto@puli-sub1:~$ mysql -u root -p
Enter password:
:
MariaDB [(none)]> create database sample_db default character set utf8;
Query OK, 1 row affected (0.002 sec)
</pre>
====Userの作成====
*'%' は、localhostのワイルドーカードにはならない
<pre>
MariaDB [(none)]> create user 'sample_user'@'%' identified by 'password';
Query OK, 0 rows affected (0.011 sec)
MariaDB [(none)]> grant all privileges on sample_db.* to 'sample_user'@'%' with grant option;
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> create user sample_user@localhost identified by 'north123';
Query OK, 0 rows affected (0.003 sec)
MariaDB [(none)]> grant all privileges on sample_db.* to sample_user@localhost with grant option;
Query OK, 0 rows affected (0.003 sec)
</pre>
====設定====
*fuel/app/config/db.php
<pre>
return array(
'default' => array(
'type' => 'mysqli',
)
);
</pre>
*fuel/app/config/development/db.php
**developmentの部分は環境別
<pre>
return array(
'default' => array(
'connection' => array(
// 'dsn' => 'mysql:host=puli-sub1.local:3306;dbname=sample_db',
'hostname' => 'localhost',
'port' => '3306',
'database' => 'sample_db',
'username' => 'sample_user',
'password' => 'password',
),
),
);
</pre>
====確認====
*確認用コントローラー
<pre>
public function action_view() {
$result = DB::query('select * from sample_table')->execute();
$data = array(
'content' => print_r($result, true),
);
$view = View::forge('sample/sample_view', $data);
return Response::forge($view);
}
</pre>
*確認用View
<pre>
<textarea style="width:800px;height:400px">
<?php echo $content; ?>
</textarea>
</pre>
[[File:fuelphp_db_result_sample.png|400px]]
===Authパッケージ===
----
*http://fuelphp.jp/docs/1.79/packages/auth/intro.html
*Fuel における標準化された認証インターフェイスを提供
*ユーザは自分自身で新しいドライバを書くことができ、 基本的なメソッドを保つことで、古いコードに新しいドライバを簡単に統合可能
|}
<blockquote>Authパッケージには、ドライバに必要なテーブルを作成するためのマイグレーションファイルが含まれる。作成要否は、Auth設定ファイルで決定</blockquote>
====設定====
*fuel/packages/auth/config から、auth.php、simpleauth.php を fuel/app/config へコピー
*fuel/app/config/config.php
<pre>
'always_load' => array(
'packages' => array(
'auth',
),
)
</pre>
====[[Database]]テーブルの作成====
*http://fuelphp.jp/docs/1.9/general/migrations.html
*fuel/core/config/migrations.php を fuel/app/config/migrations.php にコピー
<pre>
$ sudo oil refine migrate:up --packages=auth
Error - Class 'MySQLi' not found in COREPATH/classes/database/mysqli/connection.php on line 116
</pre>
<blockquote>一旦、Migrationの使用は断念</blockquote>
*Authパッケージのマイグレーションファイルから、Userテーブル作成の該当部分を抜き出して、手動実行する
*package/auth/migrations/001_auth_create_usertables.php
<pre>
public function action_createusertable() {
$table = \Config::get('simpleauth.table_name', 'users');
// only do this if it doesn't exist yet
if ( ! \DBUtil::table_exists($table))
{
// table users
\DBUtil::create_table($table, array(
'id' => array('type' => 'int', 'constraint' => 11, 'auto_increment' => true),
'username' => array('type' => 'varchar', 'constraint' => 50),
'password' => array('type' => 'varchar', 'constraint' => 255),
'group' => array('type' => 'int', 'constraint' => 11, 'default' => 1),
'email' => array('type' => 'varchar', 'constraint' => 255),
'last_login' => array('type' => 'varchar', 'constraint' => 25),
'login_hash' => array('type' => 'varchar', 'constraint' => 255),
'profile_fields' => array('type' => 'text'),
'created_at' => array('type' => 'int', 'constraint' => 11, 'default' => 0),
'updated_at' => array('type' => 'int', 'constraint' => 11, 'default' => 0),
), array('id'));
// add a unique index on username and email
\DBUtil::create_index($table, array('username', 'email'), 'username', 'UNIQUE');
}
return "Created ${table}";
}
</pre>
[[File:fuelphp_auth_user_table.png|400px]]
====SimpleAuth====
*http://fuelphp.jp/docs/1.9/packages/auth/simpleauth/usage.html
*http://fuelphp.jp/docs/1.9/packages/auth/simpleauth/intro.html
*oil コンソールから、Auth::create_user()でテストユーザーを追加
<pre>
piroto@puli-sub1:~/workspaces/sample2$ php oil console
Fuel 1.8.2 - PHP 8.0.2 (cli) (Feb 5 2021 21:42:11) [Linux]
>>> Auth::create_user('sample','password','test@typea.info');
1
</pre>
=====コントローラー=====
*認証の確認
<pre>
public function action_login() {
$postparam = Input::post();
$name = $postparam['username'];
$pass = $postparam['password'];
Log::info("${name}/${pass}");
$user = Auth::validate_user($name, $pass);
Log::info(print_r($user, true));
$loginresult = (Auth::login($name, $pass)?'Success':'Fail');
$content = "User '${name}' login ${loginresult}.";
$data = array(
'title' => 'Auth Sample.',
'content' => $content,
);
$view = View::forge('sample/auth', $data);
return Response::forge($view);
}
</pre>
=====View=====
<pre>
<!DOCTYPE html>
<html>
<head>
</head>
<bodY>
<h1><?php echo $title; ?></h1>
<form action="login" method="POST">
<p>
ID:<input type="text" name="username" value="<?php echo $username??''; ?>"/>
PASSWORD:<input type="password" name="password"/>
</p>
<input type="submit">Login</input>
</form>
<textarea style="width:800px;height:400px">
<?php echo $content; ?>
</textarea>
</body>
</html>
</pre>
=====結果=====
[[File:fuelphp_simple_auth_success.png|600px]]
==Tips==
===WebAPI JSONデータを取得する===
----
*[http://fuelphp.jp/docs/1.7/classes/request/request.html Requestクラス]
*[https://www.php.net/manual/ja/json.constants.php JSON定数]
<pre>
<?php
class Controller_JsonSample extends Controller
{
function action_index() {
$url = 'http://hoge/json-api-sample.json';
$curl = Request::forge($url, 'curl');
$curl->set_method('get');
$curl->set_params(array());
$response = $curl->execute()->response();
$json_data = json_decode($response);
$data = [
'http_response_code' => $response->status,
'json_str' => json_encode($json_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ),
'somevalue' => $json_date->somevalue,
];
$view = View::forge('jsonsample/jsonsample', $data);
return Response::forge($view);
}
}
</pre>