Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would follow this <a href="http://www.yiiframework.com/wiki/175/how-to-create-a-rest-api/" rel="nofollow">guide</a> on creating a REST API in Yii. After modifying the config urlManager entries, all of your API requests will use the APIController. You can then place the following code in the beforeAction of your APIController to return nothing if the user is a guest (Or an error message) </p> <pre><code>protected function beforeAction($event) { if (Yii::app()-&gt;user-&gt;isGuest) { echo "Invalid credentials"; Yii::app()-&gt;end(); } } </code></pre> <p>Note: The code above works for my purposes because all REST requests are sent via the same browser. (Which is already logged in and has a login cookie) </p> <p>If you replace that behavior with a new base controller placed in <code>protected/controllers</code> to force login, it will only apply to your pages that require a login and not your APIController. Here is an example of mine:</p> <pre><code>//Make sure all Controllers which require a login inherit from this class ControllerLoginRequired extends CController { public function runAction($action) { if (Yii::app()-&gt;user-&gt;isGuest &amp;&amp; 'site' != $this-&gt;route) { Yii::app()-&gt;user-&gt;returnUrl = $this-&gt;route; parent::redirect(array('site/login')); } else { parent::runAction($action); } } } </code></pre> <p>Everything explained will work for REST requests via the same browser in which the user has logged onto Yii. If you will have the need to expose your REST service to consumers that are not a browser logged into Yii, I believe you would have to come up with a custom authentication/token scheme.</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.
 

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