tp框架总结(二)
2016-05-18 16:56
441 查看
一、函数库和类库
项目中的常用的函数库要封装到项目Common/function.php中 在项目中可以直接调用 [ 函数();]
import方法是ThinkPHP内建的类库导入方法,提供了方便和灵活的文件导入机制,完全可以替代PH的 require和include方法。
例如:
import("MyApp.Action.UserAction");
impot("Think.Util.Array");
import("ORG.Util.Page");
二、权限把控
class BaseAction extends Action{
public _initialize(){
echo "先进行登录验证";
}
}
class IndexAction extends BaseAction{
public function index(){
//访问前先执行登录验证
$this->display();
}
}
例子::
生成跨项目的url
__ROOT__."/admin.php/Index/index
判断请求类型:
$this->isGet();//是否是get请求
$this->isPost();//是否是post请求
tp中空操作(_empty())
<?php
class CityAction extends Action{
public function _empty($name){
//把所有城市的操作解析到city方法中
$this->city($name);
}
//protected方法
protected function city($name){
echo $name;
}
}
?>
空模块的操作
<?php
class EmptyAction extends Action{
public function index(){
//根据当前模块名来判断要执行那个城市的操作
$cityName = MODULE_NAME;
$this->city($cityName);
}
//注意 city方法 本身是 protected 方法
protected function city($name){
//和$name这个城市相关的处理
echo '当前城市' . $name;
}
}
?>
项目中使用空模块的例子:
ThinkPHP支持伪静态URL设置
'URL_HTML_SUFFIX' => '.shtml'
URL路由
'URL_ROUTER_ON' => true, //开启路由
'URL_ROUTE_RULES' => array( //定义路由规则
'news/:year/:month/:day' => array('News/archive', 'status=1'),
'news/:id' => 'News/read',
'news/read/:id' => '/news/:1',
),
访问localhost/tpshop/index.php/news/10 就会访问Index/show方法
项目中的常用的函数库要封装到项目Common/function.php中 在项目中可以直接调用 [ 函数();]
import方法是ThinkPHP内建的类库导入方法,提供了方便和灵活的文件导入机制,完全可以替代PH的 require和include方法。
例如:
import("MyApp.Action.UserAction");
impot("Think.Util.Array");
import("ORG.Util.Page");
二、权限把控
class BaseAction extends Action{
public _initialize(){
echo "先进行登录验证";
}
}
class IndexAction extends BaseAction{
public function index(){
//访问前先执行登录验证
$this->display();
}
}
例子::
生成跨项目的url
__ROOT__."/admin.php/Index/index
判断请求类型:
$this->isGet();//是否是get请求
$this->isPost();//是否是post请求
tp中空操作(_empty())
<?php
class CityAction extends Action{
public function _empty($name){
//把所有城市的操作解析到city方法中
$this->city($name);
}
//protected方法
protected function city($name){
echo $name;
}
}
?>
空模块的操作
<?php
class EmptyAction extends Action{
public function index(){
//根据当前模块名来判断要执行那个城市的操作
$cityName = MODULE_NAME;
$this->city($cityName);
}
//注意 city方法 本身是 protected 方法
protected function city($name){
//和$name这个城市相关的处理
echo '当前城市' . $name;
}
}
?>
项目中使用空模块的例子:
ThinkPHP支持伪静态URL设置
'URL_HTML_SUFFIX' => '.shtml'
URL路由
'URL_ROUTER_ON' => true, //开启路由
'URL_ROUTE_RULES' => array( //定义路由规则
'news/:year/:month/:day' => array('News/archive', 'status=1'),
'news/:id' => 'News/read',
'news/read/:id' => '/news/:1',
),
访问localhost/tpshop/index.php/news/10 就会访问Index/show方法
相关文章推荐
- PHP下载文件的函数实例代码
- ContentProvider的主要作用
- PHP 数组遍历方法大全(foreach,list,each)
- Windows YII2安装
- PHP魔术方法和魔术常量
- php traits 详解
- PHP JSON数据 AJAX
- tp下载已经存在的xlsx
- aws 上使用EC2 搭建***
- PHP时间格式大全
- PHP数组元素的顺序与逆序排序
- php进阶函数
- php 下载文件的函数
- 最全的php.curl参数详解
- LAMP组合基础知识 WordPress、phpmyadmin安装
- PHP 分页函数
- tp框架总体学习总结(一)
- php设计模式
- php 关闭跨域ajax限制
- ContentProvider和Uri详解