Php 在Yii中正确实现runWithParams()

Php 在Yii中正确实现runWithParams(),php,yii,Php,Yii,如果您在本链接的#3下查看,它将为您提供一种将所有控制器函数组织到单个php文件中的方法 要做到这一点,您必须让每个php文件包含一个扩展CAction的类。每个类都必须在函数run()中包含其运行代码 这是我的UserController.php文件 public function actions() { $FOLDER = '.User'; $PROJECT_ROOT = 'application.controllers' . $FOLDER; return arr

如果您在本链接的#3下查看,它将为您提供一种将所有控制器函数组织到单个php文件中的方法

要做到这一点,您必须让每个php文件包含一个扩展CAction的类。每个类都必须在函数run()中包含其运行代码

这是我的UserController.php文件

public function actions()
{
    $FOLDER = '.User';
    $PROJECT_ROOT = 'application.controllers' . $FOLDER;

    return array (
        'list' => $PROJECT_ROOT . 'ListAction',
);
}

在我正在编写的应用程序中,我需要将变量传递给特定的操作

Yii在Yii 1.7中实现了runWithParams($params),允许这种情况发生。但是,当我在(例如)DeleteAction.php文件runWithParams($params)中调用write而不是run()时,我们的前端调用post/delete/?params=45而不是run()时,我得到一个错误,它说“无法在DeleteAction.php文件中找到run()

class ListAction extends CAction
{
public function runWithParam($id)
{
    SiteController::actionLoggedin();
    $id = addslashes($id);
}
这意味着我需要run()…但我无法将参数传递到run()。我需要runWithParams($params)

在函数runWithParams($params)的文档中

上面说

    Runs the action with the supplied request parameters. This method is internally called by CController::runAction().

我不明白这意味着什么?我不明白如何才能成功地实现它。

您只需要在一个CAction类中实现
run()

当然,您将自动访问$_GET参数,就像您在正常操作中一样:

class ListAction extends CAction {
  public function run() {
    $id = $_GET['id']; // $_POST works too
    $model = Model::model()->findbyPk($id);
    // render your view next, whatever
  }
}
如果要从控制器向计算传递其他常量,可以按如下方式执行:

在控制器的操作设置中:

public function actions() {
  return array(
    'userSearch'=>array(
      'class'=>'application.controllers.User.ListAction', // path alias to your action
      'model'=>'User',
      'otherVariable'=>'something here',
  ));
}
那么在你的计算中:

class ListAction extends CAction {
  public $modelName;
  public $otherVariable;
  public function run() {
    $this->modelName; //  'User' - passed in from the calling Controller
    $this->otherVariable; //  'something here' - passed in from the calling Controller
  }
}
我不确定您可能希望将哪些其他参数传递到您的操作中,但这应该包括在内。我希望这会有所帮助

更新:

这个问题让我对代码有了更深入的了解,这实际上与我不知道的Yii的一个特性有关。如果您在操作中声明一个参数(即
函数actionTest($param)
),然后Yii将解析请求的$\u GET参数,并使用传递给函数的参数调用操作。因此,您不必自己获取$\u GET参数。如下所示:

http://example.com/mycontroller/myaction?param1=test

function actionMyaction($param1) { // in a CAction, it would be run($param1)
  echo '$_GET parameter param1 set to '.$param1; // will echo "test"
}
其实施方式如下:

  • runAction()
    CController中,使用
    getActionParams()
  • runAction()
    将这些参数传递到
    runWithParams()
  • runWithParams()
    使用PHP反射查看操作方法是否有参数(例如
    $param1
  • 如果有参数,它将使用$\u GET参数调用
    runWithParamsInternal()
  • runWithParamsInternal()
    使用$\u GET参数调用
    run()
    方法(例如
    run($param1)
  • 否则它只调用
    run()
    ,不带任何参数

  • 使用此选项是完全可选的,您仍然可以在操作中像正常情况一样访问$\u GET参数,而不使用。基本上,这只是强制执行所需的$\u GET参数。如果您的操作需要参数,但请求URL中没有$\u GET参数,Yii将从
    invalidActionParams()返回“Error 400”
    。它省去了在操作中检查isset($\u GET)的负担,这很酷。

    您只需要在一个CAction类中实现
    run()

    当然,您将自动访问$_GET参数,就像您在正常操作中一样:

    class ListAction extends CAction {
      public function run() {
        $id = $_GET['id']; // $_POST works too
        $model = Model::model()->findbyPk($id);
        // render your view next, whatever
      }
    }
    
    如果要从控制器向计算传递其他常量,可以按如下方式执行:

    在控制器的操作设置中:

    public function actions() {
      return array(
        'userSearch'=>array(
          'class'=>'application.controllers.User.ListAction', // path alias to your action
          'model'=>'User',
          'otherVariable'=>'something here',
      ));
    }
    
    那么在你的计算中:

    class ListAction extends CAction {
      public $modelName;
      public $otherVariable;
      public function run() {
        $this->modelName; //  'User' - passed in from the calling Controller
        $this->otherVariable; //  'something here' - passed in from the calling Controller
      }
    }
    
    我不确定您可能希望将哪些其他参数传递到您的操作中,但这应该包括在内。我希望这会有所帮助

    更新:

    这个问题让我对代码有了更深入的了解,这实际上与我不知道的Yii的一个特性有关。如果您在操作中声明一个参数(即
    函数actionTest($param)
    ),然后Yii将解析请求的$\u GET参数,并使用传递给函数的参数调用操作。因此,您不必自己获取$\u GET参数。如下所示:

    http://example.com/mycontroller/myaction?param1=test
    
    function actionMyaction($param1) { // in a CAction, it would be run($param1)
      echo '$_GET parameter param1 set to '.$param1; // will echo "test"
    }
    
    其实施方式如下:

  • runAction()
    CController中,使用
    getActionParams()
  • runAction()
    将这些参数传递到
    runWithParams()
  • runWithParams()
    使用PHP反射查看操作方法是否有参数(例如
    $param1
  • 如果有参数,它将使用$\u GET参数调用
    runWithParamsInternal()
  • runWithParamsInternal()
    使用$\u GET参数调用
    run()
    方法(例如
    run($param1)
  • 否则它只调用
    run()
    ,不带任何参数

  • 使用此选项是完全可选的,您仍然可以在操作中像正常情况一样访问$\u GET参数,而不使用。基本上,这只是强制执行所需的$\u GET参数。如果您的操作需要参数,但请求URL中没有$\u GET参数,Yii将从
    invalidActionParams()返回“Error 400”
    。它可以省去您在操作中检查设置($\u GET)的负担,这有点酷。

    来自同一个Yii指南,您在描述中指出:

    从版本1.1.7开始,自动参数绑定也可以工作 用于基于类的操作。当操作类的run()方法 使用一些参数定义,它们将填充 对应的命名请求参数值。例如

    就这么简单。在run()中定义所需的所有参数

    当然,您始终可以通过$\u get['key']或通过Yii::app()->request->getQuery('key')获取

    在运行时添加这些参数时,要知道,如果不总是要传递所有参数,可以设置默认值,否则会出现错误

    例如:

    public function run($id, $other_param='default_value')
    

    从同一个Yii引导您在您的