Creating admin panel in Yii

2019-06-14 15:33发布

I'm trying to create an admin panel on yii, i followed every step here http://scriptbaker.com/how-to-separate-front-and-admin-panel-in-yii-framework/

but i'm getting this error when i try and excess the main page. any idea why?

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}

here is my sitecontoller.php in front/siteconteoller.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);
    }

}

my 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');

and my 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
    )
);

also, what are the advantages of creating an admin page the way this blog explains it, instead of just creating an admin page in the modules folder with a separate login?

标签: yii
2条回答
够拽才男人
2楼-- · 2019-06-14 15:57

Yii could not find our controller/action. You must be accurate while setting names for controllers and action. example:

class SiteController extends Controller{}

The file name must be exactly SiteController. It is even case-sensitive. As I see front/siteconteoller.php is not the exact name. Another note is that you're controlles is at front directory. You must be sure that you have imported files into this directory or not. You can import your classes and files in main.php file like below:

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

Also it is better to create a module via GII for your front and put your controllers/actions into your module. So you can access your admin in: http://example.com/front/login for example.

查看更多
别忘想泡老子
3楼-- · 2019-06-14 16:03

It is said in tutorial that you should include front.php config in index.php

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

But you are importing local.php or main.php configs in your dev/index.php file

Please create your front.php and local.php as described here

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

I hope this will solve your problem. Cheers!

查看更多
登录 后发表回答