在上一片的基础上写前端控制器:
1 controllers目录下创建文件IndexController,写入代码:
class Test_News_IndexController extends Mage_Core_Controller_Front_Action
{ public function indexAction() {echo "hello world!"; } }
此处的代码中:类名是有规则的,可以看出Test>News>IndexController是路径,用下划线连接而已
2 在News>etc>config.xml中注册路由器,加入代码:
<config>···<frontend><routers>
<news><use>standard</use>
<args><module>Test_News</module><frontName>news</frontName></args>
</frontend>···</config>
3 浏览器输入url: 项目路径/news/index/index,如我的是:127.0.0.1/magento/index.php/news/index/index 可以看见页面中输 hello world!
说明:1)注册路由器后,当我们在url中输入news后,就定位到项目模块 Test/News的controllers中,再根据后面的内容定位文件和函数
2)url末尾的index/index,第一个是来自于IndexController,magento的规则如此,一个控制器就是名称后跟上Controller关键字,文件名如此,class名称是;第二个index来自于函数indexAction,Action这里可以看着关键字,也是magneno的规则,index/index就代表着到IndexController文件中执行IndexAction函数,这里的url中的index可以省略,默认就是执行index控制器中的index函数,其他名称不可省略。