Laravel - My First Framework Companion for Developers Discovering Laravel PHP Framework
Total Page:16
File Type:pdf, Size:1020Kb
Laravel - my first framework Companion for developers discovering Laravel PHP framework Maksim Surguy This book is for sale at http://leanpub.com/laravel-first-framework This version was published on 2014-09-05 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. ©2014 Maksim Surguy Tweet This Book! Please help Maksim Surguy by spreading the word about this book on Twitter! The suggested hashtag for this book is #laravelfirst. Find out what other people are saying about the book by clicking on this link to search for this hashtag on Twitter: https://twitter.com/search?q=#laravelfirst Also By Maksim Surguy Integrating Front end Components with Web Applications Contents Introduction ................................................. i About the author ............................................. i Prerequisites ................................................ ii Source Code ................................................ ii 1. Meeting Laravel ............................................. 1 1.1 Introducing Laravel 4 PHP framework .............................. 1 1.1.1 Laravel’s Expressive code .................................. 2 1.1.2 Laravel applications use Model-View-Controller pattern ................. 3 1.1.3 Laravel was built by a great community .......................... 3 1.2 History of Laravel framework ................................... 4 1.2.1 State of PHP frameworks world before Laravel 4 ..................... 4 1.2.2 Evolution of Laravel framework .............................. 4 1.3 Advantages of Using Laravel ................................... 5 1.3.1 Convention over configuration ............................... 5 1.3.2 Ready out of the box .................................... 6 1.3.3 Clear organization of all parts of the application ..................... 7 1.3.4 Built-in Authentication ................................... 7 1.3.5 Eloquent ORM – Laravel’s Active Record implementation ................ 9 1.4 Summary .............................................. 10 2. Getting started for Development with Laravel ............................ 12 2.1 Development overview ...................................... 12 2.2 Meeting Composer ......................................... 13 2.3 Installing Laravel .......................................... 13 2.4 Meeting Artisan: Laravel’s command line interface ....................... 14 2.5 Application structure overview .................................. 14 2.5.1 Files and folders in the root level of the application .................... 15 2.5.2 Contents of the “app” folder ................................ 16 2.5.3 Configuration settings ................................... 17 2.6 Tutorial: building a website with Laravel ............................. 17 2.6.1 The big picture – development overview ......................... 18 2.6.2 Creating a new Laravel project ............................... 19 2.6.3 Configuring application settings .............................. 19 2.6.4 Specifying endpoints (routes) of the website ....................... 21 2.6.5 Creating the database structure and the model for the data . 22 CONTENTS 2.6.6 Filling the database with data ............................... 25 2.6.7 Creating HTML and Blade views ............................. 26 2.6.7.1 Building the layout ................................ 27 2.6.7.2 Building the template for the home page .................... 28 2.6.7.3 Building the template for the services page ................... 28 2.6.7.4 Building the template for the contact page ................... 29 2.6.8 Displaying view templates from the routes ........................ 30 2.6.8.1 Connecting the home page route to the view template . 30 2.6.8.2 Connecting the services page route to the view template . 31 2.6.8.3 Connecting the contact page route to the view template . 32 2.6.9 Adding validation to the contact form ........................... 33 2.6.10 Sending HTML email with Laravel ............................ 34 2.7 Summary .............................................. 37 3. Routing .................................................. 38 3.1 Introduction to Routing in Laravel ................................ 38 3.1.1 Structure of a basic route .................................. 39 3.1.2 Types of route requests ................................... 41 3.1.3 Routing example: login form ................................ 42 3.1.3.1 Building routing structure for the login form example . 42 3.1.3.2 Showing HTML form and processing user input . 43 3.2 Passing parameters to routes .................................... 44 3.2.1 Using route parameters ................................... 46 3.2.1.1 Passing more than one parameter ........................ 46 3.2.2 Route constraints ...................................... 46 3.2.3 Making route parameters optional ............................. 47 3.3 Route Filters ............................................ 48 3.3.1 Attaching filters to routes ................................. 49 3.3.2 Creating custom filters ................................... 50 3.3.3 Adding multiple route filters ................................ 52 3.4 Grouping routes .......................................... 52 3.5 Route responses .......................................... 53 3.5.1 Showing output ....................................... 54 3.5.2 Redirects ........................................... 56 3.5.2.1 Redirecting to other URLs inside of the application . 56 3.5.2.2 Redirecting to named routes ........................... 57 3.6 Summary .............................................. 57 4. Views ................................................... 58 4.1 Introducing views and templates ................................. 58 4.1.1 Views - application’s response to a request ........................ 60 4.1.2 Templates in Laravel, Blade ................................ 61 4.2 Creating and organizing views .................................. 62 4.2.0.1 Separating view templates in folders ...................... 63 4.3 Passing data to views ........................................ 64 4.3.1 Using “View::make” arguments to pass data ........................ 66 CONTENTS 4.3.2 Using method “with” .................................... 67 4.4 Blade template engine ....................................... 69 4.4.1 Outputting and escaping data ............................... 70 4.4.1.1 Escaping Data ................................... 71 4.4.2 Using conditional statements and loops .......................... 71 4.4.2.1 Using conditional statements in the views with plain php . 72 4.4.2.2 Using conditional statements in the views with blade . 73 4.4.2.3 Using loops .................................... 74 4.5 Blade layouts ............................................ 76 4.5.1 Creating and using a layout ................................ 77 4.5.1.1 Using layouts from views ............................ 78 4.5.1.2 Using method “@yield” to specify placeholders . 78 4.5.2 Using sections ........................................ 79 4.5.3 Nesting views by using @include ............................. 82 4.6 Summary .............................................. 83 5. Understanding Controllers ....................................... 84 5.1 Introducing controllers, the “C” in MVC ............................. 84 5.2 Default Controllers: BaseController and HomeController .................... 86 5.2.1 Base controller (app/controllers/BaseController.php) ................... 87 5.2.2 Home controller (app/controllers/HomeController.php) . 88 5.3 Creating controllers ........................................ 88 5.3.1 Creating a new controller: LoginController ........................ 89 5.3.2 Creating logic for the LoginController ........................... 90 5.3.3 Defining routing for LoginController ........................... 90 5.4 Using Basic, RESTful and Resource controllers .......................... 94 5.4.1 Creating and using Basic Controllers ........................... 95 5.4.2 Creating and using RESTful Controllers .......................... 96 5.4.3 Creating and using Resource Controllers ......................... 98 5.4.3.1 Creating resource controller with artisan command . 98 5.5 Using controllers with routes ................................... 100 5.5.1 Routing to Basic Controllers ................................ 100 5.5.2 Routing to RESTful Controllers .............................. 102 5.5.3 Routing to Resource Controllers .............................. 104 5.6 Passing route parameters to controllers .............................. 105 5.6.1 Passing parameters to methods of a Basic Controller . 106 5.6.2 Passing parameters to methods of a RESTful Controller . 108 5.6.3 Passing parameters to methods of a Resource Controller . 109 5.7 Using filters with controllers ................................... 111 5.7.1 Attaching filters to a controller .............................. 112 5.7.2 Applying filters to specific methods of a controller . 113 5.7.2.1 Using the “on” keyword ............................. 113 5.7.2.2 Using the “except” and “only” keywords . 113 5.8 Summary .............................................. 114 6. Database operations ........................................... 115 CONTENTS 6.1 Introducing Database Operations in Laravel . 115 6.1.1 Configuring database settings ..............................