Php 调用未知方法:yii2mod\cms\controllers\CmsController::setInstance()

Php 调用未知方法:yii2mod\cms\controllers\CmsController::setInstance(),php,yii2,yii2-extension,Php,Yii2,Yii2 Extension,我不明白为什么会发生这种错误 调用cms时获取错误 http://localhost/yii-cms/web/cms 调用未知方法:yii2mod\cms\controllers\CmsController::setInstance() 我正在尝试使用yii2 cms CMS控制器 <?php namespace yii2mod\cms\controllers; use Yii; use yii2mod\cms\models\CmsModel; use yii\web\Contr

我不明白为什么会发生这种错误

调用cms时获取错误
http://localhost/yii-cms/web/cms

调用未知方法:
yii2mod\cms\controllers\CmsController::setInstance()

我正在尝试使用
yii2 cms

CMS控制器

   <?php

namespace yii2mod\cms\controllers;

use Yii;
use yii2mod\cms\models\CmsModel;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii2mod\cms\models\search\CmsModelSearch;
use yii2mod\editable\EditableAction;
use yii2mod\toggle\actions\ToggleAction;

/**
 * Class CmsController
 * @package yii2mod\cms\controllers
 */
class CmsController extends Controller
{
    /**
     * @var string view path
     */
    public $viewPath = '@vendor/yii2mod/yii2-cms/views/cms/';

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'index' => ['get'],
                    'create' => ['get', 'post'],
                    'update' => ['get', 'post'],
                    'delete' => ['post']
                ],
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'edit-page' => [
                'class' => EditableAction::className(),
                'modelClass' => CmsModel::className(),
                'forceCreate' => false
            ],
            'toggle' => [
                'class' => ToggleAction::className(),
                'modelClass' => CmsModel::className(),
            ]
        ];
    }

    /**
     * Lists all CmsModel models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new CmsModelSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render($this->viewPath . 'index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel
        ]);
    }

    /**
     * Creates a new CmsModel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new CmsModel();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            Yii::$app->session->setFlash('success', Yii::t('yii2mod.cms', 'Page has been created.'));
            return $this->redirect(['index']);
        }

        return $this->render($this->viewPath . 'create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing CmsModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            Yii::$app->session->setFlash('success', Yii::t('yii2mod.cms', 'Page has been updated.'));
            return $this->redirect(['index']);
        }
        return $this->render($this->viewPath . 'update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing CmsModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        Yii::$app->session->setFlash('success', Yii::t('yii2mod.cms', 'Page has been deleted.'));
        return $this->redirect(['index']);
    }

    /**
     * Finds the CmsModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     *
     * @param integer $id
     *
     * @return CmsModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = CmsModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException(Yii::t('yii2mod.cms', 'The requested page does not exist.'));
        }
    }

}

是错误通过遵循正确的配置步骤解决

由于未执行配置第二步,出现错误

2) 通过以下代码将新规则类添加到应用程序配置中的urlManager数组中:

'components' => [
        'urlManager' => [
            'rules' => [
                ['class' => 'yii2mod\cms\components\PageUrlRule'],
            ]
        ],
    ], 
'components' => [
        'urlManager' => [
            'rules' => [
                ['class' => 'yii2mod\cms\components\PageUrlRule'],
            ]
        ],
    ],
 'components' => [
        'urlManager' => [
            'rules' => [
                ['class' => 'yii2mod\cms\components\PageUrlRule'],
            ]
        ],
    ],
您需要执行的完整配置:

1) 要首先使用此扩展,您需要配置comments扩展,然后必须在应用程序中配置主配置:

'modules' => [
        'admin' => [
            'controllerMap' => [
                'cms' => 'yii2mod\cms\controllers\CmsController'
                // You can set your template files
                // 'layout' => '@app/modules/backend/views/layouts/main',
                // 'viewPath' => '@app/modules/backend/views/cms/',
            ],
        ],
    ],
'modules' => [
        'admin' => [
            'controllerMap' => [
                'cms' => 'yii2mod\cms\controllers\CmsController'
                // You can set your template files
                // 'layout' => '@app/modules/backend/views/layouts/main',
                // 'viewPath' => '@app/modules/backend/views/cms/',
            ],
        ],
    ],

You can then access to management section through the following URL:

http://localhost/path/to/index.php?r=admin/cms/index
然后,您可以通过以下URL访问管理部分:

2) 通过以下代码将新规则类添加到应用程序配置中的urlManager数组中:

'components' => [
        'urlManager' => [
            'rules' => [
                ['class' => 'yii2mod\cms\components\PageUrlRule'],
            ]
        ],
    ], 
'components' => [
        'urlManager' => [
            'rules' => [
                ['class' => 'yii2mod\cms\components\PageUrlRule'],
            ]
        ],
    ],
 'components' => [
        'urlManager' => [
            'rules' => [
                ['class' => 'yii2mod\cms\components\PageUrlRule'],
            ]
        ],
    ],
3) 添加到SiteController(或通过urlManager中的$route param进行配置):


现在,您可以通过管理面板创建自己的页面,并通过每个页面的url访问它们。

您似乎正在使用站点组件中的cms扩展

web.php
文件中,添加以下内容:


“组件”=>[
...
‘i18n’=>[
“翻译”=>[
'*' => [
'class'=>'yii\i18n\PhpMessageSource',
“基本路径”=>“@app/messages”
'sourceLanguage'=>'en',
],
],
],
]
...,
“controllerMap”:=>[
'cms'=>'yii2mod\cms\controllers\CmsController'
],

注意:您应该排除有关在管理模块中将其配置为组件的路径,因为您无论如何都不使用它

如果您是在模块中使用它,那么自述文件中记录的步骤对您来说很好。

我有以下内容,它的工作非常出色

设置实例由于无法找到给定的类而发生错误,这可能是由于未进行配置

遵循配置链接

1) 要首先使用此扩展,您需要配置comments扩展,然后必须在应用程序中配置主配置:

'modules' => [
        'admin' => [
            'controllerMap' => [
                'cms' => 'yii2mod\cms\controllers\CmsController'
                // You can set your template files
                // 'layout' => '@app/modules/backend/views/layouts/main',
                // 'viewPath' => '@app/modules/backend/views/cms/',
            ],
        ],
    ],
'modules' => [
        'admin' => [
            'controllerMap' => [
                'cms' => 'yii2mod\cms\controllers\CmsController'
                // You can set your template files
                // 'layout' => '@app/modules/backend/views/layouts/main',
                // 'viewPath' => '@app/modules/backend/views/cms/',
            ],
        ],
    ],

You can then access to management section through the following URL:

http://localhost/path/to/index.php?r=admin/cms/index
2) 通过以下代码将新规则类添加到应用程序配置中的urlManager数组中:

'components' => [
        'urlManager' => [
            'rules' => [
                ['class' => 'yii2mod\cms\components\PageUrlRule'],
            ]
        ],
    ], 
'components' => [
        'urlManager' => [
            'rules' => [
                ['class' => 'yii2mod\cms\components\PageUrlRule'],
            ]
        ],
    ],
 'components' => [
        'urlManager' => [
            'rules' => [
                ['class' => 'yii2mod\cms\components\PageUrlRule'],
            ]
        ],
    ],
3) 添加到SiteController(或通过urlManager中的$route param进行配置):


显示相关的控制器代码。。please@scaisEdge我已经更新了
setInstance()
yii\base\Module
类中的一个方法。您是否正确设置了配置文件?您是否在某处定义了模块?显示您的错误堆栈跟踪将有所帮助。是的,我已在配置文件@slayvinsame answer中定义了模块,但您已在出现此类错误时告诉我原因。谢谢