1.部署应用
1.1准备工作
操作系统:Ubuntu
YII框架:YII-1.1.18
YII框架组织结构
framework
|- base 核心组件
|- caching 缓存组件
|- db 数据库组件
|- gii 代码自动生成组件
|- logging 日志组件
|- validators 表单验证组件
|- web 应用组件
1.2创建应用
常用web应用系统
- cms 内容管理系统
- shop 商城系统
- oa 办公系统
- crm 客户关系管理系统
创建CRM应用
cd framework
//创建cms应用
./yiic webapp ../cms
注意权限不足问题,与PHP环境变量问题。
-su: ./yiic: 权限不够
应用组织结构
cms
|- assets
|- css
|- images
|- protected
---- components 组件
---- config 配置
---- controllers 控制器
---- models 模型
---- runtime 运行时文件
---- views 视图
|- themes
访问应用localhost/cms/
1.3配置本地域名访问应用
配置Nginx多域名访问vim /usr/local/nginx/conf/vhost/cms.cn.conf
Server
{
listen 80;
server_name cms.cn
index index.html index.htm index.php default.html default.htm default.php
root /home/wwwroot/yii.cn/cms;
include other.conf;
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js/css)?$
{
expires 12h;
}
location ~ /\;
{
deny all;
}
access_log /home/wwwlog/cms.cn.log;
}
添加本地域名映射vim /etc/hosts
127.0.0.1 cms.cn
重启Ngnix服务器service nginx restart
使用本地域名访问应用cms.cn
YII访问需路由(router)支持,设置路由即可寻找到对应的控制器(controller)和方法(action),默认路由为cms.cn/index.php?r=site/index
2.用户登录模块
创建控制器cms/protected/controllers/UserController.php
<?php
class UserController extends Controller
{
public function actionLogin()
{
$this->renderPartial('login');
}
}
创建视图cms/protected/views/user/login.php
<!DOCTYPE html>
<html>
<head>
<title>用户登录</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content=""/>
<link href="<?=CSS_URL?>/login.css" rel='stylesheet' type='text/css' />
<script type="application/x-javascript">
addEventListener("load", function() {
setTimeout(hideURLbar, 0);
}, false);
function hideURLbar(){
window.scrollTo(0,1);
}
</script>
</head>
<body>
<h1>管理中心</h1>
<div class="app-location">
<h2>欢迎登录</h2>
<div class="line"><span></span></div>
<div class="location"><img src="<?=IMG_URL?>/location.png" class="img-responsive" alt="" /></div>
<form>
<input type="text" class="text" value="" onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = '';}" >
<input type="password" value="" onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = 'Password';}">
<div class="submit"><input type="submit" onClick="myFunction()" value="登录" ></div>
<div class="clear"></div>
<div class="new">
<h3><a href="#">找回密码</a></h3>
<h4><a href="#">注册</a></h4>
<div class="clear"></div>
</div>
</form>
</div>
<div class="copy-right">
<p>© 2017.狮源骏琦 版权所有</p>
</div>
</body>
</html>
创建静态资源文件cms/assets/default/css/
cms/assets/default/js/
cms/assets/default/img/
cms/assets/default/css/login.css
/* reset */
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,dl,dt,dd,ol,nav ul,nav li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline;}
article, aside, details, figcaption, figure,footer, header, hgroup, menu, nav, section {display: block;}
ol,ul{list-style:none;margin:0px;padding:0px;}
blockquote,q{quotes:none;}
blockquote:before,blockquote:after,q:before,q:after{content:'';content:none;}
table{border-collapse:collapse;border-spacing:0;}
/* start editing from here */
a{text-decoration:none;}
.txt-rt{text-align:right;}/* text align right */
.txt-lt{text-align:left;}/* text align left */
.txt-center{text-align:center;}/* text align center */
.float-rt{float:right;}/* float right */
.float-lt{float:left;}/* float left */
.clear{clear:both;}/* clear float */
.pos-relative{position:relative;}/* Position Relative */
.pos-absolute{position:absolute;}/* Position Absolute */
.vertical-base{ vertical-align:baseline;}/* vertical align baseline */
.vertical-top{ vertical-align:top;}/* vertical align top */
nav.vertical ul li{ display:block;}/* vertical menu */
nav.horizontal ul li{ display: inline-block;}/* horizontal menu */
img{max-width:100%;}
/*end reset*/
/****-----start-body----****/
body{
background: #0BE093;
font-family: 'Open Sans', sans-serif;
}
body a,form li,.submit input[type="submit"],.new h3 a,.new h4 a{
transition: 0.1s all;
-webkit-transition: 0.1s all;
-moz-transition: 0.1s all;
-o-transition: 0.1s all;
}
body h1 {
color:#fff;
text-align: center;
padding: 1em 0;
font-size: 2.9em;
}
.app-location h2{
color: #fff;
text-align: center;
padding: 1em 0;
font-size: 1.6em;
font-weight: 300;
}
.app-location{
width:28%;
margin:0 auto;
text-align: center;
background: #323542;
padding:4em;
}
.location {
padding: 1em 0;
}
.line {
position: relative;
}
.line span{
position: absolute;
top: 10%;
left: 39%;
background: #5b5c61;
height: 1px;
width: 20%;
}
form {
padding: 0% 1%;
}
/*-----*/
.location img {
margin: 2em 0;
}
.app-location input[type="text"],.app-location input[type="password"]{
width: 81.2%;
padding: 1.1em 1em 1.1em 4em;
color: #858282;
font-size: 16px;
outline: none;
background: #fff;
font-weight: 500;
border: none;
font-family: 'Open Sans', sans-serif;
border-radius: 0.3em;
-o-border-radius: 0.3em;
-moz-border-radius: 0.3em;
-webkit- border-radius: 0.3em;
margin:0.7em 0;
background: url("../img/icons.png") no-repeat 14px 16px #474a55;
}
.app-location input[type="password"]{
background: url("../img/icons.png") no-repeat 13px -61px #474a55;
}
.submit {
margin: 1em 0;
}
.app-location input[type="submit"]{
font-size: 20px;
font-weight: 300;
color: #fff;
cursor: pointer;
outline: none;
padding: 17px 15px;
width:100%;
border:3px solid #0bd38a;
background: #0bd38a;
border-radius: 0.3em;
-o-border-radius: 0.3em;
-webkit-border-radius: 0.3em;
-moz-border-radius: 0.3em;
}
input[type="submit"]:hover{
background:none;
border: 3px solid #0bd38a;
color: #0bd38a;
}
.new {
margin: 4em 0 1em 0;
}
.new h3{
float:left;
}
.new h3 a,.new h4 a{
color:#78797C;
font-weight: 400;
font-size: 1em;
}
.new h3 a:hover,.new h4 a:hover{
text-decoration: underline;
}
.new h4{
float:right;
}
/*---------------*/
.copy-right {
padding: 3em 1em;
}
.copy-right p {
color: #fff;
font-size: 1em;
font-weight:400;
margin: 0 auto;
text-align: center;
}
.copy-right p a {
color: #323542;
}
.copy-right p a:hover {
text-decoration: underline;
}
/*-----start-responsive-design------*/
@media (max-width:1440px){
.app-location{
width:30%;
}
.app-location input[type="text"],.app-location input[type="password"]{
width: 82.2%;
}
}
@media (max-width:1366px){
.app-location{
width:34%;
}
.app-location input[type="text"], .app-location input[type="password"] {
width: 82.7%;
}
.line span{
position: absolute;
top:10%;
left: 39%;
background: #5b5c61;
height: 1px;
width: 20%;
}
body h1 {
font-size: 2.6em;
}
}
@media (max-width:1280px){
.app-location {
width: 37%;
}
}
@media (max-width:1024px){
.app-location {
width: 47%;
}
}
@media (max-width:768px){
body h1 {
font-size: 2.4em;
}
.location img {
margin: 2em 0;
width: 59%;
}
.app-location h2 {
padding: 0.8em 0;
font-size: 1.6em;
}
.line span {
top: 10%;
left: 39%;
height: 1px;
width: 20%;
}
.app-location {
width: 63%;
}
.copy-right {
padding: 1em 1em;
}
}
@media (max-width:640px){
body h1 {
font-size: 2.1em;
}
.app-location {
width: 69%;
}
.app-location input[type="text"], .app-location input[type="password"] {
width: 81%;
}
}
@media (max-width:480px){
.app-location {
width: 90%;
padding: 2em 1em 1em 1em;
}
.copy-right p {
font-size: 0.9em;
}
.new h3 a, .new h4 a {
font-size: 0.9em;
}
form {
width: 82%;
margin: 0 auto;
}
.app-location input[type="text"], .app-location input[type="password"] {
width: 76.8%;
margin: 0.4em 0;
}
}
@media (max-width:320px){
.app-location {
width: 87%;
padding: 2em 1em 1em 1em;
}
body h1 {
font-size: 1.5em;
}
.copy-right p {
font-size: 0.85em;
line-height: 1.7em;
}
.app-location input[type="text"], .app-location input[type="password"] {
width:68%;
padding: 1em 1em 1em 4em;
font-size: 15px;
background: url("../img/icons.png") no-repeat 14px 17px #474a55;
background-size: 11%;
}
.app-location input[type="password"] {
background: url("../img/icons.png") no-repeat 14px -41px #474a55;
background-size: 11%;
}
.app-location h2 {
padding: 0.5em 0;
font-size: 1.2em;
}
.new h3 a, .new h4 a {
font-size: 0.75em;
}
.new {
margin: 2em 0 1em 0;
}
.line span {
top: 10%;
left: 35%;
height: 1px;
width: 29%;
}
.submit {
margin: 0.5em 0;
}
.app-location input[type="submit"] {
font-size: 18px;
padding: 11px 11px;
}
form {
width: 87%;
margin: 0 auto;
}
}
创建常量配置cms/protected/config/constant.php
<?php
define('SITE_URL','http://cms.cn');
//自定义静态资源常量地址
define('CSS_URL',SITE_URL.'/assets/default/css');
define('JS_URL',SITE_URL.'/assets/default/js');
define('IMG_URL',SITE_URL.'/assets/default/img');
入口文件加载常量配置cms/index.php
<?php
$yii=dirname(__FILE__).'/../framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
defined('YII_DEBUG') or define('YII_DEBUG',true);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
//加载常量配置
require_once(dirname(__FILE__).'/protected/config/constant.php');
require_once($yii);
Yii::createWebApplication($config)->run();
控制器分析
用户自定义控制器->父类控制器->组件目录components->主配置文件main.php->统一入口文件index.php
用户自定义控制器均继承自Controller,父类Controller来自cms/protected/components/Controller.php
。
<?php
class IndexController extends Controller
{
public function actionIndex()
{
$this->renderPartial('index');
}
}
父类Controller
通过cms/protected/config/config/main.php
配置加载。
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
),
而主配置文件main.php
在统一入口文件index.php
加载。
<?php
$yii=dirname(__FILE__).'/../framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
布局模板设置
自定义布局设置位于父类控制器cms/conponents/Controller.php
的$layout
成员属性中。
public $layout='//layouts/column1';
使用render()
来渲染布局
<?php
class IndexController extends Controller
{
public $layout = 'cms';
public function actionIndex()
{
$this->render('index');
}
}
7