How to change the language based on user preference in Yii2? - yii2

my site start with a default language(which is English) then based on user's preference i should change it. is this possible in Yii2 ? is there any widget for this

I use contentNegotiator, without assign a language to the user the language is automatically assigned by the application.
for this
In config/main.php in bootstrap section start the component
'bootstrap' => [
'log',
'contentNegotiator',
],
in component section
'components' => [
'contentNegotiator' =>[
'class' => 'yii\filters\ContentNegotiator',
'languages' => [
'en-US',
'it-IT',
'fr-FR',
],
],
],
otherwise you can change when and where you want. Is application action eg you can do in any controller you chose. this way
\Yii::$app->language = 'zh-CN';

Related

define access role to all user (user,guest,admin,..) in yii2

I use rbac (dektrium) and ACF to check to access users in my project (yii2). I created some role for example :admin, manager, suser,user,.. I have some actions that all user can use its for example view action. how can define in behaviors method that all user can use view action?
To do this we assigned actions to user '*' in yii1.
...
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('view'),
'users'=>array('*'),
),
...
in yii2 use this code ()
...
[
'allow' => true,
'actions' => ['view'],
'roles' => ['?'],
],
...
but when admin user or manager user want to access to myController/view shows forbidden. only guests can access to myController/view, how can define a role or access to access to all user by default?
If you want to allow everyone to access action then there is no need to apply access filter for that action. To avoid applying access filter for specific actions you can use $except property of yii\filters\AccessControl. For example like this:
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'except' => ['view'],
'rules' => [
//rules for other actions ...
]
],
//other behaviors ...
];
}
Other option would be to use a combination of roles ? (guests) and # (all logged in users) like this:
[
'allow' => true,
'actions' => ['view'],
'roles' => ['?', '#'],
]
There is also $only property in yii\filters\AccessControl that allows to apply filter only to explicitly named actions. But it's better to use $except for security reasons.
Resources:
yii\filters\AccessControl
yii\filters\AccessRule::$roles

How/where to set Yii2 Login Controller/Method

How/where do you set the controller/method Yii2 uses when a user is required to be logged in? Currently it goes to site/login and I'm trying to point it to auth/login.
Assuming you are using advanced template in your config/main.php (or main-local.php) you should define your user component g for frontented app
'user' => [
'identityClass' => 'app\models\web\User',
'enableAutoLogin' => true,
'loginUrl'=>['/frontend/auth/login'],
],

yii2 acf and rbac collaboration

Anyone know how to collaborate ACF and RBAC in yii2? i just want to add role name (ie: admin, contributors, editors) in my code:
'access' => [
'class' => AccessControl::className(),
'only' => ['logout', 'signup', 'try','about'],
'rules' => [
[
'actions' => ['try'],
'allow' => true,
'roles' => ['?','admin','editors'],
],
],
]
You can simply create proper permission in your rbac module (the rbac rule are not mandatory) and the set in your acf the action allow or deny to the permission create .. then the user assigned to this premission or group of permissions can accessing the action allowed ..
You can also test the permession for the user in you code
eg. assuming there is a set of users whit permissione manageUser you can test this way
if (Yii::$app->User->can('manageUser')) {
// your code for who ca manage user
}

How to customize vendor view files?

In yii2 how do I customize vendor view files without modifying the original view files?
I'm using dektrium yii2-user and would like to make a few changes to the login page.
You can assign your view path for dektrium yii2-user in this way (assume #app your app alias) :
'components' => [
'view' => [
'theme' => [
'pathMap' => [
'#dektrium/user/views' => '#app/views/your_dir_views' // mapping for override the views dektrium with your views
],
],
.....
],

Problems with Yii2 Rules creation

I have a Yii2 with Yii2 admin, user andAdminLTE installed. My problem is I don't know how to create rules, actually I don't know how to define the Class Name. Where "Classes" should be defined? How can I see which Classes do I have or add Classes?
Thanks a lot,
I don't know what is that module that you use but i know how to define rules for controller.please open a controller for example : mycontroller.
when you want create a rule for action in your mycontroller you should use the 'behaviors' function as you can see in bellow.
class MyController extends Controller {
public function behaviors() {
return [
'access' => [
//you can use this class is use for every controller "AccessControl::className()"
'class' => AccessControl::className(),
// use this rules just for these two actions(logout and signup)
'only' => ['logout', 'signup'],
//this is your rules for your controller's actions
'rules' => [
[
'actions' => ['signup'],
'allow' => true,
// '?' is the default roles in yii2
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
// '#' is the default roles in yii2
'roles' => ['#'],
],
],
],
];
}
i have two actions in this controller 'signup','logout'and i give roles to every actions.i give ? role to sign up and # role to logout.
? roles:means every user with out login can see this action.
# roles:means every user with login cans see this action.
as you can see the class in rules definition is static and you don't need to specify class you can just can use AccessControl::className() in your code.
best regards