M
M
Mors Clamor2019-10-18 20:10:33
Yii
Mors Clamor, 2019-10-18 20:10:33

How to pass user id to view?

Good evening! I have a site that has a profile system. All ajax is releasing a js file, where there is the following function:

function getUserPosts(user_id) {
  $.ajax("/api/user/5/posts", {
      type: "GET",
....


Since the api is designed as a module, I did not turn off the sessions in it and check if the user has the right to do this or that action. What is the actual problem - you need to somehow pass the user_id to the view, because the user can go to both his own profile and someone else's, and depending on this variable, posts and so on will be received. How to do it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2019-10-18
@66demon666

You have a Profile controller and an actionView. You need to get Yii in it: $app->user->identify-getId()
Then search the database for its data by id.
If the posts need to be received by the current user, then the user_id does not need to be passed. You need to get it as written above.
I don't understand what is the problem? The question is a little unclear.

M
Mors Clamor, 2019-10-18
@66demon666

Yii:$app->user->identify-getId() gets the id of the current user. I'll explain in more detail. I need dynamics, ajax, that is. I made api as a module, through ajax (js script) I access it. There is a conditional variable user_id - the id of the user whose profile we are looking at, is responsible for the operation of the functions to actually receive a post, like, comment, etc. Actually, we can be on our profile. I realized that the question is strange, I thought to do something like
GET profile => profile (0 means that we are on our profile)
GET profile/ =>profile (if there is a parameter, we pass it)
But I thought it was very crooked

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question