家居装修知识网

首页 装修资讯 家居知识 家居百科
当前位置 >> 家居知识 hello world例子

hello world例子 2011-04-24

https://m.biud.com.cn 2011年04月24日07:12 家居装修知识网  


  


  关于HDwiki的简朴应用,其实HDwiki运行机制和原理此文中其实已经有了比较详尽的说明,这里本不该赘述,但为了让大家更快速的入门,给出一简朴的“hello,world.”实例。

控制器


  


  一个控制器就是一个类文件,是以一种能够和 URI 关联在一起的方式来命名的。

假设这个 URI:
example.com/index.php?blog 

系统将尝试寻找并装载一个名为 blog.php 的控制器。

当控制器的名字匹配 URI 的第一段时,它将被装载。

让我们试试看: hello,world!

我们来创建一个简朴的控制器,以便更直观地了解其工作原理。运用你的文本编辑器,创建一个名为 blog.php 的文件,然后输入下列代码:

!defined('IN_HDwiki') && exit('Access Denied');

class control extends base{
 
 function control(& $get,& $post){
 $this->base( & $get,& $post);
 }
 
 function dodefault(){
 echo 'hello,world!';
 }
}
?>

然后保存文件到 /control/文件夹。

现在运用类似这样的 URL 访问你的站点:
example.com/index.php?blog 

如果你做的没错,你应当看到 hello,world!

同时,一直确保你的控制器扩展自父控制器类base,以便它能够连续其所有的方法。

方法


  


  
上面的例子中用到的方法名是 dodefault()。如果 URI 的第二部分为空的话,会默认载入 “dodefault” 方法。也可以将地址写成这样来访问 “Hello World”:
example.com/index.php?blog-default

URI 的第二部分是用来决定调用控制器中哪个方法的。

我们再来试试。在你的控制器中加入一个新的方法:

!defined('IN_HDwiki') && exit('Access Denied');

class control extends base{
 
 function control(& $get,& $post){
 $this->base( & $get,& $post);
 }
 
 function dodefault(){
 echo 'hello,world!';
 }
 
 function docomments(){
 echo "This is a comment.";
 }
}
?>

现在在地址栏中输入下面的内容来访问 comments 方法:
example.com/index.php?blog-comments

你应当看到新的信息了:This is a comment.

参数


  


  
将 URI 参数传递给方法

如果你的 URI 超过两个部分,那么超过的将被作为参数传递给方法。

举例来说,如果你的 URI 是这样的:
example.com/index.php?blog-comments-sandals-123

URI 的第3和第4部分会被传递给你的方法,可以用$this->get[2]、$this->get[3]得到

!defined('IN_HDwiki') && exit('Access Denied');

class control extends base{
 
 function control(& $get,& $post){
 $this->base( & $get,& $post);
 }
 
 function dodefault(){
 echo 'hello,world!';
 }
 
 function docomments(){
 echo $this->get[0];
 echo $this->get[1];
 }
}
?>

访问example.com/index.php?blog-comments-sandals-123
此时将显示sandals123



相关知识

hello world例子
Android 开发Hello World!
在Linux下灵活使用expect脚本的小窍门
Java认证:给JavaScript初学者的24小窍门
3款儿童卧室床品 打造奇妙世界
hello kitty 凯蒂猫的个人档案
2011年2月派意馆顶级专业系列讲座回顾
诺贝尔和箭牌为何上不了"500最具价值品牌榜"
探秘亚洲首家Hello Kitty主题西餐厅
青岛新东方雅思口语个性化答题法

本文来自 家居装修知识网 hello world例子 https://m.biud.com.cn/zhidao-view-id-9691.html