Heim  >  Artikel  >  php教程  >  ThinkPHP写第一个模块应用

ThinkPHP写第一个模块应用

WBOY
WBOYOriginal
2016-06-06 20:40:03953Durchsuche

前面,我们创建了第一个ThinkPHP项目,下面我们来创建第一个模块应用

找到项目文件夹下面的Lib/Action这个目录,在下面有个创建好的例子IndexAction.class.php,加入我们创建的是admin这个项目,那么./admin/Lib/Action/IndexAction.class.php,这个模块是默认加载的模块。在ThinkPHP中,自动加载的动作、方法、操作等等都是以index命名的。
下面,我们创建一个自己的模块,比如UserAction,class.php(注意命名规则),我们编辑这个文件:
代码如下:
//先继承Action这个类,注意:文件名要与类名保持一致
class UserAction extends Action
{
//每个模块中默认加载的动作(操作、方法)是index方法
function index ()
{
echo '你来到了user模块';
}
//方法(操作、动作)命名规则是:第一个单词小写紧跟着的首字母大写
function listName()
{
echo '你的名字是'.$_GET['name'];
}
}
?>

接下来在浏览器测试:
输入:http://thinkphp.com/admin.php?m=user,输出:你来到了user模块
输入:http://thinkphp.com/admin.php?m=user&a=index,输出:你来到了user模块
输入:http://thinkphp.com/admin.php?m=user&a=listname,输出:你的名字是
输入:http://thinkphp.com/admin.php?m=user&a=listname&name=123,输出:你的名字是123
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn