thinkphp使用方法是什么

thinkphp使用方法是什么

一、项目部署

1、虚拟主机部署/本地部署

去掉public/index.php改到 根目录下/index.php。在网站根目录下建立文件index.php,内容如下

<?php // 定义应用目录 define(&#39;APP_PATH&#39;, __DIR__ . &#39;/apps/&#39;); // 加载框架引导文件 require  &#39;./thinkphp/start.php&#39;;

这样基本上就可以了,这是最简单的配置。

立即学习PHP免费学习笔记(深入)”;

2、服务器部署

服务器部署项目文件入口public ,优势:只给外人看到public目录下的文件,与public同级的文件,在外网是隐藏状态的,如:thinkphp、apps、extend、tests、vendor。更简单的意思就是通过域名是访问不到这些文件下内容的,但又不影响框架的使用。

二、创建模块(自动生成模块)

我的项目是部署在本地www/thinkphp 目录下。在做之前,先要考虑清楚,你需要几个模块来完成你的项目。

开始实例

1、创建三个模块 Common(公共模块),Home(前台模块),Admin(后台模块)。公共模块是必不可少的。

修改的情况下为public下的index.php,打开是这样子的

// 定义应用目录 define('APP_PATH', __DIR__ . '/../application/'); // 加载框架引导文件 require __DIR__ . '/../thinkphp/start.php';

在后面加上这两句

$build = include '../build.php'; // 运行自动生成 thinkBuild::run($build);

build.php配置 (自动生成目录) 手册参考:http://www.kancloud.cn/manual/thinkphp5/118021

在项目的根目录有一个build.php文件,打开后看到是这样的:

<?php return [     // 生成应用公共文件     &#39;__file__&#39; => ['common.php', 'config.php', 'database.php'],       // 定义demo模块的自动生成 (按照实际定义的文件名生成)     'demo'     =&gt; [         '__file__'   =&gt; ['common.php'],         '__dir__'    =&gt; ['behavior', 'controller', 'model', 'view'],         'controller' =&gt; ['Index', 'Test', 'UserType'],         'model'      =&gt; ['User', 'UserType'],         'view'       =&gt; ['index/index'],     ],     // 其他更多的模块定义 ];

然后我们给我们需要的文件名在这个地方加上,给的demo可以直接注释掉,如下:

<?php return [     // 生成应用公共文件     &#39;__file__&#39; => ['common.php', 'config.php', 'database.php'],     //公共模块目录     'common' =&gt; [         '__file__'   =&gt; ['common.php'],         '__dir__'    =&gt; ['controller', 'model','lang'],         'controller' =&gt; ['Index'],         'model'      =&gt; ['Base'],     ],     // Index模块     'home'     =&gt; [         '__file__'   =&gt; ['common.php'],         '__dir__'    =&gt; ['behavior', 'controller', 'model', 'view','lang'],         'controller' =&gt; ['Index'],         'model'      =&gt; ['Test'],         'view'       =&gt; ['index/index'],     ],     // Admin 模块     'admin'     =&gt; [         '__file__'   =&gt; ['common.php'],         '__dir__'    =&gt; ['behavior', 'controller', 'model', 'view','lang'],         'controller' =&gt; ['Index'],         'model'      =&gt; ['Test'],         'view'       =&gt; ['index/index'],     ],  ];

1)其中的SITE_PATH,与 RUNTIME_PATH后面都有用到,所有优先放在index.php里面方便后面调用。

2)这两个东西,要放一起使用

$build = include './build.php'; // 运行自动生成 thinkBuild::run($build);

相关推荐:《ThinkPHP教程

三、创建基类

开干之前,得先设置好“基类”,为什么呢?打个比方,要访问会员中心相关的控制器,这类控制器是不是都要有一个“登录限制”,才让访问会员相关的控制器? 基类的作用就出来了。

1、创建三大基类

原始基类

位置:thinkphpappscommoncontrollerbase.php

作用:base模块下的内容,Index模块,和Admin模块都可以调用。

代码:

<?php /**  * 原始基类  * */ namespace appCommoncontroller; use  thinkController; class Base extends Controller{     public function _initialize()     {         parent::_initialize();         echo &#39;原始基类&#39;;     }     public function test1(){         return &#39;test1&#39;;     } }

Index模块基类

位置:thinkphpappscommoncontrollerbase.php

作用:Index模块下的控制器,都要“继承基类”并且“调用基类”。

代码:

<?php /**  * 前端基类  * */ namespace appindexcontroller; use  appCommoncontrollerBase; class IndexBase extends  Base {     public function _initialize()     {         parent::_initialize();     }     public function index()     {             } }

Admin模块基类

位置:thinkphpappscommoncontrollerbase.php

作用:Admin模块下的控制器,都要“继承基类”并且“调用基类”。

代码:

/**  * 后台首页  * */ namespace appAdmincontroller; use appAdmincontrollerAdminBase; class Index extends AdminBase {     public function _initialize()     {         parent::_initialize();     }     public function index()     {         return $this-&gt;fetch();     } }

(User模块基类,如果有会员的话,这个也必须要创建的)

创建基类的主要目的,就是“继承”与“调用”。

四、设置模板路径

默认的模板路径在模块/view文件里面。如果你觉得这样不太方便管理,想要把他设置Template目录下,可以这样做。

模板参数 ,能够影响的它参数,是当前模块下config.php template->view_path参数。

实际操作

1、配置共享参数

在apps/config.php设置一些参数,方便在Index或Admin模块下config.php调用。

apps/config.php,增加一些参数。

'template'               =&gt; [// 模板路径         'view_path'    =&gt; 'template/',     // 就是这里 /**      * 前台文件配置      * Author: MR.zhou      * */     'index' =&gt; [         // 模快名称         'model_name' =&gt;'index',         // 默认模板文件名称         'default_template' =&gt; 'default',       // 这里可以切换模块下的默认模板名称     ],     /**      * 后台文件配置      * Author: MR.zhou      * */     'admin'=&gt;[         // 模快名称         'model_name' =&gt;'admin',         // 默认模板文件名称         'default_template' =&gt;'default',        // 这里可以切换模块下的默认模板名称 ],

2、设置模板参数

index/config.php

'template'=&gt; [     // 模板路径     'view_path'=&gt; config('template.view_path').config('index.model_name').'/'.config('index.default_template').'/', ],

admin/config.php

<?php //配置文件 return [     // 模板配置     &#39;template&#39;               => [         // 模板路径         'view_path'    =&gt; config('template.view_path').config('admin.model_name').'/'.config('index.         default_template').'/',     ], ];

扩展:

1.模板后缀view_suffix,它的影响

http://localhost/thinkphp/index/news/index/id/1212

http://localhost/thinkphp/index/news/index/id/1212.html

五、配置data文件夹

如果你看项目下的各种文件,有种乱七八糟的感觉的话,你就可以进行以下配置。

配置data文件夹的,整理各种文件,让看起来舒服些。

1、设置runtime文件夹

index.php

define('RUNTIME_PATH', __DIR__ . '/data/runtime/');

2、设置upload,存放上传图片、上传文件

3、设置static,存放jquery.js、bootstrap、一些效果插件什么的

// 视图输出字符串内容替换 'view_replace_str'       =&gt; [     '__DATA__' =&gt; SITE_PATH.'data/',     // 上传文件路径     '__UPLOAD__' =&gt;SITE_PATH.'data/upload/',     //  静态文件路径 (如bootshop,js,css)     '__STATIC__' =&gt;SITE_PATH.'data/upload/',     ],

4、定义模板文件路径,方便调用模板下的css、js、images

'view_replace_str'       =&gt; [     // 模板文件路径     '__TEMPLATE__' =&gt; config('template.view_path').config('index.model_name').'/'.config('index.default_template')     .'/',     // 模板下的共享文件路径(css,js,images...)     '__PUBLIC__' =&gt; SITE_PATH.'/'.config('template.view_path').config('index.model_name').'/'.config('index.     default_template').'/public/', ],

模板页引用:

<script> <link href=”__PUBLIC__css/style.css”> @@##@@</script>

5、想放什么都可以,自己设置 

六、公共模块common的使用

common模块属于公共模块,Thinkphp框架,默认就能调用。

实际用处:任何模块都可能用到的模型、控制、事件提取出来放到公共模块下。

1、公共事件 appscommoncommon.php

作用:一般存放密码加密、下拉框封装、读取某文件夹下文件

/**  * 密码加密  * @param string $password  * @param string $password_salt  * @return string  */ function password($password, $password_salt){     return md5(md5($password) . md5($password_salt)); }

2、公共配置 appscommonconfig.php

把Index模块、Admin模块公用的部分提取出来放到这里面,如:公用的模板路径

'template'               =&gt; [     // 模板路径     'view_path'    =&gt; 'template/', ]

3、公共语言包 appscommonlangzh-cn.php

比如经常用到的词 提交成功、提交失败、执行成功、执行错误、添加成功、添加失败、修改成功、修改失败、删除成功、删除失败… 可以放到公共语言包,在Index模块、Admin模块都可以用的到。

<?php /**  * 全局语言包  * zh-cn  * */ return [     &#39;success&#39;          => '执行成功',     'error'            =&gt; '执行失败',     'add_success'      =&gt; '添加成功',     'add_error'        =&gt; '添加失败',     'edit_success'     =&gt; '修改成功',     'edit_error'       =&gt; '修改失败',     'delete_success'   =&gt; '删除成功',     'delete_error'     =&gt; '删除失败', ];

php页面调用:$lang = lang(‘success’)

html页面调用:{:lang(‘success’)}

4、公共控制器 appscommoncommon.php

跟上面差不多个意思 Index模块、Admin模块都能用到的放这里。

5、公共模块 appscommoncommon.php

跟上面差不多个意思 Index模块、Admin模块都能用到的放这里。

七、设置错误页面①

设置网站的错误提示页面,也是一个很重要的环节。

1、空操作

在当前控制器里面增加_empty操作

public function _empty(){     $this-&gt;error('方法不存在'); } Public function index(){        }

测试方法:

正常:

http://localhost/thinkphp/index/index/index

错误: 会提示“方法不存在”

http://localhost/thinkphp/index/index/df

2、空控制器

在模块下建立Error控制器,

位置: index/error.php 相关参数:empty_controller

代码:

<?php /**  * 前端首页  * */ namespace appindexcontroller; use appindexcontroller; class Error extends IndexBase {     public function index(){         echo &#39;访问的控制器不存在&#39;;     } }

测试:http://localhost/thinkphp/index/inde3dfx/index

3、异常错误抛出

能够影响它的是,当前模块下的配置文件。如果当前配置文件无效,则会自动锁定公共模块下的配置参数。

相关参数:exception_tmpl,error_message

// 异常页面的模板文件 'exception_tmpl'=&gt; THINK_PATH . 'tpl' . DS . 'think_exception.tpl',

八、设置错误页面②

完美的去设置错误页面

1、准备一个错误页面 error.html,位置:thinkphptemplateindexdefaulterror.html ,准备把前段所有的错误提示都指向这里。

2、空操作指向

在appsindexcontrollerIndexbase.php,“基类”里面设置_empty。

<?php /**  * 前端基类  * */ namespace appindexcontroller; use  appCommoncontrollerBase; class IndexBase extends  Base {     public function _initialize()     {         parent::_initialize();     }     /**      * 空操作 跳转      * */     public function _empty(){         //abort();              exception();     //  这两种方法都可以     } }

3、空控制器指向

在appsindexcontrollerError.php

<?php /**  * 空控制器跳转  * */ namespace appindexcontroller; use appindexcontroller; class Error extends IndexBase {     public function index(){         abort();     } }

4、异常错误指向 

在 index/config.php exception_tmpl 参数

'exception_tmpl'         =&gt; THINK_PATH . 'tpl' . DS . 'think_exception.tpl',  //'exception_tmpl' =&gt;'E:/wamp/www/thinkphp/template/index/default/error.html',

注意:地址一定要绝对路径。

拓展,

401,404,500等错误页面自定义

相关参数:http_exception_template

手册地址:http://www.kancloud.cn/manual/thinkphp5/163256

代码:

config.php

'http_exception_template'    =&gt;  [         // 定义404错误的重定向页面地址         404 =&gt;  ROOT_PATH.config('template.view_path').config('index.model_name').'/'.config         ('index.default_template').'/404.html',         // 还可以定义其它的HTTP status         401 =&gt;  ROOT_PATH.config('template.view_path').config('index.model_name').'/'.config         ('index.default_template').'/401.html',     ],

控制器调用

abort(404,’错误信息’)

error.html,404.html 页面代码,可以参考thinkphpthinkphptplthink_exception.tpl

九、路由别名Route

主要作用:隐藏自己的真实路由名称

Route.php

方法一:

<?php use thinkRoute; Route::alias(&#39;home&#39;,&#39;index/index&#39;); Route::alias(&#39;admin&#39;,&#39;admin/index&#39;);

方法二:

<?php return [     &#39;__pattern__&#39; => [         'name' =&gt; 'w+',     ],     '[hello]'     =&gt; [         ':id'   =&gt; ['index/hello', ['method' =&gt; 'get'], ['id' =&gt; 'd+']],         ':name' =&gt; ['index/hello', ['method' =&gt; 'post']],     ],     '__alias__' =&gt;  [         'home'  =&gt;  'index/index',        'admin'=&gt; 'admin/index'     ], ];

http://localhost/thinkphp/index.php/home/test 同等与http://localhost/thinkphp/index.php/index/index/test

http://localhost/thinkphp/index.php/admin/edit/ 同等与http://localhost/thinkphp/index.php/admin/index/edit

注释:别名 => ‘模型/控制器’ ( 别名等于模块+控制器)

十、路由设置,隐藏indx.php

网站根目录下.htaccess

<ifmodule> Options +FollowSymlinks -Multiviews RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] </ifmodule>

thinkphp使用方法是什么

以上就是

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享