B
B
BadassRolf2017-08-07 07:48:28
Yii
BadassRolf, 2017-08-07 07:48:28

How to pass data to yii2?

How to pass data from web.php component like this

'components' => [
   'user' => [
            'class' => 'register\setUser',
            'username' => 'login',
            'password' => 'password_user',
        ]
]

How to pass all this to the setUser class?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Decadal, 2017-08-07
@BadassRolf

First, take a look at the Service-locator
link This is how configs work. Data from configs is passed to constructors using configuration objects.
And then - tell me, is this such a strange example for you to show what data you want to transfer, or are you seriously going to use such a user component?
At a minimum, the login-pass of the test user should be stored at least in the local file, because locally you can have one username-password, and in production - another.
And here is this link - more details on how to pass data to components

M
Maxim Timofeev, 2017-08-07
@webinar

Regarding the login and the standard User component, everything is simple:
But it's not possible to set it from the config. In general, if your class has a public variable
and it is defined in the config:

'components' => [
   'user' => [
            'class' => 'register\setUser',
            'moyaHren' => 'балалайка',
        ]
]

then no one will stop you from getting it like this:
I'll tell you more, you can make a method in this class:
public $moyaHren;

public function getChiyaOna($my='моя',$color="зеленовая"){
  return $my . ' ' . $color . ' ' . $this->moyaHren;
}

and to get
echo Yii::$app->user->getChiyaOna('его');
// получите "его зеленовая балалайка"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question