🔙 Quay lại trang tải sách pdf ebook Laravel 5 Cookbook Enhance Your Amazing Applications
Ebooks
Nhóm Zalo
Laravel 5 Cookbook Enhance Your Amazing Applications Nathan Wu © 2015 - 2016 Nathan Wu Contents Book Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Book Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 What You Will Get . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Book Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Book Status, Changelog and Contributors . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Current Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Laravel 5 Cookbook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Chapter 1: Back End Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 List Of Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Recipe 1 - Introducing CLI (Command Line Interface) . . . . . . . . . . . . . . . . . . . . 8 Recipe 2 - All About Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Recipe 3 - Build A Laravel Starter App . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Recipe 4 - Create A User Authentication System with Facebook and Socialite . . . . . . . 31 Recipe 5 - Create A User Authentication System Using Laravel Auth Scaffold . . . . . . . 42 Recipe 6 - Image Upload In Laravel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Recipe 7 - Seeding Your App Using Faker . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Recipe 8 - Pagination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Recipe 9 - Testing Your App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Recipe 10 - Writing APIs with Laravel . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Chapter 2: Front End Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 List Of Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Recipe 201 - Notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 CONTENTS Recipe 202 - Integrating Buttons With Built-in Loading Indicators . . . . . . . . . . . . . 116 Recipe 203 - Create A Registration Page Using AJAX and jQuery . . . . . . . . . . . . . 123 Recipe 204 - Create A Login Page Using AJAX And jQuery . . . . . . . . . . . . . . . . . 139 Recipe 205 - Upload Files Using AJAX And jQuery . . . . . . . . . . . . . . . . . . . . . 148 Recipe 206 - Cropping Images Using jQuery . . . . . . . . . . . . . . . . . . . . . . . . . 167 Chapter 3: Deployment Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 List Of Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Recipe 301 - Deploying your applications using DigitalOcean (PHP 7 and Nginx) . . . . . 185 Recipe 302 - Deploying your applications using Heroku . . . . . . . . . . . . . . . . . . . 200 Recipe 303 - Deploying your applications blazingly fast using GIT . . . . . . . . . . . . . 208 Book Information 1 Book Description If you’re looking for a book that can help you to build amazing web applications, this is the book for you! Aimed at people who have some experience with Laravel, this cookbook has your back! There are many proven code rich recipes for working with Laravel. Each recipe includes practical advice, tips and tricks for working with jQuery, AJAX, JSON, API, data persistence, complex application structure, modular PHP, testing, deployment and more. Think about this book as a collection of all premium Laravel tutorials or the successor to the popular Learning Laravel 5 book. Laravel 5 Cookbook also includes tested code that you can download and reuse in your own applications. You’ll save time, learn more about Laravel and other related technologies in the process. We also have a forum for discussion and debate. You can freely ask any questions, provide your valuable feedback and help others. It’s time to discover Laravel more! Requirements The projects in this book are intended to help people who have grasped the basics of PHP and HTML to move forward, developing more complex projects, using Laravel advanced techniques. The fundamentals of the PHP and Laravel are not covered, you will need to: • Read Learning Laravel 5 book. (optional) • Have a basic knowledge of PHP, HTML, CSS and Laravel. • Love Laravel. What You Will Get • Lifetime access to the online book. (Premium Only) • Digital books: PDF, MOBI, EPUB (Premium Only) • Full source code (Premium Only) • Access new chapters of the book while it’s being written (Premium Only) • A community of 20000+ students learning together. • Amazing bundles and freebies to help you become a successful developer. • iPhone, iPad and Android Accessibility. 2 Book Description 3 Book Structure Note: This is a draft version. This book is still under active development, that means some chapters and its content may change. The book also may have some errors and bugs. For any feedback, please send us an email. Thank you. Chapter 1 - Back End Recipes Building APIs and large applications using modern technologies can be a daunting task. In this chapter, you’ll learn best practices and modern techniques for back-end development, starting with an introduction to the command line and Git. These complete, easy-to-use recipes show you how to use cookies, sessions, web storage and some popular Laravel packages. You’ll also learn about writing APIs and debugging techniques. In addition to mastering the technologies, you’ll understand when they’re needed and how to use them. Chapter 2 - Front End Recipes Whether you are a beginner or intermediate web developer, if you wish to make good interactive web applications, then this chapter is for you. In this chapter, you’ll be getting some recipes about front-end web technologies and popular front end tools. These recipes cover best practices and modern techniques for front-end development such as: integrating Twitter Bootstrap, AJAX loading, notifications, cropping images, file uploads and many more. By the end, you should have a better understanding of how to work with AJAX, Jquery, front end frameworks and responsive design. You can apply these techniques to build beautiful applications and add that interactivity to any site you work on. Chapter 3 - Deployment Recipes After learning some tricky topics to successfully build a full stack application, it’s time to deploy your app. This chapter contains some helpful recipes about working with Heroku, Digital Ocean, etc. Deploy your applications blazingly fast using GIT and secret techniques are also discussed in the book! Book Description 4 Feedback Feedback from our readers is always welcome. Let us know what you liked or may have disliked. Simply send an email to
[email protected]
. We’re always here. Translation We’re also looking for translators who can help to translate our book to other languages. Feel free to contact us at
[email protected]
. Here is a list of our current translators: List of Translators¹ Book Status, Changelog and Contributors You can always check the book status, changelog and view the list of contributors at: Book Status² Changelog³ Contributors⁴ ¹http://learninglaravel.net/books/laravelcookbook/cookbook-translators ²http://learninglaravel.net/books/laravelcookbook/cookbook-status ³http://learninglaravel.net/books/laravelcookbook/cookbook-changelog ⁴http://learninglaravel.net/books/laravelcookbook/cookbook-contributors Changelog Current Version Latest version the book: • Version: 0.20 • Status: Complete (Beta Version) • Updated: May 15th, 2016 5 Laravel 5 Cookbook 6 Chapter 1: Back End Recipes Introduction Building APIs and large applications using modern technologies can be a daunting task. In this chapter, you’ll learn best practices and modern techniques for back-end development, starting with an introduction to the command line and Git. These complete, easy-to-use recipes show you how to use cookies, sessions, web storage and some popular Laravel packages. You’ll also learn about writing APIs and debugging techniques. In addition to mastering the technologies, you’ll understand when they’re needed and how to use them. Project Files All project files of this book can be downloaded at: https://github.com/LearningLaravel/cookbook/releases⁵ At the end of each recipe, you can find the recipe’s project files (Tag). Feel free to use each of them at any stage of your development process. List Of Recipes Note: As this is a cookbook, you may skip any recipe that you know already. The book is still under active development, that means some chapters and its recipes may change. The book also may have some errors and bugs. For any feedback, please send us an email. Backend recipes • Recipe 1 - Introducing CLI (Command Line Interface) • Recipe 2 - All About Git • Recipe 3 - Build A Laravel Starter App • Recipe 4 - Create A User Authentication System with Facebook and Socialite ⁵https://github.com/LearningLaravel/cookbook/releases 7 Chapter 1: Back End Recipes 8 • Recipe 5 - Create A User Authentication System Using Laravel Auth Scaffold • Recipe 6 - Image Upload In Laravel • Recipe 7 - Seeding Your App Using Faker • Recipe 8 - Pagination • Recipe 9 - Testing Your App • Recipe 10 - Writing APIs with Laravel • (More recipes will be added later) Recipe 1 - Introducing CLI (Command Line Interface) Laravel 5 Cookbook contains many recipes to create interactive web applications. These recipes are premium tutorials for web developers of all skill levels. For most of the recipes in this book, you will need to use Git to install sample code and Homestead to execute your code. If you don’t have Homestead installed yet, you can follow these instructions to install it: http://learninglaravel.net/laravel5/installing-laravel Working with Laravel and GIT requires a lot of interactions with the CLI, thus you will need to know how to use it. What will we learn? This recipe shows you how to use the command line on PC and Mac. CLI for MAC OSX Luckily, on Mac, you can find a good CLI called Terminal at /Applications/Utilities. Most of what you do in the Terminal is enter specific text strings, then press Return to execute them. Alternatively, you can use iTerms 2⁶. ⁶https://www.iterm2.com Chapter 1: Back End Recipes 9 Iterm inteface CLI for Windows Unfortunately, the default CLI for Windows (cmd.exe) is not good, you may need another one. The most popular one called Git Bash. You can download and install it here: http://msysgit.github.io⁷ Most of what you do in Git Bash is enter specific text strings, then press Enter to execute them. CLI for Linux On Linux, the CLI is called Terminal or Konsole. If you know how to install and use Linux, I guess you’ve known how to use the CLI already. ⁷http://msysgit.github.io Chapter 1: Back End Recipes 10 Recipe 2 - All About Git There’s a chance you may already know about Git! Today, most programmers prefer Git over other distributed version control systems. What will we learn? This recipe introduces Git and provides a list of some important Git commands to get you going with Git. What is Version Control? Version Control System (VCS) let you store different versions of your projects and all its files. You can roll back to an earlier version or take a look at an older snapshot to see which files have been changed. Here is a nice infographic about Version Controler System: Iterm inteface Chapter 1: Back End Recipes 11 Why do you need to use Git? Git is becoming an ‘industry standard’. If you want to become a better developer, you may need to use Git to develop software and collaborate with other developers. Git lets you manage code development in a virtually endless variety of way. Here are benefits of using Version Control System/Git: • Git allows you to create as many branches of your project as you want. You can use each branch to test, create a new feature, fix bugs, etc. • You can see what was changed in your project’s files. This helps you understand what happened and improve your code. • You can easily store all the versions of your site and restore previous versions whenever you want. • Store your files on cloud-based Git repository host services like Github and Bitbucket. • You can easily share your files with others. • A VCS or Git helps your team work more efficiently. Everyone knows what is going on and can merge the changes into a common version. How to install Git? Note: if you don’t know how to run a command, please read the Recipe 1 - Introducing CLI (Command Line Interface). Install Git on Mac The easiest way is to install the Xcode Command Line Tools. You can do this by simply running this command: 1 xcode-select --install Click Install to download Command Line Tools package. Alternatively, you can also find the OSX Git installer at this website: http://git-scm.com/download/mac⁸ Install Git on Windows You can download GitHub for Windows to install Git: https://windows.github.com⁹ Install Git on Linux/Unix You can install Git by running this command: ⁸http://git-scm.com/download/mac ⁹https://windows.github.com Chapter 1: Back End Recipes 12 1 sudo yum install git If you’re on a Debian-based distribution, use this: 1 sudo apt-get install git For more information and other methods, you can see this guide: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git¹⁰ Configuring Git When you first install Git, you should set your name, email address and enable coloring to pretty up command outputs. Open your CLI and run these commands: 1 git config --global user.name "Your Name" 2 git config --global user.email "Your Email Address" 3 git config --global color.ui auto Note: Remember to replace Your Name and Your Email Address. Start versioning your project using Git Git is very simple to use. First, you need to go to your working directory: 1 cd Code/Laravel Note: If you’re using Homestead, the Code directory is where we will put our Laravel apps. Code/Laravel is your working directory. You can use Git on Homestead or on your local machine, it’s up to you. Now we can use the git init command to initialize Git: 1 git init This command creates an empty Git repository. If you’re using Homestead, the path of the Git directory is: ¹⁰https://git-scm.com/book/en/v2/Getting-Started-Installing-Git Chapter 1: Back End Recipes 13 1 /home/vagrant/Code/Laravel/.git/ “.git” is a hidden folder and it doesn’t contain your project’s files yet. Add and commit your files Now we can use git status command to check the status of our working directory: 1 git status You will see a list of untracked files, that means Git doesn’t monitor those files yet. Untracked files To tell Git that you want to include all these files, use the git add -A command: 1 git add -A Note: Alternatively, you can use git add –a or git add –all or git add . command. Chapter 1: Back End Recipes 14 When we run the git status command again, you’ll see: Add files The git add command tells git to add changes in your project to the staging area. However, those changes aren’t saved yet until you run git commit: 1 git commit -m "First commit" You can use the -m flag (stands for message) to give a comment on the command line. My message is “First commit”, but you can use whatever you like. Well done! You’ve made your first commit! Chapter 1: Back End Recipes 15 Store your files on Git repository host services You can store all your files on cloud-based Git repository host services and access them anywhere, anytime. The two most popular services are Github¹¹ and Bitbucket¹². We’ll use Github in this book, but feel free to use what you like. Let’s register an account on Github if you don’t have one yet. After that, click the New repository button to create a new repository. New repository This repository contains all your project’s files. ¹¹http://github.com ¹²http://bitbucket.org Chapter 1: Back End Recipes 16 Creata a new repository When creating a new repository, you can choose any name that you like. Choose Private if you don’t want anyone access your files. Note: Don’t worry too much about the settings, you can change those settings later. Click Create repository to confirm. Great! You now have a new Github repository! Push your project to Github You should see Github’s quick setup guide: Chapter 1: Back End Recipes 17 Creata a new repository Your new repository (repo) is empty. You need to upload your files to that Github repo. Every repository has a unique remote URL, your remote URL should look like this: https://github.com/YourGithubUsername/YourRepoName.git Take note of this link. Good! We will try to upload our Laravel app (/Code/Laravel) to Github. Navigate to the working directory (on your Homestead or local machine): 1 cd /Code/Laravel Add a new remote by using git remote add command: 1 git remote add origin https://github.com/LearningLaravel/Laravel.git origin is the remote name, https://github.com/LearningLaravel/Laravel.git is the remote URL. Note: Your remote URL should be different. Be sure to use your remote URL. After adding a new remote, we can push our files to Github using the git push command: 1 git push -u origin master If it asks for a password, enter your Github password. origin is the remote that we’ve just added. master is your working directory. When we use the -u (stands for upstream) flag, we add the upstream reference. If we successfully push our files to the repo, Git will remember it, so we don’t have to type origin master next time. That means, if we want to upload our files again, we can just type: Chapter 1: Back End Recipes 18 1 git push You now have your files on the cloud! Cloning a repository To download any repo, you can use the git clone command. First, navigate to the location where you want to place the cloned directory: 1 cd Code Type git clone and the unique remote URL to clone the repo: 1 git clone https://github.com/YourGithubUsername/YourRepoName.git This command creates a local clone of the repository on your computer. Note: you can clone any public repository. If you don’t want anyone to download your repo, set it private. Recipe 2 Wrap-up In this recipe, you learned some major Git commands. Throughout this book, we’ll use Git to download the source code, front end components and deploy our Laravel applications. We won’t talk about it anymore because this is a Laravel book, not a Git book. If you wish to learn more about Git, check these sites out: Atlassian Git Tutorials¹³ Git-Tower¹⁴ Codeschool - Try Git¹⁵ Super Useful Need To Know Git Commands¹⁶ It’s time to start learning about Laravel! ¹³https://git-scm.com/book/en/v2/Getting-Started-Installing-Git ¹⁴https://www.git-tower.com/learn/git/ebook ¹⁵https://www.codeschool.com/courses/try-git ¹⁶http://zackperdue.com/tutorials/super-useful-need-to-know-git-commands Chapter 1: Back End Recipes 19 Recipe 3 - Build A Laravel Starter App What will we learn? We will build a simple app and use it as a template for our next recipes. Throughout this process, we will learn how to run multiple Laravel sites on Homestead and how to integrate Twitter Bootstrap into our apps. Installing Laravel Let’s start by installing Laravel! Note: Please note that I’m using Homestead. If you don’t use Homestead, the process could be different. First, SSH into our Homestead: 1 vagrant ssh Then navigate to our Code directory. 1 cd Code Be sure to have the latest version of Laravel Installer by running this command: 1 Composer global require "laravel/installer" New repository Now let’s create a new cookbook app: Chapter 1: Back End Recipes 20 1 laravel new cookbook Great! You should have a new Laravel app! Feel free to change the name of the app to your liking. Note: If you’re not familiar with this process, please read the Learning Laravel 5 book. Create multiple Laravel apps on Homestead You can’t access your new app because Homestead doesn’t know about it yet. Therefore, let’s follow these steps to activate your site: Note: Be sure to backup your current projects’ files and databases. First, we have to go to the Homestead directory: 1 cd ~/.homestead And edit the Homestead.yaml file: 1 vim Homestead.yaml We use VIM to edit the file. If you don’t know how to use VI or VIM, you can open it with your favorite editor by using this command: 1 open Homestead.yaml Find: 1 sites: 2 - map: homestead.app 3 to: /home/vagrant/Code/Laravel/public Just a quick reminder, this section allows us to map a domain to a folder on our VM. For example, we can map homestead.app to the public folder of our Laravel project, and then we can easily access our Laravel app via this address: “http://homestead.app”. Our new app is called cookbook, and I would like to access it via this address:“http://cookbook.app”. So, let’s add the following code: Chapter 1: Back End Recipes 21 1 - map: cookbook.app 2 to: /home/vagrant/Code/cookbook/public Save the file. Tip: if you’re using VIM, press ESC and then write :wq (write quit) to save and exit Remember that, when we add any domain, we must edit the hosts file on our local machine to redirect requests to our Homestead environment. On Linux or Mac, you can find the hosts file at etc/hosts or /private/etc/hosts. You can edit the hosts file using this command: 1 sudo vim /private/etc/hosts On Windows, you can find the hosts file at C:WindowsSystem32\drivers\etc\hosts. After opening the file, we need to add this line at the end of the file: 1 192.168.10.10 cookbook.app Note: All sites will be accessible by HTTP via port 8000 and HTTPS via port 44300 by default. To let the system know that we’ve edited the hosts file, run this command: 1 source /private/etc/hosts Finally, SSH into our Homestead (by using vagrant ssh or homestead ssh), and use the serve command to activate our new site: 1 serve cookbook.app /home/vagrant/Code/cookbook/public/ Good job! If everything is working correctly, we should see our app’s home page: Chapter 1: Back End Recipes 22 New home page Creating Our Home Page Note: If you don’t understand any step in this section, be sure to check out the Learning Laravel 5 book. If you don’t want to follow along, you may skip these steps and download the sample app at the end of this recipe. We will create a new home page for our app. First, let’s create a home view (views/home.blade.php) for our homepage: Chapter 1: Back End Recipes 23 1
Home Page
4 5
7 8 40 41 42
Chapter 1: Back End Recipes 24 43
44
Home Page
45
Our Home page!
46
47
48 49