0

我正在以某种 MVC 风格构建一个 wordpress 插件,并且我已经设置了几乎所有东西,但是当使用 call_user_func 调用请求操作类时,我无法将 $this 与请求的类一起使用。

到目前为止,这是我的代码...

    class Action{

    protected $class;
    protected $method;
    protected $args = array();
    protected $template='settings/index';

    public function __construct() {
       $this->getRequest();

    }


    public function getRequest(){
        if(isset($_GET['c'])){
            $route = $_GET['c'];
            $parts = explode('/',$route);
            if(isset($parts[0]))
            $this->class = $parts[0];

            if(isset($parts[1]))
            $this->method = $parts[1];

            if(isset($parts[2]))
            $this->args[] = $parts[2];

            if(isset($parts[3]))
            $this->args[] = $parts[3];
        }
    }

    public function render(){

        extract($this->data);

        ob_start();

        require(LINKWAG_VIEW .DS. $this->template . P);

        $this->output = ob_get_contents();

        ob_end_clean();

        return $this;
    }

    public function output(){
        echo $this->output;
    }
}



class Grv extends Action{
    public function __construct() {
         parent::__construct();
        add_action('admin_menu', array($this,'setup'));
    }

    public function setup(){
         add_menu_page( 'LinkWag', 'LinkWag', 'manage_options', 'linkwag',array($this,'execute'), plugins_url( 'myplugin/images/icon.png' ), 6 ); 



    }

    public function  execute(){
        if($this->class){ 
           $this->controller = call_user_func_array(array($this->class,$this->method), $this->args);
        }
    }
}

请求的课程在这里

class Settings extends Grv{
public function __construct() {
    //parent::__construct();
}

public function view($id=false){
    $this->template = 'settings/view'; // using $this here craetes a fatal error

   $this->render();


}

}

假设我要求

admin.php?page=grv&c=settings/view/2

请告诉我我做错了什么..

4

1 回答 1

0

使用$this->render()而不是$this->class->render();

另一个是:添加 ucfirst 因为您的类名是首字母大写,并且您在参数中传递小写。

$this->controller = call_user_func_array(array(ucfirst($this->class), $this->method), $this->args)
于 2013-06-17T09:06:51.867 回答