ebook img

Laravel Artisan PDF

199 Pages·2016·2.17 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Laravel Artisan

Laravel Artisan An in depth coverage of Laravel 5.3 features Johnathon Koster Thisbookisforsaleathttp://leanpub.com/laravelartisan Thisversionwaspublishedon2016-08-24 ThisisaLeanpubbook.LeanpubempowersauthorsandpublisherswiththeLeanPublishing process.LeanPublishingistheactofpublishinganin-progressebookusinglightweighttools andmanyiterationstogetreaderfeedback,pivotuntilyouhavetherightbookandbuild tractiononceyoudo. ©2015-2016JohnathonKoster Tweet This Book! PleasehelpJohnathonKosterbyspreadingthewordaboutthisbookonTwitter! Thesuggestedhashtagforthisbookis#laravelphp. Findoutwhatotherpeoplearesayingaboutthebookbyclickingonthislinktosearchforthis hashtagonTwitter: https://twitter.com/search?q=#laravelphp Contents AbouttheBook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i Why? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i WhoisThisBookFor? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i HowIsTheBookStructured? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i SymbolsUsedInThisBook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ii IsThisBookDone? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ii WhatWasUsedtoWriteThisBook? . . . . . . . . . . . . . . . . . . . . . . . . . . . iii Laravel: The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.1 WhatYouShouldKnow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.1.1 PHPandObject-OrientedProgramming(OOP)Concepts . . . . . . . . . 4 1.1.2 ALittleLinux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.1.3 Composer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.1.4 NamespacesandAuto-Loading . . . . . . . . . . . . . . . . . . . . . . . 9 1.1.5 Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2. Illuminate\Translation\FileLoader . . . . . . . . . . . . . . . . . . . . . . . . 16 2.1 FileLoaderPublicAPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.1.1 load($locale, $group, $namespace = null) . . . . . . . . . . . . 17 2.1.2 addNamespace($namespace, $hint) . . . . . . . . . . . . . . . . . . 17 2.2 TranslationNamespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3. Hashing:OneWayEncryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.1 Laravel’sBcryptHasher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 3.1.1 setRounds($rounds) . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 3.1.2 make($value, array $options = []) . . . . . . . . . . . . . . . . . 20 3.1.3 check($value, $hashedValue, array $options = []) . . . . . . 21 3.1.4 needsRehash($hashedValue, array $options = []) . . . . . . . . 23 3.2 TheHashFacade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.3 AvailableHashingFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Artisan Console Overview . . . . . . . . . . . . . . . . . . . . . . 27 4. DatabaseManagementCommands . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 CONTENTS 5. CacheManagementCommands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 6. DefaultArtisanCommands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 6.1 Theclear-compiledCommand. . . . . . . . . . . . . . . . . . . . . . . . . 30 6.2 TheenvCommand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 6.3 ThemigrateCommand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 6.4 TheoptimizeCommand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 6.5 TheserveCommand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Writing Artisan Console Commands . . . . . . . . . . . . . 36 7. TheConsoleKernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 7.1 TheApplicationConsoleKernel . . . . . . . . . . . . . . . . . . . . . . . . . 39 7.2 TheFrameworkConsoleKernel . . . . . . . . . . . . . . . . . . . . . . . . . 42 7.2.1 FrameworkKernelPublicAPI . . . . . . . . . . . . . . . . . . . . . . . . 42 8. WritingCommands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 8.1 CommandInputandCommandSignatures . . . . . . . . . . . . . . . . . . . 50 8.1.1 InputParameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 8.1.2 InputParameterDefaultValues . . . . . . . . . . . . . . . . . . . . . . . 51 8.1.3 AddingDescriptionstoCommandParameters . . . . . . . . . . . . . . . 52 8.1.4 CommandOptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 8.1.5 CommandOptionDefaultValues . . . . . . . . . . . . . . . . . . . . . . 53 8.1.6 AddingDescriptionstoCommandOptions . . . . . . . . . . . . . . . . . 54 8.1.7 CommandOptionShortcuts . . . . . . . . . . . . . . . . . . . . . . . . . 54 8.1.8 UsingCommandOptionsasFlags . . . . . . . . . . . . . . . . . . . . . . 55 8.1.9 ArrayParameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 8.1.10 ArrayOptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 8.1.11 MakingInputArgumentsOptional . . . . . . . . . . . . . . . . . . . . . 58 8.1.12 CommandInputSyntaxReference . . . . . . . . . . . . . . . . . . . . . 59 8.1.13 CommandSignatureAlternatives . . . . . . . . . . . . . . . . . . . . . . 59 8.2 RetrievingUserInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 8.2.1 argument($key = null) . . . . . . . . . . . . . . . . . . . . . . . . . 68 8.2.2 hasArgument($name) . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 8.2.3 option($key = null) . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 8.2.4 hasOption($name) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 8.3 DefaultCommandOptions(GlobalCommandOptions) . . . . . . . . . . . . 72 8.3.1 ApplicationVersionandCommandEnvironments . . . . . . . . . . . . . 73 8.3.2 CommandVerbosityLevelsandOutputControl . . . . . . . . . . . . . . 74 8.4 PromptingforUserInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 8.4.1 PromptingforUserInput . . . . . . . . . . . . . . . . . . . . . . . . . . 78 8.4.2 SuggestingInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 8.4.3 PromptingforSensitiveUserInput . . . . . . . . . . . . . . . . . . . . . 84 8.4.4 ProvidingChoicesforUserInput . . . . . . . . . . . . . . . . . . . . . . 85 8.4.5 The--no-interactionDefaultOption . . . . . . . . . . . . . . . . . . 90 CONTENTS 9. CommandOutput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 9.1 GeneralOutput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 9.2 OutputtingLineswithColors . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 9.3 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 9.4 ProgressBars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 9.5 AdditionalOutputMethods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 9.5.1 block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 9.5.2 title . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 9.5.3 section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 9.5.4 listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 9.5.5 writeln . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 9.5.6 write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 9.5.7 newLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 9.5.8 text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 9.5.9 comment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 9.5.10 success . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 9.5.11 errorandwarning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 9.5.12 note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 9.5.13 caution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Helper Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 10. StringHelperFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 10.1 ImmutableStrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 10.2 ascii($value) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 10.3 studly($value) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 10.3.1 studly_case($value) . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 10.4 camel($value) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 10.5 snake($value, $delimiter = '_') . . . . . . . . . . . . . . . . . . . . . 117 10.5.1 snake_case($value, $delimiter = '_') . . . . . . . . . . . . . . . 117 10.6 replaceFirst($search, $replace, $subject) . . . . . . . . . . . . . . 118 10.6.1 str_replace_first($search, $replace, $subject) . . . . . . . . 118 10.7 trans($id = null, $parameters = [], $domain = 'messages', $lo- cale = null) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 10.8 trans_choice($id, $number, array $parameters = [], $domain = 'messages', $locale = null) . . . . . . . . . . . . . . . . . . . . . . . . 119 10.9 e($value) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 11. ArrayHelperFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 11.1 ImmutableArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 11.2 “Dot”Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 11.3 add($array, $key, $value) . . . . . . . . . . . . . . . . . . . . . . . . . 123 11.3.1 array_add($array, $key, $value) . . . . . . . . . . . . . . . . . . 124 11.4 get($array, $key, $default = null) . . . . . . . . . . . . . . . . . . . 124 11.4.1 array_get($array, $key, $default = null) . . . . . . . . . . . . 125 11.5 pull(&$array, $key, $default = null) . . . . . . . . . . . . . . . . . 125 CONTENTS 11.5.1 array_pull(&$array, $key, $default = null) . . . . . . . . . . 126 12. ApplicationandUserFlowHelperFunctions . . . . . . . . . . . . . . . . . . . . . 127 12.1 app($make = null, $parameters = []) . . . . . . . . . . . . . . . . . . 127 12.2 auth($guard = null) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 12.3 policy($class) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 12.4 event($event, $payload = [], $halt = false) . . . . . . . . . . . . 128 12.5 dispatch($command) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 12.6 factory() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 12.7 method_field($method) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 12.8 validator(array $data = [], array $rules = [], array $messages = [], array $customAttributes = []) . . . . . . . . . . . . . . . . . . 131 13. Configuration,Environment,SecurityandLoggingHelperFunctions . . . . . . . 133 13.1 bcrypt($value, $options = []) . . . . . . . . . . . . . . . . . . . . . . 133 13.1.1 bcryptOptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 13.2 encrypt($value) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 13.3 info($message, $context = []) . . . . . . . . . . . . . . . . . . . . . . 135 14. URLGenerationHelperFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 14.1 action($name, $parameters = [], $absolute = true) . . . . . . . . 136 14.1.1 RelativeURLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 15. MiscellaneousHelperFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 15.1 dd() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 15.2 data_get($target, $key, $default = null) . . . . . . . . . . . . . . . 138 15.3 windows_os . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 15.4 tap($value, $callback) . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 16. BasicUsage-CollectionsasArrays . . . . . . . . . . . . . . . . . . . . . . . . . . 146 16.1 NotesonBasicUsage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 16.2 CreatingaNewCollectionInstance . . . . . . . . . . . . . . . . . . . . . . . 148 17. Collections:ThePublicAPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 17.1 all . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 17.2 toArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 17.3 chunk($size, $preserveKeys = false) . . . . . . . . . . . . . . . . . . 152 17.4 only($keys) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 17.5 keyBy($keyBy) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 18. MessageBags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 18.1 Illuminate\Contracts\Support\MessageProviderInterface . . . . . . . 157 18.2 ViewErrorBags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 18.2.1 ViewErrorBagPublicAPI . . . . . . . . . . . . . . . . . . . . . . . . . 159 CONTENTS 19. Fluent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 19.1 FluentPublicAPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 19.1.1 get($key, $default = null) . . . . . . . . . . . . . . . . . . . . . . 169 19.1.2 getAttributes() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 19.1.3 toArray() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 19.1.4 jsonSerialize() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 19.1.5 toJson($options = 0) . . . . . . . . . . . . . . . . . . . . . . . . . . 171 20. Facades . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 20.1 FacadeAliasesandImportingFacades . . . . . . . . . . . . . . . . . . . . . . 173 20.2 UsingFacades . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 20.3 CreatingFacades . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 20.3.1 CreatingaFacadeAlias . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 20.4 FacadeClassReference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 20.4.1 NotesonBlade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 20.4.2 NotesonSchema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 20.4.3 AdditionalCookieMethods . . . . . . . . . . . . . . . . . . . . . . . . . 182 20.4.4 AdditionalInputMethods . . . . . . . . . . . . . . . . . . . . . . . . . 182 20.4.5 AdditionalSchemaMethods . . . . . . . . . . . . . . . . . . . . . . . . . 183 20.5 ResolvingtheClassBehindaFacade . . . . . . . . . . . . . . . . . . . . . . . 183 AppendixA:AvailableHashFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 AppendixB:HTTPStatusCodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 About the Book Thebookstartedasacollectionofpersonalnotesthathavebeengrowingwhileworkingonboth professionaland personal projects using the Laravel PHP framework. These notes mainly focus onthenuancesintheframework’sfeaturesaswellasextensivecoverageofthehelperfunctions, classes and features. Many of these notes can be found in fragmented versions on my personal website(http://www.stillat.com/¹).Thisbookisacohesivecollectionandexpansionofsaidnotes (notallthecontentinthebookisavailableonthewebsite). Why? When this project (the collection of notes, and eventually this book) started back in the Laravel 4.xdays,thedocumentationwasnotasgreatasitistoday(alotofhardworkandefforthasgone into the recent Laravel documentation by many talented and dedicated people). The following collection of notes were created as a sort of supplemental documentation to reinforce ideas and conceptslearnedabouttheframeworkwhiledevelopingprojectsandideas. Who is This Book For? This book is for anyone who uses the Laravel framework to create things, or anyone who is interestedinlearningmoreabouttheframework.AbasicknowledgeofPHPisrecommendedas wellassomeexperiencewithframeworkslikeLaravel.Therearesomesectionsthatdigdeepinto someunderlyingframeworkimplementationdetails;youhavebeenwarned.Herebedragons. How Is The Book Structured? Eachchapterwasdesignedtohopefullystandoutonitsown.Thiswasdonetoreducetheneedto flipbackandforthbetweenpagesrepeatedly.However,thechaptersandsectionsareorganized so that the sections flow in a logical order (for example, set and get method sections usually appearnexttooneanother). The table of contents is also very detailed, often including function and method signatures as part of the listing. This book does not include an index, and structuring the table of contents in thiswayallowsreaderstoquicklyfindamethodorfunctionandthecorrespondingpagenumber tolookup. Thebookalsoincludesappendicesthatprovidesupplementalinformationtothecontentsofthe book. For example, completed appendices (at the time this introduction was written) include a list of available hashing functions available for use with PHP’s crypt function as well as a list ofcommonHTTPstatuscodes. ¹http://www.stillat.com/ i AbouttheBook ii Symbols Used In This Book This icon will be used when defining a term. This icon is not used often, and usually onlyappliedwhentheremaybeconfusionaboutorterm,orwhensufficientambiguity alreadyexistsaroundaterm. This icon is used to present the thoughts or opinions of the author(s) and acts as an invitationforconversation.Thesesectionsalsofeatureatitlestylingthatismoresubtle. Look for this icon in the text to learn about any appendices that may additional referencematerialforagivensection. Thisiconindicatesinformationthatisimportantorthecauseofgeneralerrors. This icon is used to point out or explain the cause of specific errors or problems that mayariseduringdevelopment. Thisiconisusedtopointoutadditionalinformationthatmightbeusefulinthecontext ofagivenchapter,butisnotrequired. Thisiconisusedtoprovideadditionalcontextually-relevantinformationtohelpclarify ideasortopointoutmultiplewaysofdoingsomething. Is This Book Done? In a word, no. The level of detail most of the sections get into can take a very long time to research, test and write. I believe that the book, as it sits, contains a wealth of information as is and should be released. This will allow me to gauge interest in the book and receive feedback. The received interest and feedback will hopefully provide motivation and justification for the continuedworkonthisbook(thesethingstakeamassive amountoftime). The short answer is that, yes, there are plans to cover all the major features of the Laravel framework at later dates. Which features covered will largely depend on reader interest and requests. Each section will attempt to cover each topic completely as well as point out any inconsistencies,nuancesorhiddenfeaturesrelatedtoagivenfeatureset. ThefollowingarethechaptersandsectionsthatIamcurrentlyworkingnext:

Description:
list of available hashing functions available for use with PHP's crypt function . easy with its database abstraction layer as well as its Eloquent ORM.
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.