Symfony (I):添加一个Webpage(Action,Template,Helper)
2008-01-20 18:40
357 查看
AddingaPage
Insymfony,thelogicbehindpagesisstoredintheaction,andthepresentationisintemplates.
Listing4-2-AddinganActionIsLikeAddinganExecuteMethodtotheActionClass
Thenameoftheactionmethodisalways
Now,ifyourequestthefollowingURL:
symfonywillcomplainthatthe
Listing4-5-TheAlternativePHPSyntax,GoodforTemplates
Listing4-4-TheUsualPHPSyntax,GoodforActions,ButBadforTemplates(不推荐)
Listing4-9-ItIsFasterandEasiertoUseHelpersThantoUseHTMLTags
If,intheexampleinListing4-9,youthinkthehelperversionisnotreallyfastertowritethantheHTMLone,considerthisone:
ThisoutputsthefollowingHTML:
Youshouldalwaysusethe
MostHelpersAcceptanOptionArgument
Listing4-13-GettingDatafromtheRequestParameterintheAction
Listing4-14showshowthe
Listing4-14-GettingDatafromtheRequestParameterDirectlyintheTemplate
Note:TheusualPHPvariables(the
Insymfony,thelogicbehindpagesisstoredintheaction,andthepresentationisintemplates.
AddinganAction
The"Hello,world!"pagewillbeaccessiblethroughamyActionaction.Tocreateit,justaddan
executeMyActionmethodtothe
mymoduleActionsclass,asshowninListing4-2.
Listing4-2-AddinganActionIsLikeAddinganExecuteMethodtotheActionClass
<?php
classmymoduleActionsextendssfActions
{
publicfunctionexecuteMyAction()
{
$today=getdate();
$this->hour=$today['hours'];
}
}
Thenameoftheactionmethodisalways
execute``Xxx``(),wherethesecondpartofthenameistheactionnamewiththefirstlettercapitalized.
Now,ifyourequestthefollowingURL:
http://localhost/myapp_dev.php/mymodule/myAction
symfonywillcomplainthatthe
myActionSuccess.phptemplateismissing.That'snormal;insymfony,apageisalwaysmadeofanactionandatemplate.
AddingaTemplate
Theactionexpectsatemplatetorenderitself.Atemplateisafilelocatedinthetemplates/directoryofamodule,namedbytheactionandtheactiontermination.Thedefaultactionterminationisa"success,"sothetemplatefiletobecreatedforthe
myActionactionistobecalled
myActionSuccess.php.
Listing4-5-TheAlternativePHPSyntax,GoodforTemplates
<p>Hello,world!</p>
<?phpif($test):?>
<p><?phpecho time();?></p>
<?phpendif;?>
Listing4-4-TheUsualPHPSyntax,GoodforActions,ButBadforTemplates(不推荐)
<p>Hello,world!</p>
<?php
if($test)
{
echo"<p>". time()."</p>";
}
?>
IfyouneedtoexecutesomePHPcodeinthetemplate,youshouldavoidusingtheusualPHPsyntax,asshowninListing4-4.Instead,Listing4-5.
GatheringInformationfromtheUserwithForms
AhelperisaPHPfunctiondefinedbysymfonythatismeanttobeusedwithintemplates.ItoutputssomeHTMLcodeandisfastertousethanwritingtheactualHTMLcodebyyourself.Usingsymfonyhelpers,youcanhavethesameresultasinListing4-8withthecodeshowninListing4-9.Listing4-9-ItIsFasterandEasiertoUseHelpersThantoUseHTMLTags
<p>Hello,world!</p>
<?phpif($hour>=18):?>
<p>OrshouldIsaygoodevening?It'salready<?phpecho$hour?>.</p>
<?phpendif;?>
<?phpechoform_tag('mymodule/anotherAction')?>
<?phpecholabel_for('name','Whatisyourname?')?>
<?phpechoinput_tag('name')?>
<?phpechosubmit_tag('Ok')?>
</form>
If,intheexampleinListing4-9,youthinkthehelperversionisnotreallyfastertowritethantheHTMLone,considerthisone:
<?php
>$card_list=array(
>'VISA'=>'Visa',
>'MAST'=>'MasterCard',
>'AMEX'=>'AmericanExpress',
>'DISC'=>'Discover');
>echoselect_tag('cc_type',options_for_select($card_list,'AMEX'));
>?>
ThisoutputsthefollowingHTML:
<selectname="cc_type"id="cc_type">
<optionvalue="VISA">Visa</option>
<optionvalue="MAST">MasterCard</option>
<optionvalue="AMEX"selected="selected">AmericanExpress</option>
<optionvalue="DISC">Discover</option>
</select>
Youshouldalwaysusethe
link_to()helpertocreatehyperlinkstoyourapplication'sactions.
<?phpecholink_to('Ineversaymyname','mymodule/anotherAction?name=anonymous')?>
MostHelpersAcceptanOptionArgument
//Optionargumentasanassociativearray
<?phpecholink_to('Ineversaymyname','mymodule/anotherAction?name=anonymous',
array(
'class'=>'special_link',
'confirm'=>'Areyousure?',
'absolute'=>true
))?>
//Optionargumentasastring
<?phpecholink_to('Ineversaymyname','mymodule/anotherAction?name=anonymous',
'class=special_linkconfirm=Areyousure?absolute=true')?>
//Bothcallsoutputthesame
=><aclass="special_link"onclick="returnconfirm('Areyousure?');"
href="http://localhost/myapp_dev.php/mymodule/anotherAction/name/anonymous">
Ineversaymyname</a>
GettingInformationfromtheRequest
Whethertheusersendsinformationviaaform(usuallyinaPOSTrequest)orviatheURL(GETrequest),youcanretrievetherelateddatafromtheactionwiththegetRequestParameter()methodofthe
sfActionsobject.Listing4-13showshow,in
anotherAction,youretrievethevalueofthe
nameparameter.
Listing4-13-GettingDatafromtheRequestParameterintheAction
<?php
classmymoduleActionsextendssfActions
{
...
publicfunctionexecuteAnotherAction()
{
$this->name=$this->getRequestParameter('name');
}
}
Listing4-14showshowthe
anotherActionSuccess.phptemplatewouldretrievethesame
nameparameter.
Listing4-14-GettingDatafromtheRequestParameterDirectlyintheTemplate
<p>Hello,<?phpecho$sf_params->get('name')?>!</p>
Note:TheusualPHPvariables(the$_POST
,$_GET
,or$_REQUEST
)won'tworkanymore
相关文章推荐
- C# PDF Page操作——设置页面切换按钮 C# 添加、读取Word脚注尾注 C#为什么不能像C/C++一样的支持函数只读传参 web 给大家分享一个好玩的东西,也许你那块就用的到
- Web页面添加一个基类 BasePage解决登陆验证
- selinium webdriver ActionChains 动作链的一个方法
- VS使用验证控件出现“WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分
- [10月19日的脚本] 在SharePoint中为一个 Web 应用程序添加允许内联下载的MIME类型
- [10月19日的脚本] 在SharePoint中为一个 Web 应用程序添加允许内联下载的MIME类型
- 在Web.Config文件中使用configSource,避免动态修改web.config导致asp.net重启(另添加一个Config文件用于管理用户数据)
- 添加一个向前导航的动作(Adding an Up Action)
- 如何用约束在scrollView中开启paging并添加多张图片,一个图片一个page
- 怎样给一个Web应用程序添加加密保护?
- [UML工具]分享一个WEB在线画时序图的工具 & StarUml 添加时序等图 &Jude
- 一个webinterface站点同时添加xenapp和xendesktop
- 一个WEB程序,一个是类库程序,WEB程序调用类库,虽然添加了引用但是写using的时候找不到
- (三)spring boot + mybatis +pagehelper 添加Dao工具类
- 【Android Developers Training】 34. 添加一个简单的分享行为(Action)
- WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptRes
- Google Android开发者文档系列-创建有内容分享特性的应用之添加一个简单的共享action
- Web MVC 架构中,一个 C(controller)至少应该提供几个 A(Action)?
- 可以在web根目录下建一个index.action的空文件
- WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptRes