在Yii中创建管理面板

时间:2014-02-20 10:30:22

标签: yii

我正在尝试在yii上创建一个管理面板,我按照这里的每一步http://scriptbaker.com/how-to-separate-front-and-admin-panel-in-yii-framework/

但是当我尝试超出主页面时,我收到此错误。知道为什么吗?

CHttpException

Unable to resolve the request "site/error". (/Applications/XAMPP/xamppfiles/htdocs/dev/yii/web/CWebApplication.php:286)

#0 /Applications/XAMPP/xamppfiles/htdocs/dev/yii/base/CErrorHandler.php(331): CWebApplication->runController('site/error')
#1 /Applications/XAMPP/xamppfiles/htdocs/dev/yii/base/CErrorHandler.php(204): CErrorHandler->render('error', Array)
#2 /Applications/XAMPP/xamppfiles/htdocs/dev/yii/base/CErrorHandler.php(129): CErrorHandler->handleException(Object(CHttpException))
#3 /Applications/XAMPP/xamppfiles/htdocs/dev/yii/base/CApplication.php(732): CErrorHandler->handle(Object(CExceptionEvent))
#4 [internal function]: CApplication->handleException(Object(CHttpException))
#5 {main}

这是我在前面/ siteconteoller.php中的sitecontoller.php

<?php

class SiteController extends Controller {

    /**
     * Declares class-based actions.
     */
    public function actions() {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    /**
     * This is the default 'index' action that is invoked
     * when an action is not explicitly requested by users.
     */
    public function actionIndex() {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->render('index');
    }

    /**
     * This is the action to handle external exceptions.
     */
    public function actionError() {
        if ($error = Yii::app()->errorHandler->error) {
            if (app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

    /**
     * Displays the contact page
     */
    public function actionContact()
    {
        $model=new ContactForm;
        if(isset($_POST['ContactForm']))
        {
            $model->attributes=$_POST['ContactForm'];
            if($model->validate())
            {
                $name='=?UTF-8?B?'.base64_encode($model->name).'?=';
                $subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
                $headers="From: $name <{$model->email}>\r\n".
                    "Reply-To: {$model->email}\r\n".
                    "MIME-Version: 1.0\r\n".
                    "Content-Type: text/plain; charset=UTF-8";

                mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
                Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
                $this->refresh();
            }
        }
        $this->render('contact',array('model'=>$model));
    }

    /**
     * Displays the login page
     */
    public function actionLogin() {
        if(app()->user->isGuest()){
            $model = new LoginForm;

            // if it is ajax validation request
            if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
                echo CActiveForm::validate($model);
                app()->end();
            }

            // collect user input data
            if (isset($_POST['LoginForm'])) {
                $model->attributes = $_POST['LoginForm'];
                // validate user input and redirect to the previous page if valid
                if ($model->validate() && $model->login()) {
                    $user = app()->user->getUser();
                    User::model()->updateByPk($user->id, array('last_login' => new CDbExpression('NOW()')));
                    if (isset($_POST['ajax'])) {
                        echo app()->user->getHomeUrl();
                        app()->end();
                    } else {
                        $this->redirect(app()->user->returnUrl);
                    }
                } else {
                    if (isset($_POST['ajax'])) {
                        echo "bad";
                        app()->end();
                    } else {
                        app()->user->setFlash('error', 'Login failed. Please try again.');
                    }
                }
            }
            // display the login form
            $this->render('login', array('model' => $model));
        } else {
            $this->redirect(array('/user/update', 'id' => app()->user->id));
        }
    }

    /**
     * Logs out the current user and redirect to homepage.
     */
    public function actionLogout() {
        app()->user->logout();
        $this->redirect(app()->homeUrl);
    }

}

我的 dev / index.php

$dirname = dirname(__FILE__);
$hostname = $_SERVER['SERVER_NAME'];
$shortcuts = $dirname . '/protected/helpers/shortcuts.php';

if ($hostname == 'localhost') { //local development
    $yii = $dirname . '/yii/yii.php';
    $config = $dirname . '/protected/config/local.php';

    defined('YII_DEBUG') or define('YII_DEBUG', true);
    defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 3);
} 
else { //live site
    $yii = $dirname . '/yii/yii.php';
    $config = $dirname . '/protected/config/main.php';
}

require_once($yii);
require_once($shortcuts);

Yii::createWebApplication($config)->run();

Yii::createWebApplication($config)->runEnd('front');

和我的 config / front.php

return CMap::mergeArray(
    require(dirname(__FILE__).'main.php'),
    array(
        'theme' => 'bootstrap',
        'components'=>array(
            'urlManager' => array(
            'urlFormat' => 'path',
            'showScriptName' => false,
            'rules' => array(
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            ),
        ),
        )
        // Put front-end settings there
    )
);

另外,创建管理页面的好处与此博客解释的方式有什么关系,而不是仅使用单独的登录名创建模块文件夹中的管理页面?

2 个答案:

答案 0 :(得分:2)

教程中说,您应该在front.php

中加入index.php配置

http://scriptbaker.com/how-to-separate-front-and-admin-panel-in-yii-framework/#index

但您要在local.php文件中导入main.phpdev/index.php个配置

请按照此处的说明创建front.php和local.php

http://scriptbaker.com/how-to-separate-front-and-admin-panel-in-yii-framework/#config-front

我希望这能解决你的问题。干杯!

答案 1 :(得分:1)

Yii找不到我们的控制器/动作。在为控制器和操作设置名称时,您必须准确无误。例如:

class SiteController extends Controller{}

文件名必须正好SiteController。它甚至区分大小写。我认为front/siteconteoller.php不是确切的名称。另一个注意事项是你的控件位于front目录。您必须确保已将文件导入此目录。您可以在main.php文件中导入类和文件,如下所示:

'import' => array(
    'application.models.*',
    'application.components.*',
    'application.front.*'
),

最好通过GII为module创建front并将控制器/操作放入模块中。因此,您可以访问管理员:http://example.com/front/login,例如。