title: How to use beforeAction in Yii or slug troubles
body: <p>Yii-jedis!</p>
  
  <p>I'm working on some old Yii-project and must to add to them some features. Yii is quite logical framework but it has some things I couldn't understand. Perhaps I haven't understand Yii-way yet. So I'll describe my problem step-by-step. For impatients - briefly question at the end.</p>
  
  <p><strong>Intro:</strong> I want to add human-readable URLs to my project. 
  Now URLs looks like: <em>www.site.com/article/359</em><br>
  And I want them to look like this: <em>www.site.com/article/how-to-make-pretty-urls</em><br>
  Very important: old articles must be available on old format URLs, and new - on new URLs.</p>
  
  <p><strong>Step 1:</strong> First, I've updated rewrite rules in <em>config/main.php</em>:</p>
  
  <pre><code>'&lt;controller:\w+&gt;/&lt;id:\S+&gt;' =&gt; '&lt;controller&gt;/view',
  </code></pre>
  
  <p>And I've added new <em>texturl</em> column to article table. So we will store here <em>human-readable-part-of-url</em> for new articles. Then I've updated one article with <em>texturl</em> for tests.</p>
  
  <p><strong>Step 2:</strong> Application show articles in <em>actionView</em> of <em>ArticleController</em> so I've added there this code for preproccessing ID parameter:</p>
  
  <pre><code>if (is_numeric($id)) {
      // User try to get /article/359
      $model = $this-&gt;loadModel($id); // Article::model()-&gt;findByPk($id);
      if ($model-&gt;text_url !== null) {
          // If article with ID=359 have text url -&gt; redirect to /article/text-url
          $this-&gt;redirect(array('view', 'id' =&gt; $model-&gt;text_url), true, 301);
      }
  } else {
      // User try to get /article/text-url
      $model = Article::model()-&gt;findByAttributes(array('text_url' =&gt; $id));
      $id = ($model !== null) ? $model-&gt;id : null ;
  }
  </code></pre>
  
  <p>And then begin legacy code:</p>
  
  <pre><code>$model = $this-&gt;loadModel($id); // Load article by numeric ID
  // etc
  </code></pre>
  
  <p>It <strong>works</strong> perfectly! But...</p>
  
  <p><strong>Step 3:</strong> But we have many actions with ID parameter! What we have to do? Update all actions with that code? I think it's ugly. I've found <a href="http://www.yiiframework.com/doc/api/1.1/CController#beforeAction-detail">CController::beforeAction</a> method. Looks good! So I declare beforeAction and place ID preproccessing there:</p>
  
  <pre><code>protected function beforeAction($action) {
      $actionToRun = $action-&gt;getId(); 
      $id = Yii::app()-&gt;getRequest()-&gt;getQuery('id');
      if (is_numeric($id)) {
          $model = $this-&gt;loadModel($id);
          if ($model-&gt;text_url !== null) {
              $this-&gt;redirect(array('view', 'id' =&gt; $model-&gt;text_url), true, 301);
          } 
      } else {
          $model = Article::model()-&gt;findByAttributes(array('text_url' =&gt; $id));
          $id = ($model !== null) ? $model-&gt;id : null ;
      }
      return parent::beforeAction($action-&gt;runWithParams(array('id' =&gt; $id)));
  }
  </code></pre>
  
  <p>Yes, it works with both URL-formats, but it executes <em>actionView</em> <strong>TWICE</strong> and shows page two times! What can I do with this? I've totally confused. Have I choose a right way to solve my problem?</p>
  
  <p><strong>Briefly:</strong> Can I proceess ID (GET-parameter) before execute of any actions and then run requested action (once!) with modified only ID parameter?</p>