Yii中

2014-02-20 20 views
-1

我試圖創建警予的管理面板創建管理面板,我也跟着這裏的每一步http://scriptbaker.com/how-to-separate-front-and-admin-panel-in-yii-framework/Yii中

但是當我嘗試和過剩的主網頁我得到這個錯誤。任何想法爲什麼?

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); 
    } 

} 

開發/ 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'); 

和我配置/前面。 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 
    ) 
); 

另外,創建管理頁面的方式與本博客解釋它的方式有什麼相似之處,而不是僅使用單獨的登錄名在模塊文件夾中創建管理頁面?

+1

你有'SiteController'嗎?如果是,它是否有'actionError'方法?路徑'site/error'是在Yii中聲明的默認errorAction。但是從你發佈的鏈接中,你需要對你的配置文件(front.php和back.php)進行一些修改。定義errorHandlers像這樣你的應用程序的兩端:'... '分量'=>數組( \t \t '的ErrorHandler'=>數組( \t \t \t 'errorAction'=> '位點/錯誤', \t \t),),...' –

+0

@ragingprodigy更新了我的問題。我在哪裏放錯誤處理程序?順便說一下..這樣做的好處是什麼?而不是僅僅做一個管理模塊? – user2636556

+0

errorHandler將放置在配置文件的組件部分。其次,我沒有看到以這種方式創建管理模塊的優勢......這只是一個偏好問題(這只是我的觀點) –

回答

1

Yii無法找到我們的控制器/操作。在爲控制器和動作設置名稱時,您必須準確無誤。例如:

class SiteController extends Controller{} 

文件名必須正好爲SiteController。它甚至區分大小寫。正如我所看到的front/siteconteoller.php不是確切的名字。另外需要注意的是,你的控制檯位於front目錄。您必須確保您已將文件導入到此目錄中。你可以導入你的類和文件main.php文件象下面這樣:

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

而且最好是通過創建一個GII爲modulefront並把你的控制器/行動統一到您的模塊。因此,您可以訪問您的管理員,例如:http://example.com/front/login