ページ読み込み時に定数定義を行ってみます。
今回はプラグインの書き方で作成したplugin_baseプラグインに書き足す形で機能の追加を行います。
今回はページ読み込みの開始時にSOYCMS_PLUGIN_SAMPLE_MODEの定数を設けてみます。
/CMSインストールディレクトリ/common/site_include/plugin/plugin_base/plugin_base.php
<?php
class PluginBasePlugin{
const PLUGIN_ID = "plugin_base";
function getId(){
return self::PLUGIN_ID;
}
function init(){
CMSPlugin::addPluginMenu(self::PLUGIN_ID,array(
"name" => "プラグインの基本構造",
"type" => Plugin::TYPE_NONE,
"description" => "",
"author" => "",
"url" => "",
"mail" => "",
"version" => "1.0"
));
// 当プラグインが有効であるかを調べる
if(CMSPlugin::activeCheck(self::PLUGIN_ID)){
if(defined("_SITE_ROOT_")){
// 公開側ページの方で動作する拡張ポイントで使用したいものを追加する
// ページの出力直前の拡張ポイントを利用します。三番目の値の配列のonSiteAccessはプラグインのクラスに追加するメソッド名になります
CMSPlugin::setEvent("onSiteAccess", self::PLUGIN_ID, array($this, "onSiteAccess"));
}else{
// 管理画面側の方で動作する拡張ポイントで使用したいものを追加する
}
}
}
// CMSPlugin::setEventで追加した拡張ポイントの処理のコードを書きます。
function onSiteAccess($args){
// ページコントローラが格納されているが、ページを出力する為の準備は出来ていないので、あまり使えない
$controller = &$args["controller"];
// 前に呼び出される別のプラグインで定数の定義をしている可能性があるため、定数を定義する際に必ずdefinedで確認しておくと良い
if(!defined("SOYCMS_PLUGIN_SAMPLE_MODE")) define("SOYCMS_PLUGIN_SAMPLE_MODE", true);
}
public static function register(){
$obj = CMSPlugin::loadPluginConfig(self::PLUGIN_ID);
if(!$obj) $obj = new PluginBasePlugin();
CMSPlugin::addPlugin(self::PLUGIN_ID, array($obj, "init"));
}
}
PluginBasePlugin::register();
プラグインで使用できる拡張ポイントについてはhttps://github.com/inunosinsi/soycms/blob/master/cms/common/util/CMSPlugin.class.phpをご覧ください。
onSiteAccessでは、今回のように定数定義に用いたり、$_SERVER["REQUEST_URI"]の値を確認した後に他のページにリダイレクトを行ったりしています。
onSiteAccessについて実際のプラグインで確認したい場合は短縮URLプラグイン(プラグインID : UrlShortener)や携帯自動振り分けプラグイン(プラグインID : UtilMobileCheckPlugin)がおすすめです。
追記
onSiteAccessの拡張ポイントが実行されるタイミングは
https://github.com/inunosinsi/soycms/blob/master/cms/common/site_include/CMSPageController.class.php
のexecuteメソッドの序盤の
//CMS:PLUGIN callEventFunction
CMSPlugin::callEventFunc('onSiteAccess', array("controller" => $this));
で行われます。
