How to set home page in Yii2 - yii2

public function actionIndex() {
$this->layout = 'landing';
$loginForm = new LoginForm();
if (\Yii::$app->request->getIsPost()) {
$loginForm->load(\Yii::$app->request->post());
if ($loginForm->validate()) {
$user = $loginForm->getUser();
\Yii::$app->user->login($user);
return $this->goHome();
}
}
}
method goHome() sends to the home page. I have added '' => 'site/index' to the URL Manager earlier to send people to the SiteController and Index action, but Yii2 does not do anything. How to set up a correct home page rule?

You should write homeUrl parameter on config/main.php. For example:
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'homeUrl' => ['some/home-url-example'],
'modules' => [
...
],
...
]

Related

YII2 Editable Column does not work after PJAX

A column in gridview having EditableColumn integrated which works fine on each page load, but not after any PJAX call executions like search, pagination change etc.
In View EditableColumn code looks like this :
[
'class' => EditableColumn::class,
'attribute' => 'visit_status',
'url' => ['update'],
'type' => 'select',
'editableOptions' => function ($model) {
return [
'source' => [
'PENDING' => 'PENDING',
'APPROVED' => 'APPROVED',
'REJECTED' => 'REJECTED'
],
'value' => $model->visit_status,
'editable' => true
];
},
],
And Controller Action is like below:
public function actionUpdate()
{
$pk = unserialize(base64_decode(Yii::$app->request->post('pk')));
$model = MyModel::findOne($pk);
if ($model) {
$model->visit_status = Yii::$app->request->post('value');
if ($model->validate(['visit_status'])) {
$model->save(false);
} else {
throw new BadRequestHttpException($model->getFirstError('visit_status'));
}
}
}
If PJAX is disabled then it works fine, but cant compromize PJAX for that

yii2 is not getting id in action after $model->save() and when I click the update item icon

I have a yii2 project, I am developing on my windows localhost and hosting remotely on linux.
Locally (windows) every thing is perfect.
While on linux, I have $model->id = null after $nodel->save(), although data is saved.
public function actionCreate() {
$model = new AppBreakingNews();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
I have tried die($model->id) after the save, it printed null.
Moreover, when I click on the update icon in the grid view, I am facing the same problem.
The AppBreakingNews is as follows:
<?php
namespace app\models\appmodels;
use app\models\BreakingNews;
use yii\behaviors\TimestampBehavior;
class AppBreakingNews extends BreakingNews {
public function behaviors() {
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => date('Y-m-d H:i:s'),
],
];
}
}
Notice that appBreakingNews extends the model BreakingNews that is generated by yii2 without any change.
Thanks in advance..
Please try to save model->save(false); because i think it validate something from model file.
public function actionCreate() {
$model = new AppBreakingNews();
if ($model->load(Yii::$app->request->post()) && $model->save(false)) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
or can you please print your model file here.

Yii2 dektrium add new field to user model and change it in account form

I have added a new field to user model 'paypal' and need to change it in overridden account form.
Override user model
<?php
namespace common\models;
class User extends \dektrium\user\models\User
{
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios['create'][] = 'paypal';
$scenarios['update'][] = 'paypal';
$scenarios['register'][] = 'paypal';
return $scenarios;
}
public function rules()
{
$rules = parent::rules();
$rules['paypalLength'] = ['paypal', 'string', 'max' => 255];
return $rules;
}
}
Override SettingsForm model
<?php
namespace common\models;
class SettingsForm extends \dektrium\user\models\SettingsForm
{
public $paypal;
public function rules()
{
$rules = parent::rules();
$rules['paypalLength'] = ['paypal', 'string', 'max' => 255];
return $rules;
}
}
Configure module
'user' => [
'class' => 'dektrium\user\Module',
'modelMap' => [
'User' => 'common\models\User',
'RegistrationForm' => 'common\models\RegistrationForm',
'SettingsForm' => 'common\models\SettingsForm',
],
'controllerMap' => [
...
And I have overridden account form view. When I'm trying to change paypal field in user/settings/account it doesn't change it. What should I do to make it work?
Thanks.
Also you must override view paths:
'view' => [
'theme' => [
'pathMap' => [
'#dektrium/user/views' => '#app/views/user',
],
],
],
And after that open #vendor/dektrium/yii2-user/views and make a folder on #app/views based on dektrium view folders. For example create a folder named admin (because you have on #vendor/dektrium/yii2-user/views a folder named admin) and create corresponded folder on app views, i.e. #app/views/admin.
After that create your view file that you want to change on #app/views/[dektrium-folder] and change it.

Yii2 multi captcha in one page

I have a page that contains multi captcha in separated forms form example login and register modals , etc.
if I use below codes the problem is that when refresh one captcha then another captcha will be unusable because they use same session varible:
1) Login
login modal (view):
echo Captcha::widget([
'id' => 'Login-captcha',
'name' => 'LoginModel[captcha]',
'captchaAction' => '/site/captcha'
]);
LoginModel :
public function rules()
{
return [
['captcha', 'captcha'],
];
}
2) Register
register modal (view):
echo Captcha::widget([
'id' => 'register-captcha',
'name' => 'RegisterModel[captcha]',
'captchaAction' => '/site/captcha'
]);
RegisterModel:
public function rules()
{
return [
['captcha', 'captcha'],
];
}
to solve session problem I used different captcha actions to set different session variables:
1)Login
login modal view:
echo Captcha::widget([
'id' => 'Login-captcha',
'name' => 'LoginModel[captcha]',
'captchaAction' => '/site/captcha-login'
]);
LoginModel :
public function rules()
{
return [
['captcha', 'captcha', 'captchaAction' => 'site/captcha-login',],
];
}
2) Register
register modal (view):
echo Captcha::widget([
'id' => 'register-captcha',
'name' => 'RegisterModel[captcha]',
'captchaAction' => '/site/captcha-register'
]);
RegisterModel:
public function rules()
{
return [
['captcha', 'captcha', 'captchaAction' => 'site/captcha-register',],
];
}
until now everything is ok but when I move sessions from regular php files to database by below config in commponent section of main config file:
'session' => [
'class' => 'yii\web\DbSession',
],
then captchas in the first page load not works and have to refresh them to work correctly.
what is the problem?
Try this, it is for yii1 but you can get the idea, more detail
public function rules()
{
return array(
...
array('verifyCode1', 'captcha', ...
array('verifyCode2', 'verifycaptcha2', ...
);
}
public function verifycaptcha2($attribute, $params)
{
$captcha2 = Yii::app()->getController()->createAction('captcha2nd');
if (!$captcha2->validate($this->verifyCode2, false))
{
$this->addError('verifyCode2', 'invalid captcha.');
}
}
Also see this

Yii2: Returning an array of primary keys

Maybe I'm missing the essentials but why the following code will throw a Bad Request error (#400) complaining on "Missing parameter id" when rendering a view on a MySQL view?
In model:
public static function primaryKey()
{
return [
'vcostumbre_id',
'vbibliografia_id',
'vpagina_inicial',
];
}
In controller:
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
But this will work:
public function actionView($vcostumbre_id, $vbibliografia_id, $vpagina_inicial)
{
$id = [
'vcostumbre_id' => $vcostumbre_id,
'vbibliografia_id' => $vbibliografia_id,
'vpagina_inicial' => $vpagina_inicial,
];
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
Because in the URL you have not the parameter "id".
It should be /mycontroller/view?id=42".
Check the view file where the link is. It should be :
Url::to(['/controller/view', 'id' => 42])