您的位置:首页 > 其它

Symfony (I):添加一个Webpage(Action,Template,Helper)

2008-01-20 18:40 357 查看
AddingaPage
Insymfony,thelogicbehindpagesisstoredintheaction,andthepresentationisintemplates.

AddinganAction

The"Hello,world!"pagewillbeaccessiblethrougha
myAction
action.Tocreateit,justaddan
executeMyAction
methodtothe
mymoduleActions
class,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.php
templateismissing.That'snormal;insymfony,apageisalwaysmadeofanactionandatemplate.

AddingaTemplate

Theactionexpectsatemplatetorenderitself.Atemplateisafilelocatedinthe
templates/
directoryofamodule,namedbytheactionandtheactiontermination.Thedefaultactionterminationisa"success,"sothetemplatefiletobecreatedforthe
myAction
actionistobecalled
myActionSuccess.php
.

Listing4-5-TheAlternativePHPSyntax,GoodforTemplates
<p>Hello,world!</p>

<?phpif($test):?>

<p><?phpechotime();?></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),youcanretrievetherelateddatafromtheactionwiththe
getRequestParameter()
methodofthe
sfActions
object.Listing4-13showshow,in
anotherAction
,youretrievethevalueofthe
name
parameter.

Listing4-13-GettingDatafromtheRequestParameterintheAction
<?php


classmymoduleActionsextendssfActions

{

...


publicfunctionexecuteAnotherAction()

{

$this->name=$this->getRequestParameter('name');

}

}

Listing4-14showshowthe
anotherActionSuccess.php
templatewouldretrievethesame
name
parameter.

Listing4-14-GettingDatafromtheRequestParameterDirectlyintheTemplate
<p>Hello,<?phpecho$sf_params->get('name')?>!</p>

Note:TheusualPHPvariables(the
$_POST
,
$_GET
,or
$_REQUEST
)won'tworkanymore

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐