yii2mod / yii2-comments

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

Unable to override controller. #12

Closed jsobers1331 closed 8 years ago

jsobers1331 commented 8 years ago

I've tried the following below to override your controller and it didn't work.


'controllerMap' => [
        'comments' => 'yii2mod\comments\controllers\ManageController'
  ]

Can you explain to me where exactly in the main config file i should place this code? I see no admin module section in this file. I'm using yii2 advanced .

Thanks.

ihorchepurnyi commented 8 years ago

Hi, if you use the advanced template, you can place this code in the main config for the your backend application. For example, you can place this code after this line. You can then access to comments management section through the following URL: your-backed-application-url/comments/index

jsobers1331 commented 8 years ago

I'm accessing the backend comments sections just fine. I want to modify the code you have in the Default Controller. But it's not picking up my file. Still using yours, even when i did your suggestion above.

ihorchepurnyi commented 8 years ago

Do you want override the ManageController?

jsobers1331 commented 8 years ago

Yes

ihorchepurnyi commented 8 years ago

Ok, you can create your own controller for example

namespace app\controllers;

class ManageController extends \yii2mod\comments\controllers\ManageController
{
    // your custom index action
    public function actionIndex()
    {
    }
}

And then edit the config:

 'controllerMap' => [
       'comments' => 'app\controllers\ManageController' // your own controller
 ],
jsobers1331 commented 8 years ago

I did that but it didn't work. i placed it after the module section in main config like you suggested and it didn't work. Before i had placed it in the module config like:

'comment' => [
            'class' => 'yii2mod\comments\Module',  
            'controllerMap' => [
                'DefaultController' => 'frontend\controllers\comments\DefaultCommentController'
            ],
        ],

and it didn't work like this either.

ihorchepurnyi commented 8 years ago

Ok, now i understand. If you want override the yii2mod\comments\controllers\DefaultController you can do it by the following code:

1) Create your own DefaultController

<?php

namespace app\controllers;

use Yii;

class DefaultController extends \yii2mod\comments\controllers\DefaultController
{
    // your custom create  comment action
    public function actionCreate($entity)
    {

    }
}

2) Modify config

'modules' => [
       'comment' => [
            'class' => 'yii2mod\comments\Module',
            'controllerNamespace' => 'app\controllers'  // namespace for the your default controller
        ],
   ],
jsobers1331 commented 8 years ago

Thank you i got it to work i think.