当前位置:Gxlcms > PHP教程 > 如何在同一个插件中添加多个action?

如何在同一个插件中添加多个action?

时间:2021-07-01 10:21:17 帮助过:23人阅读

比如我有个插件,叫做Test

class Test_Plugin implements Typecho_Plugin_Interface {
    public static function activate() {
        Helper::addAction('Test', 'Test_Action');   //Action.php
        Helper::addAction('Custom', 'Custom_Action'); //Custom.php
    }
    ...
    ..
    .
}

我想添加两个Action,分别是Test和Custom。。
这样写,访问action/Test是正常的,但是访问action/Custom却报错,说找不到。

请问能不能在一个插件中添加两个Action?如果可以,要怎么写代码?

回复内容:

比如我有个插件,叫做Test

class Test_Plugin implements Typecho_Plugin_Interface {
    public static function activate() {
        Helper::addAction('Test', 'Test_Action');   //Action.php
        Helper::addAction('Custom', 'Custom_Action'); //Custom.php
    }
    ...
    ..
    .
}

我想添加两个Action,分别是Test和Custom。。
这样写,访问action/Test是正常的,但是访问action/Custom却报错,说找不到。

请问能不能在一个插件中添加两个Action?如果可以,要怎么写代码?

widget/do.php代码

if (isset($widgetName) && class_exists($widgetName)) {
    $reflectionWidget =  new ReflectionClass($widgetName);
    if ($reflectionWidget->implementsInterface('Widget_Interface_Do')) {
        $this->widget($widgetName)->action();
        return;
    }
}

确实不可以,Custom_Action的文件include路径是Custom/Action.php,并且类名为Custom_Action

一个action就能够处理所有事情吧,如果需要增加action的话根据action的访问原理,你只能新增加一个plugins来定义它

人气教程排行