備忘録です。OctoberCMSだんだん使い方分かってきて、component試してみました。
php artisan create:component my.company TestComponent
で雛形ができます。次にコンポーネントを利用するための登録を行います。
vim plugins/my/company/Plugin.php
public function registerComponents()
{
//return []; // Remove this line to activate
//ここで先程のcomponentを登録する
return [
'my\Company\Components\TestComponent' => 'TestComponent',
];
}
backendからlogout loginで登録されたcomponentが見えるようになります。
そこでテストページを作り、コンポーネントエリアからTestComponentをドロップダウンすると、コンポーネントとしての挙動開始します。
この段階では、
This is the default markup for component TestComponent
You can delete this file if you want
と表示されるだけで、コンポーネントのプロパティもエイリアスのみです。
作成したTestComponentを編集します。
vim plugins/my/company/components/TestComponent.php
//ここで定義したプロパティにアクセスできるようになります
public function defineProperties()
{
return [
'productid' => [
'title' => '商品番号',
'description' => '指定された商品番号の商品を展開します',
'default' => '{{ :slug }}',
'type' => 'string'
],
];
}
//コンポーネントが実際にページから描画される際は onRun()が呼ばれるようです
public function onRun(){
//コンポーネントエディタで登録したプロパティ値が取得出来てます
dump($this->property('productid'));
$db = new MyDB(); //モデルはここから呼び出せるものなら何でも良いはず
$val =$db->hoge($this->property('productid'));
dump($val); //コンポーネントがデータ受け取れていること確認
//次が少しまごつきましたが、$this->pageに渡すことで,componentのdefault.htmでも、
//呼び出す方の .htmでも使えるようになりました。
$this->page['val'] = $val;
component側の default.htm
vim plugins/my/company/components/testcomponent/default.htm
<p>default.htm start </p>
{{ dump (val) }}
<p>default.htm end </p>
貼り付けた方のhtm
vim themes/test/pages/testcomponent.htm
{% component 'Test' %}
<p>呼んだ方の始まり</p>
{{ dump(val)}}
<p>呼んだ方終わり</P>
で2回valがdump表示されます。
component はpartialの拡張版みたいなものに感じました。