yii2mod / yii2-comments

Comments module for Yii2
MIT License
158 stars 63 forks source link

Comments not saving. No error message. #72

Closed aagge closed 7 years ago

aagge commented 7 years ago

Hi,

I have done the installation and configuration.

In my view after entering data in the comments field and clicking Post nothing saves.

I checked the comment table and there is nothing saved. No error messages. Where can I check?

ihorchepurnyi commented 7 years ago

Hi, can you show your configuration, yii version, and template(basic or advanced)?

aagge commented 7 years ago

Hi,

Still learning Yii2 and I am working on my first project.

Yii version 2.0.12 I am using advanced template with dmstr/yii2-adminlte-asset. frontend\config\main.php

`<?php $params = array_merge( require(DIR . '/../../common/config/params.php'), require(DIR . '/../../common/config/params-local.php'), require(DIR . '/params.php'), require(DIR . '/params-local.php') );

return [ 'id' => 'app-frontend', 'name'=>'CCMS', 'basePath' => dirname(DIR), 'bootstrap' => ['log'], 'controllerNamespace' => 'frontend\controllers', 'components' => [ 'request' => [ 'csrfParam' => '_csrf-frontend', ], 'user' => [ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, 'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true], ], 'session' => [ // this is the name of the session cookie used for login on the frontend 'name' => 'advanced-frontend', ], 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], ], ], ], 'errorHandler' => [ 'errorAction' => 'site/error', ],

    /*
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
        ],
    ],
    */
],
'modules' => [
    'comment' => [
        'class' => 'yii2mod\comments\Module',
        // when admin can edit comments on frontend
        'enableInlineEdit' => false,
    ],
],    

'params' => $params,

];`

ihorchepurnyi commented 7 years ago

Here is my configuration for advanced template:

common/config/main.php

<?php

return [
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'components' => [
        'redis' => [
            'class' => 'yii\redis\Connection',
        ],
        'cache' => [
            'class' => 'yii\caching\DummyCache',
        ],
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
            'defaultRoles' => ['guest', 'user'],
        ],
        'urlManagerFrontend' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                '<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
            ],
        ],
    ],
];

frontend/config/main.php

<?php

$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/../../common/config/params-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/params-local.php')
);

return [
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'frontend\controllers',
    'modules' => [
        'comment' => [
            'class' => 'yii2mod\comments\Module',
        ],
    ],
    'components' => [
        'request' => [
            'csrfParam' => '_csrf-frontend',
        ],
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => [
                'name' => '_identity-frontend',
                'httpOnly' => true,
            ],
        ],
        'session' => [
            'name' => 'advanced-frontend',
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                '<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
            ],
        ],
        'i18n' => [
            'translations' => [
                '*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                ],
            ],
        ],
    ],
    'params' => $params,
];

View File: (site/index.php)

<?php

/* @var $this yii\web\View */

$this->title = 'My Application';
?>
<div class="environment-main-section">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <?php echo \yii2mod\comments\widgets\Comment::widget([
                    'model' => $model,
                ]); ?>
            </div>
        </div>
    </div>
</div>

Action:

    public function actionIndex()
    {
        $model = Post::findByTitle('some-post');

        return $this->render('index', compact('model'));
    }
ihorchepurnyi commented 7 years ago

I use yii 2.0.12, php 7.1, mysql 5.7 and all works fine.

aagge commented 7 years ago

So far still no luck. Your configuration looks similar to mine. It has to be something I am overlooking. I am still checking.

ihorchepurnyi commented 7 years ago

Try to add this rules to your frontend urlManager

'rules' => [
    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    '<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
],
ihorchepurnyi commented 7 years ago

Your urlManager should be like this

        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                '<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
            ],
        ],
aagge commented 7 years ago

Ok I got it working. My problem was in the view file.

What I was doing wrong was placing the code: <?php echo \yii2mod\comments\widgets\Comment::widget(['model' => $model,]); ?> inside the ActiveForm. Once I placed it outside it worked.

Thanks much for your help. This is a great component.

ihorchepurnyi commented 7 years ago

Thanks :)