TP父类及模板继承

一、TP父类方法继承

session用法

用登录页面做例子

<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller{
	public function login(){
		//显示页面,实现登录
		if(empty($_POST)){
			$this->show();
		}else{
			//处理登录逻辑
			$uid = $_POST["uid"];
			$pwd = $_POST["pwd"];
			
			$db = D("Users");
			$arr = $db->find($uid);
			if($arr["pwd"]==$pwd&&!empty($pwd)){

				session("uid",$uid);
                                //启动session

				$url = U("Index/index");//不同控制器下的方法要写控制器名
				//不在不同模块下要加上模块名
				$this->success("登录成功!",$url);
			}else{
				$this->error("登陆失败!");//默认跳转到上一个界面
			}
		}
	}
}                            

 在以后做项目中有可能做好多页面都需要加session,为了减少代码量可以做个父类,让后面的方法继承就可以了

新建个父类

<?php
namespace Home\Controller;
use Think\Controller;
class BaseController extends Controller{
	
	public function __construct(){
		parent::__construct();
		if(session("?uid")){
			
		}else{
			$this->redirect('Login/login',array(),0,'');
			exit;
		}
	}
}

 index方法继承

<?php
namespace Home\Controller;
use Home\Controller\BaseController;
class IndexController extends BaseController{
	
    public function index(){
	   echo session("uid");
    }
	
}

 登录后

如果直接打开登陆后的页面,会直接跳转回登录页

二、父类模板继承

新建新的文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
	<block name="tou"></block>			
    </head>
    
    <body>
	<div style="width:100%;height:60px;background-color:green;"></div>
	
	<block name="neirong"></block>
	<div style="width:100%;height:100px;background-color:blue;"></div>
    </body>
</html>

 

<?php
namespace Home\Controller;
use Home\Controller\BaseController;
class IndexController extends BaseController{
	
    public function index(){
	   $this->show();
    }
	
}

 

<extend name="Base:base" />
<block name="tou">
	<style type="text/css">
		#nr{
			width:100%;
			height:200px;
			background-color:yellow;
			}
	</style>
</block>

<block name="neirong">
	<div id="nr" ></div>
</block>

 

<?php
namespace Home\Controller;
use Think\Controller;
class BaseController extends Controller{
	
	public function __construct(){
		parent::__construct();
		if(session("?uid")){
			
		}else{
			$this->redirect('Login/login',array(),0,'');
			exit;
		}
		//父类模板所需要的数据
		$db = D("rules");
		$arr = $db->select();
		$this->assign("menu",$arr);
	}
	
}

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
	<block name="tou"></block>			
    </head>
    
    <body>
	<div style="width:100%;height:60px;background-color:green;">
		<foreach name="menu" item="v">
			<span style="color:#fff;">{$v.name}</span>
		</foreach>
	</div>
	
	<block name="neirong"></block>
	<div style="width:100%;height:100px;background-color:blue;"></div>
    </body>
</html>

 

 

 

posted @ 2018-03-15 12:30  navyyouth  阅读(299)  评论(1编辑  收藏  举报