If you look under #3 in this link http://www.yiiframework.com/doc/guide/1.1/en/basics.controller it gives you a way to organize all of your controller functions into individual php files.
To do this you have to have each php file contain a class that extends CAction. Each class must have its running code inside of the function run().
This is my UserController.php file
public function actions()
{
$FOLDER = '.User';
$PROJECT_ROOT = 'application.controllers' . $FOLDER;
return array (
'list' => $PROJECT_ROOT . 'ListAction',
);
}
In the application that I am writing I need to pass variables to specific Actions.
Yii implemented runWithParams($params) in Yii 1.7 to allow this to happen. However, when I call write in the (for example) DeleteAction.php file runWithParams($params) instead of run() and our front end calls post/delete/?params=45 instead of run() I get an error that says “could not find run() in file DeleteAction.php
class ListAction extends CAction
{
public function runWithParam($id)
{
SiteController::actionLoggedin();
$id = addslashes($id);
}
Which means that I need run()…but I can’t pass parameters into run(). I need runWithParams($params).
In the documentation for the function runWithParams($params)
http://www.yiiframework.com/doc/api/1.1/CAction#runWithParams-detail
it says
Runs the action with the supplied request parameters. This method is internally called by CController::runAction().
I am confused what this means? I don’t understand how I can implement this successfully.
You just need to implement
run()in a CAction class.You will automatically have access to the $_GET params, of course, like you would in your action normally:
If you want to pass in additional constants to the CAction from the Controller, you can do it like this:
In your action setup in your Controller:
Then in your CAction:
I’m not sure what other parameters you might want to pass in to your action, but that should cover it. I hope that helps!
Update:
This question has made me look at the code in more depth, and this is actually all to do with a feature of Yii which I did not know about. If you declare a parameter on your action (i.e.
function actionTest($param)), then Yii will parse the $_GET parameters of a request and call the Action with that parameter passed in to the function. So you don’t have to get the $_GET parameter yourself. Like so:The way this is implemented is:
runAction()CController gets the $_GET parameters (if there are any) withgetActionParams()runAction()passes these parameters intorunWithParams()runWithParams()uses PHP Reflection to see if the action method has parameters (e.g.$param1)runWithParamsInternal()with the $_GET parametersrunWithParamsInternal()calls therun()method with the $_GET parameters (e.g.run($param1))run()without any parametersIt’s totally optional to use this, you can still access the $_GET parameters like normal in your action without. Basically, this just enforces required $_GET parameters. If your action needs a parameter but there are not $_GET parameters in the request URL, Yii returns an “Error 400” from
invalidActionParams(). It saves you the burden of checkingisset($_GET)in your action, which is kind of cool.