在 Yii 中创建管理面板 [英] Creating admin panel in Yii
问题描述
我正在尝试在 yii 上创建一个管理面板,我按照这里的每一步 http://scriptbaker.com/how-to-separate-front-and-admin-panel-in-yii-framework/
但是当我尝试超出主页时出现此错误.知道为什么吗?
CHttpException无法解决请求站点/错误".(/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 [内部函数]:CApplication->handleException(Object(CHttpException))#5 {主要}
这是我在 front/siteconteoller.php
中的 sitecontoller.php大批('类' =>'CCaptchaAction','背景颜色' =>0xFFFFFF,),//页面操作呈现存储在protected/views/site/pages"下的静态"页面//它们可以通过以下方式访问: index.php?r=site/page&view=FileName'页面' =>大批('类' =>'CViewAction',),);}/*** 这是调用的默认索引"操作* 当用户未明确请求操作时.*/公共函数 actionIndex() {//渲染视图文件 'protected/views/site/index.php'//使用默认布局 'protected/views/layouts/main.php'$this->render('index');}/*** 这是处理外部异常的动作.*/公共函数 actionError() {if ($error = Yii::app()->errorHandler->error) {if (app()->request->isAjaxRequest)echo $error['message'];别的$this->render('error', $error);}}/*** 显示联系页面*/公共函数 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="发件人:$name <{$model->email}>\r\n".回复:{$model->email}\r\n"."MIME 版本:1.0\r\n".内容类型:文本/纯文本;字符集=UTF-8";邮件(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);Yii::app()->user->setFlash('contact','感谢您联系我们,我们会尽快回复您.');$this->refresh();}}$this->render('contact',array('model'=>$model));}/*** 显示登录页面*/公共函数 actionLogin() {if(app()->user->isGuest()){$model = 新登录表单;//如果是ajax验证请求if (isset($_POST['ajax']) && $_POST['ajax'] === '登录表单') {回声 CActiveForm::validate($model);app()->end();}//收集用户输入数据if (isset($_POST['LoginForm'])) {$model->attributes = $_POST['LoginForm'];//验证用户输入并在有效时重定向到上一页如果 ($model->validate() && $model->login()) {$user = app()->user->getUser();User::model()->updateByPk($user->id, array('last_login' => new CDbExpression('NOW()')));如果 (isset($_POST['ajax'])) {echo app()->用户->getHomeUrl();app()->end();} 别的 {$this->redirect(app()->user->returnUrl);}} 别的 {如果 (isset($_POST['ajax'])) {echo "坏";app()->end();} 别的 {app()->user->setFlash('error', '登录失败,请重试.');}}}//显示登录表单$this->render('login', array('model' => $model));} 别的 {$this->redirect(array('/user/update', 'id' => app()->user->id));}}/*** 注销当前用户并重定向到主页.*/公共函数 actionLogout() {app()->用户->注销();$this->redirect(app()->homeUrl);}}
我的dev/index.php
$dirname = dirname(__FILE__);$hostname = $_SERVER['SERVER_NAME'];$shortcuts = $dirname .'/protected/helpers/shortcuts.php';if ($hostname == 'localhost') {//本地开发$yii = $dirname .'/yii/yii.php';$config = $dirname .'/protected/config/local.php';定义('YII_DEBUG')或定义('YII_DEBUG',真);定义('YII_TRACE_LEVEL')或定义('YII_TRACE_LEVEL',3);}else {//现场$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(要求(目录名(__FILE__).'main.php'),大批('主题' =>'引导','组件'=>数组('urlManager' =>大批('urlFormat' =>'小路','showScriptName' =>错误的,'规则' =>大批('<控制器:\w+>/'=>'<控制器>/视图','<控制器:\w+>/<动作:\w+>/<id:\d+>'=>'<控制器>/<动作>','<控制器:\w+>/<动作:\w+>'=>'<控制器>/<动作>',),),)//将前端设置放在那里));
此外,按照本博客所解释的方式创建管理页面,而不是仅在模块文件夹中创建一个单独登录的管理页面有什么好处?
教程中说你应该在index.php
中包含front.php
配置>
http://scriptbaker.com/how-to-separate-front-and-admin-panel-in-yii-framework/#index
但是您要在 dev/index.php
文件中导入 local.php
或 main.php
配置
请按照此处的说明创建您的 front.php 和 local.php
http://scriptbaker.com/how-to-separate-front-and-admin-panel-in-yii-framework/#config-front
希望这能解决您的问题.干杯!
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?
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!
这篇关于在 Yii 中创建管理面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!