Sie sind auf Seite 1von 17

2/18/2016

ForgotPasswordFormandController|LaravelTips

SettingUpaForgot
PasswordFormand
ControllerinLaravel5.1
Accessalltutorialsinsprocketicon.
June16,2015fromauthorBillKeck.
HowtoSetupForgotPasswordinLaravel5.1
Ok,soinmylasttutorial,HowtoMakeUserLoginandRegistrationLaravel5.1,we
setupabasicuserregistrationandlogin,butwelefttheforgotpasswordfunctionality
outbecausethetutorialwasgoingtoolong.
InLaravel5.1,theyarenotincludingtheviewsyouneedserveforgotpassword
functionoutofthebox.Thecontrollerandtraitsareinplace,however.
Soallweneedtodoismakesurethatwehavetheroutesetandtheproperviews,
anditwillworkperfectly.
TheRoute
Youshouldhavearouteinapp/Http/routes.phpfileasfollows:





IfyoustillhavetheroutetheoldAuthController,youcangoaheadanddeletethe
https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

1/17

2/18/2016

ForgotPasswordFormandController|LaravelTips

Auth\Authcontrollerline,wewillnotbeusingit.
Withthistypeofroute,ifwehaveforexample,agetEmailfunctiononourcontroller,
theroutewillusetheverbgetfromthefunctionnametofigureoutwhatkindof
requestwearelookingfor.
Weabandonedthisapproachfortheloginandregistrationcontrollersbecausewe
wantedasimplerapproach.Butsinceforgotpasswordfunctionalityisalittle
complicated,Imleavingwellenoughalone,especiallysinceIdontanticipatehaving
toworkonitinthefuture.Itdoesntseemlikelytochange,whereasIknowformy
registration,itcouldeasilybedifferentinthefuture,especiallyifIhookupsocial
authtoit.
Ok,sowehaveourrouteinplace,letslookatthecontroller.
TheController
Thepathisapp/Http/Controllers/Auth/PasswordController.php.
Itshouldlooklikethis:

 

 
 










https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

2/17

2/18/2016

ForgotPasswordFormandController|LaravelTips



















Sowherearealltheactions?IntheResetsPasswordstraitofcourse.Frankly,Idont
understandwhyitsdonethisway.Itdoesntseemlikelythatthemethodsinthetrait
wouldbeusedforanythingotherthantheforgotpasswordfeature.
Perhapstheyweretryingtokeepthecontrollerskinny.
TheTrait
Hereswhatthetraitlookslike:

 

 
 
  
  
  
https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

3/17

2/18/2016

ForgotPasswordFormandController|LaravelTips






 









 
 











 


  















https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

4/17

2/18/2016

ForgotPasswordFormandController|LaravelTips







 




 








 
 























https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

5/17

2/18/2016

ForgotPasswordFormandController|LaravelTips

 







 




























OneobviousimprovementwouldbetogetridoftheredirectPathfunctionandsimply
useourRedirectsUserstrait,sincethatisthesolepurposeofthattraitandthe
methodsareidentical.

https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

6/17

2/18/2016

ForgotPasswordFormandController|LaravelTips

HowevertheResetsPasswordstraitlivesinthe
vendor/laravel/illuminate/Foundation/Authdirectory,whichmeanswhenwerun
composerupdate,thefilecanbeoverwritten,whichisnotwhatwewant.
WecouldjustcopythetraitintoourAuthTraitsfolder,giveitanewnameandthen
modifyithowwewant,butisitworththebother?Probablynot.LikeIsaid,wewill
notbemessingwiththiscodemuchinthefuture,soitsprobablybestjusttoleaveit
asis.
Soletstakeabrieflookatthemethodsinthistrait,keepinginmindthatIdidntwrite
them,soIdontknoweverydetail,butwecanstillgetageneralunderstandingof
howitworks.
Thefirstmethodsimplyreturnstheform:






Wecantellfromtheviewmethodthatweareexpectinganauthfolderintheviews
folderandapassword.blade.phpfilewithintheauthfolder.
Whentheusergivesushisemailandsubmits,weposttheform:






https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

7/17

2/18/2016

ForgotPasswordFormandController|LaravelTips






 


  






Thisonegetsfairlycomplicatedwhenwelookunderthehood.Thereisaclass
namedPasswordBrokerthatlivesinvendor/laravel/framework/src/Auth/Passwords/
thathasthesendResetLinkmethod.
Sothesimpleexplanationisthemethodtriestofindauserwiththatemailandsend
themaresetlink.Itswitchesontheresponse,andifitissuccessfulitredirectsback
withstatus.Ifnot,itreturnsanerrormessageforinvaliduser.
Exactlyhowitsallstitchedtogetherwithallthedependencieswouldmakean
excellentLaracastsepisode.Otherwiseitsbeyondthescopeofthistutorial.
Thenextmethodreturnstheemailsubject:






https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

8/17

2/18/2016

ForgotPasswordFormandController|LaravelTips

NextwehavethegetResetmethod,whichreturnstheresetviewwiththetoken.This
willbetheformwheretheytypeinthenewpasswordandconfirmit.




 





Notetheuseofthedefaultofnullforthetoken.Sononeedtosetupatry/catch,
sinceitwillhaveavalueinanycase.Itsanicecleanalternativetoatry/catchblock.
NextwehavepostReset:

















https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

9/17

2/18/2016

ForgotPasswordFormandController|LaravelTips







 




Ok,sowevalidate,setthecredentialsfromtheformpost,usetheresetmethodof
thePasswordBrokerclass,whichisaccessedviathePasswordfacade.
Thendependingontheresponse,eithertakethemtodestinationsetbyredirectPath
orgobacktotheformwitherrors.
Ifyouareinterested,thePasswordfacadeclassisfoundat
vendor/laravel/framework/src/Illuminate/Support/Facades/Password.php.
TheresamethodintherenamegetFacadeAccessor.Itsprobablyworthfollowing
thechainhereforamoment:

 




Thisreturnsthenameofthecomponent.Sonowwecancheckoutthe
PasswordResetServiceProviderclass,whichyoucanfindat:
https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

10/17

2/18/2016

ForgotPasswordFormandController|LaravelTips

vendor/framework/src/Illuminate/Auth/Passwords/PasswordResetServiceProvider.php
Letslookatonemethodthere:





 
















Thisiskindofcomplicated,toomuchforabeginningtutorial.ButthemainthingI
wantedyoutoseeisthatauth.passwordisboundtoPasswordBroker.So,sincethe
Passwordfacadeislinkedtoauth.passwordandauth.passwordisboundto
PasswordBroker,weendupusingthemethodsofPasswordBrokerwhenwecall
Password.
Ifyoureadmyserviceprovidertutorial,whichwassimple,yougetsomesenseof
this,andherewehaveaveryadvancedexample.Ishouldalsosayitsextremely
wellcommented.

https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

11/17

2/18/2016

ForgotPasswordFormandController|LaravelTips

Ifyouareabeginnerandallthisseemslikebeingdroppedinthemiddleofanocean
withoutalifejacketorraft,dontworry,weveallbeenthere.Juststaywithit,
eventually,youwillgetit.
Youdonthavetomemorizeitalltomakeprogress.Eventually,enoughwillstickso
thatyoucanusewhatyouhaveasaknowledgebasethatyoucanbuildon.Thats
howmostofusdoit.
Ok,headingbacktoourResetsPasswordstrait,weneedtolookatthe
resetPasswordmethod:










Thisisthemethodthatsetsandsavesthenewpasswordandlogsintheuser.We
useinourpostResetmethod.
ThefinalmethodisredirectPath,whichwehavealreadycovered.
Youcanseejusthowinvolvedtheprocessofresettingapasswordis,andyoucan
seewhyIdidntwanttobuilditfromscratchormesswithittoomuch.
TheViews
Thelaststeptomakeourforgotpasswordfunctionalityoperationalistocreatesome
views.Letsstartbycreatingthetwofoldersthatwewillneed,whichwillresidein
theviewsfolder.Makeanemailsfolderandanauthfolderinsidetheviewsfolder.
https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

12/17

2/18/2016

ForgotPasswordFormandController|LaravelTips

Letsmakeapassword.blade.phpfileandplaceitintheemailsfolderwiththe
followingcontents:



ThingshadtogeteasiersoonerorlaterYoucanofcourseaddwhatever
messagingyouwanttothisview.Thisistheemailthatwillbesenttotheuserwith
thelinktheyneedtoresettheirpassword.
Next,weneedtocreatereset.blade.phpandplaceitwithintheauthfolderwiththe
followingcontents:



















https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

13/17

2/18/2016

ForgotPasswordFormandController|LaravelTips









































https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

14/17

2/18/2016

ForgotPasswordFormandController|LaravelTips

Thisviewisjusttheformforthepasswordreset,sotheycanenterthenew
password.
Andfinally,weneedtocreateafilenamedpassword.blade.phpintheauthfolder
withthefollowingcontents:
































https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

15/17

2/18/2016

ForgotPasswordFormandController|LaravelTips



















Thisisanotherverysimpleform.Usersentertheiremailanditpoststothe
postEmailmethodontheResetsPasswordstrait.
Andthatisallweneed.Ifyouclickontheforgotpasswordlinkinyourloginform,
assumingyoufollowedmyHowtoMakeUserLoginandRegistrationLaravel5.1,
youwillthenbetakentotheauth.passwordviewtodothelookuponemailaddress
andcausetheemailtobesent.
Thenassumingtheuserreceivestheemail,itwillbeformattedwiththe
emails.passwordview,sotheycanclickthatlinkandgetthemtotheresetview,
wheretheycanenterthenewpassword.
Wedontcoverconfigurationforsendinganactualemailinthistutorial,butifyougo
toapp/config/mail.php,thereisasettingyoucanchangeatthebottomofthefile:

https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

16/17

2/18/2016

ForgotPasswordFormandController|LaravelTips





Whenitssettotrue,itwillsendanemailtoyourapplicationlogfiles,locatedat
app/storage/logs/laravel.log.
Youcantestyourforgotpasswordimplementationtothepointwhereitwillmakean
entryinthelogfile.
Thatsgonnadoitforthistutorial,Ihopeyouenjoyed.Pleasecomment,share,and
likeifyoucan,thanks!
Idonthaveadonatebutton,butIfyouwouldliketosupportmywork,youcandoso
bybuyingoneofmy99books,Ireallyappreciateit.

https://laraveltips.wordpress.com/category/forgotpasswordformandcontroller/

17/17