日期:2014-05-17  浏览次数:20440 次

ThinkPHP学习笔记(二)入口文件的作用、URL控制、模板的简单使用方式

admin.php

<?php
ini_set("session.save_handler", "files");
//ThinkPHP核心框架文件路径
//第二个意义:防跳墙,防止用户直接访问敏感文件
//做一个页面A,包含一个敏感文件B,用户的访问必须通过A页面才能访问(define方法)
define("THINK_PATH", "./ThinkPHP/");
//应用路径(项目路径)
define("APP_PATH", "./admin/");
//项目名称
//1.让ThinkPHP加载时进行很好的区分
//2.能够让在做权限管理时RBAC,能很好的分开
define("APP_NAME", "admin");
//设置临时文件夹
//define("RUNTIME_PATH", "Tmp");
//如果提示runtime.php中有错误
//可以先保留空白和注释
//define("STRIP_RUNTIME_SPACE", false);
//开发过程中让ThinkPHP不缓存相关的加载项目
//define("NO_CACHE_RUNTIME", true);

//require、require_once、include、include_once
//设置入口文件
require THINK_PATH.'ThinkPHP.php';
App::run();


//app.php这个文件,通常包含以下文件:项目配置文件(默认配置、自定义配置),
//app.php 必须要返回一个array数组
?>

IndexAction.php

<?php
// 本类由系统自动生成,仅供测试用途
/**
 * thinkphp一共有四种路径访问模式,可以在conf文件夹中修改
 *	0: 普通模式:http://localhost/MyThinkPHP/admin.php?m=index&a=hello
 *	1:pathInfo:
 *		http://localhost/MyThinkPHP/admin.php/index/hello
 *		http://localhost/MyThinkPHP/admin.php/index/hello
 *	2:rewrite(伪静态):自己可以写相关的rewrite规则,也可以使用系统提供的rewrite规则
 *		http://localhost/MyThinkPHP/index/hello
 *		Apache收藏的URL指南中有介绍
 *	3:兼容模式:路径访问(防止服务器不支持pathInfo模式)
 *		http://localhost/MyThinkPHP/admin.php?s=/index/hello
 *	
 *	如何在MVC中来显示模板
 *		
 */
class IndexAction extends Action{
	//thinkphp当中模块控制器,动作,与URL路径之间的关系
	//我们的 所以动作,都必须经过项目入口文件,只有这样才能找到模块(控制器)
	//http://localhost/MyThinkPHP/admin.php/index/index
	//访问indexAction中的index方法(默认访问indexAction)
    public function index(){
        header("Content-Type:text/html; charset=utf-8");
        echo "<div style='font-weight:normal;color:blue;float:left;width:345px;text-align:center;border:1px solid silver;background:#E8EFFF;padding:8px;font-size:14px;font-family:Tahoma'>^_^ Hello,欢迎使用<span style='font-weight:bold;color:red'>ThinkPHP</span></div>";
    }
    
	//访问模式:path_Info
	//http://localhost/MyThinkPHP/admin.php?m=index&a=hello
    //http://localhost/MyThinkPHP/admin.php/index/hello
	public function hello(){
        header("Content-Type:text/html; charset=utf-8");
        echo "这是我自己的方法哦!";
    }
    /**
     * 模板使用
     * 大小写一定要注意
     */
	public function template(){
		//必须在对应的项目的Tpl的default下创建与当前控制器名相同的文件夹Index
		//然后html文件名为方法名
		
		//默认分割符方式{$content}
		//可以在模板指南针中进行定界符的修改
		$this->assign("content","小和尚下山去化斋,老和尚有交代");
		$this->display();
		//1.同级下面指定新模板
		//$this->display("newTemplate");
		//2.跨控制器的引用模板(也可以添加分组信息)
		//$this->display("分组:User:verify");
		//3.跨皮肤引用
//		$this->display("test@User:verify");
		//4.全路径输出(在项目同级目录新建public文件夹)
		//加载文件的位置全以主入口文件来定位
//		$this->display("./public/ss.html");
		//display参数
//		$this->display("位置","utf-8","text/xml");
    }
}
?>

conf/config.php

<?php
return array(

//更换模式最好删除一些~app.php和~runtime.php

//'配置项'=>'配置值'
//因为开启URL重新不论是被重写的还是没被重写的,都可以通过原有路径访问
//如果想开启rewrite模式,需要做如下操作
//1.query服务器已经开启了Apache的rewrite模块
//	LoadModule rewrite_module modules/mod_rewrite.so
//2.在与主入口文件,统计目录下,新建一个.htaccess(vi:save .htaccess;记事本:".htaccess")
//如果选用模式2(rewrite)会加大服务器的消耗
'URL_MODEL'=>1,
	

'URL_PATNINFO_MODEL'=>2,
//pathinfo包含两类
	//1普通模式:加上m和a:顺序关系可以发生变化
	//http://localhost/MyThinkPHP/admin.php/m/index/a/index
	//传值
	//http://localhost/MyThinkPHP/admin.php/m/index/a/index/username/zhangsan/password/password
	//2智能识别模块操作(默认模式就是智能识别)
	//http://localhost/MyThinkPHP/admin.php/index/index
	//传值
	//http://localhost/MyThinkPHP/admin.php/index/index/username/zhangsan/password/password
		

//修改URL分隔符
//'URL_PATHINFO_DEPR'=>'-',

//修改模板左右定界符
'TMPL_L_DELIM'=>'<!--{',
'TMPL_R_DELIM'=>'}-->',

//开启调试模式
//1.模拟linux系统来识别大小写
//2.方法名的大小写与