-
-
Notifications
You must be signed in to change notification settings - Fork 296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
REST API Guide #165
Comments
@WadeShuler |
Yes, that would be good. |
any updates on this issue when will we be having the proper and complete guide for the REST API |
When someone contributes. I'm too busy with Yii 3.0 currently. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One of the issues I have just ran into, is using Yii2 to create a REST API. The official docs lack in this area (and many others). It takes into account, assumptions, that you know where things go. While I have used Yii2 for a while, it still would be nice to get the full story, instead of bits and pieces and having to find the rest. The docs say "Implement xxx", but doesn't tell you how, so we have to fill in the gaps. My experience with Yii helps me find these answers quickly, but beginners will get stuck on these things for days.
Your cookbook is helpful, and shows great examples of use-cases and a "how-to" for specific things. Which is why I think it would be great if you could add a full walk through on setting up a REST API, without the gaps of assuming everyone knows what they are doing. Assuming that they know nothing about Yii :)
While I just got a QueryParamAuth finally working, it was a pain. After following various guides (including official Yii docs), it leaves out a lot of necessary info. I will start with what I have so far.
I copied the "frontend" app, renamed it to "api", and removed unnecessary things like the "views" folder.
Here is a tree view:
api/config/main.php
api/modules/v1/components/ApiController.php
api/modules/v1/controllers/UserController.php
api/modules/v1/models/User.php
api/modules/v1/Module.php
Then lastly, outside of the API app, in "common" directory, modify
/common/models/User.php
to handle theaccess_token
Now, it still doesn't work. You have to manually put in an
access_token
in order for it to work. I grabbed it from another guide:Either manually create a new column, or create a migration
./yii migrate/create update_user_table
Replace with these 2 functions:
access_token
for the user in your db to: 4p9mj82PTl1BWSya7bfpU_Nm8u07hkcBNow you can call it using PostMan: http://api.mydomain.dev/v1/users?access-token=4p9mj82PTl1BWSya7bfpU_Nm8u07hkcB
NOTE: I have mapped api.mydomain.dev as a VHOST to
/path/to/yii2site/api/web
Now what? There is no guidance on how to set the
access_token
for the users, or how to revoke it. I assume this would be done on login. I can do this, but others may not be able to! You can create a login process for your API, but also could set it during normalfrontend
login, and use the API for filling the GridView and DetailView.What about revoking the token? If you look at the findByPasswordResetToken() function, it splits the token and checks the time stamp (second half after the underscore). The
findIdentityByAccessToken()
(As shown in Yii2 docs) doesn't show any verification on the token itself. You don't want the token to live forever unchecked!I think there needs to be a full step-by-step guide on how to properly setup a REST API, and go further by showing how to login, validate the token, handle invalid token (ie: session expired), logout (removing the token). Maybe split into a few parts to handle the different types: Query, BasicAuth, Custom, etc.
The text was updated successfully, but these errors were encountered: