Joomla组件2(Joomla Component2)
1.简单的输出内容
让我们接着Joomla组件1,继续来扩充我们的Joomla Helloworld组件。
打开helloworld.php文件,输入一下内容:
<?php
// no direct access
defined(‘_JEXEC’) or die(‘Access to this file is prohibited’);
echo ‘Hello World!’;
?>
再来连接一下 http://localhost/joomla/index.php?option=com_helloworld,这次我们可以看到页面上出现Hello world。如何我们终于比前面进了一大步。
同时第一行的代码也防止非法用户直接放我门的Helloworld.php文件,此时浏览器中只会显示Access to this file is prohibited。你可以连接http://localhost/joomla/components/com_helloworld/helloworld.php来测试一下效果。:)
如果为了Joomla 1.0 或者Mambo撰写扩展,请使用_VALID_MOS代替 _JEXEC来判断用户是否直接访问。
2.接收Request的Get参数
接收Get参数,可以使用JRequest::getVar办法,函数经常使用两种格式:
- $task = JRequest::getVar(“task”);
这个例子用来获取URL task参数的值,如果无task的参数,函数会返回null。
- $name = JRequest::getVar(“name” , “Joomla test”);
这个例子用来获取URL name参数的值,如果无name的参数,函数和上个例子中不同,它会使用Joomla test作为默认值来代替null作为函数的返回值。
<?php
// no direct access
defined(‘_JEXEC’) or die(‘Access to this file is prohibited’);
// load the HTML View class
require_once (JApplicationHelper :: getPath(‘front_html’, com_helloworld’));
$task = JRequest::getVar(‘task’);
$name = JRequest::getVar(‘name’, ‘Joomla! 1.5.0′);
switch ($task) {
case ‘show’:
default:
hello_HTML::show($name);
break;
}
?>