如果你看一下这个链接中的#3 http://www.yiiframework.com/doc/guide/1.1/en/basics.controller,它给你提供了一种将所有控制器函数组织到单独的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);
}
这意味着我需要...but(),我不能将参数传递给run()。我需要runWithParams($params)。
在function runWithParams($params) http://www.yiiframework.com/doc/api/1.1/CAction#runWithParams-detail的文档中
上面写着
Runs the action with the supplied request parameters. This method is internally called by CController::runAction().
我搞不懂这是什么意思?我不明白如何才能成功地实现这一点。
发布于 2012-02-05 01:46:12
您只需要在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
}
}
如果你想把额外的常量从控制器传递给CAction,你可以这样做:
在控制器中的操作设置中:
public function actions() {
return array(
'userSearch'=>array(
'class'=>'application.controllers.User.ListAction', // path alias to your action
'model'=>'User',
'otherVariable'=>'something here',
));
}
然后在你的CAction中:
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的一个我不知道的功能有关。如果您在操作中声明了一个参数(即function actionTest($param)
),那么Yii将解析请求的$_GET参数,并使用传递给函数的参数调用操作。因此,您不必自己获取$_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"
}
它的实现方式是:
getActionParams()
runAction()
中的getActionParams()
runAction()
获取$_GET参数(如果有)将这些参数传递到getActionParams()
runAction()
中使用反射查看操作方法是否有参数(例如$param1
) runWithParamsInternal()
使用run()
参数(例如$_GET参数)调用$_GET方法
1. Otherwise it just calls `run()` without any parameters
使用它是完全可选的,你仍然可以像正常操作一样访问$_GET参数,而不需要。基本上,这只是强制执行所需的$_GET参数。如果您的action需要一个参数,但是请求URL中没有$_GET参数,那么Yii会从invalidActionParams()
返回“error400”。它为你省去了在操作中检查isset($_GET)
的负担,这是一件很酷的事情。
发布于 2015-02-04 18:53:09
从同样的Yii指南,你在你的描述中指出:
从1.1.7版开始,自动参数绑定也适用于基于类的操作。当使用某些参数定义操作类的run()方法时,将使用相应的命名请求参数值填充这些参数。例如,
class UpdateAction extends CAction
{
public function run($id)
{
// $id will be populated with $_GET['id']
}
}
就这么简单。在run()中定义所需的所有参数。
当然,您总是可以通过$_ get 'key‘或通过Yii::app()->request->getQuery('key')来获取
如果你在run中添加了它们,要知道如果你不总是要传递所有的参数,你可以设置默认值,否则你会得到错误。
示例:
public function run($id, $other_param='default_value')
https://stackoverflow.com/questions/9133678
复制