Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You just need to implement <code>run()</code> in a CAction class.</p> <p>You will automatically have access to the $_GET params, of course, like you would in your action normally:</p> <pre><code>class ListAction extends CAction { public function run() { $id = $_GET['id']; // $_POST works too $model = Model::model()-&gt;findbyPk($id); // render your view next, whatever } } </code></pre> <p>If you want to pass in additional constants to the CAction from the Controller, you can do it like this:</p> <p>In your action setup in your Controller:</p> <pre><code>public function actions() { return array( 'userSearch'=&gt;array( 'class'=&gt;'application.controllers.User.ListAction', // path alias to your action 'model'=&gt;'User', 'otherVariable'=&gt;'something here', )); } </code></pre> <p>Then in your CAction:</p> <pre><code>class ListAction extends CAction { public $modelName; public $otherVariable; public function run() { $this-&gt;modelName; // 'User' - passed in from the calling Controller $this-&gt;otherVariable; // 'something here' - passed in from the calling Controller } } </code></pre> <p>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!</p> <p>Update:</p> <p>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. <code>function actionTest($param)</code>), 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:</p> <pre><code>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" } </code></pre> <p>The way this is implemented is:</p> <ol> <li>In <code>runAction()</code> CController gets the $_GET parameters (if there are any) with <code>getActionParams()</code></li> <li><code>runAction()</code> passes these parameters into <code>runWithParams()</code></li> <li><code>runWithParams()</code> uses PHP Reflection to see if the action method has parameters (e.g. <code>$param1</code>) <ol> <li>If there are parameters, it calls <code>runWithParamsInternal()</code> with the $_GET parameters <ol> <li><code>runWithParamsInternal()</code> calls the <code>run()</code> method with the $_GET parameters (e.g. <code>run($param1)</code>)</li> </ol></li> <li>Otherwise it just calls <code>run()</code> without any parameters</li> </ol></li> </ol> <p>It'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 <code>invalidActionParams()</code>. It saves you the burden of checking <code>isset($_GET)</code> in your action, which is kind of cool.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload