Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Laravel 5.x Cookbook
Laravel 5.x Cookbook

Laravel 5.x Cookbook: A recipe-based book to help you efficiently create amazing PHP-based applications with Laravel 5.x

eBook
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Laravel 5.x Cookbook

Chapter 1. Setting Up and Installing Laravel

In this chapter, we will cover the following topics:

  • Setting up Homestead
  • Setting up composer and PHP on your local machine for faster Workflows
  • Using .env for your local build
  • Using sequel pro and connecting to local and remote databases
  • Setting up your first application in Homestead
  • Setting up Gulp and Elixir

Introduction

In this chapter, we will cover installing and setting up Laravel and Homestead. As I have often said in this book, the online Laravel docs are great, and I will refer to them as needed. This book should be regarded as a complement to the official documentation, expanding on the explanations found there and adding some tips and tricks for everyday use. Also, I will show a number of shortcuts to help speed up your workflow. Finally, I will touch on Gulp and Elixir.

Setting up Homestead

This section will work off the existing Laravel docs to make sure your Homestead is set up correctly, as well as give you some background as to what is going on.

Getting ready

You will need a terminal application, access to the Git command, and decent Internet. As for the terminal on Mac, I suggest iTerm, available at https://www.iterm2.com/; it really is a nice tool for something you are going to use quite often. For Windows, git for Windows https://git-for-windows.github.io/ got me going quickly both for git and a Bash such as terminal. Linux has a nice terminal to begin with, and installing git is easy. As far as Vagrant and VirtualBox are concerned, I will link you to the related sites since they do a good job at explaining how to install each of them on your system.

How to do it...

The following are the steps to set up Homestead:

  1. First, install VirtualBox as noted on their site at https://www.virtualbox.org/wiki/Downloads.
  2. Once this is in place, install Vagrant from https://www.vagrantup.com/.

    At this point, Vagrant will be ready to use at the command line:

    How to do it...

    You will not really need to open VirtualBox.

    Tip

    For Windows users, this link helped me a lot to get started with Homestead: http://blog.teamtreehouse.com/laravel-homestead-on-windows.

  3. Make a folder in your home directory called Code with a capital C.

    On a Mac, this will look like—/Users/alfrednutile/Code.

  4. From here, the online docs do a great job of getting you going on the final Homestead installation and setup—https://laravel.com/docs/5.2/homestead.

How it works...

When done, you will have the Homestead.yml configuration information in the ~/.homestead folder to modify as needed. On Mac, this would be /Users/alfrednutile/.homestead/Homestead.yml.

Tip

Laravel Docs talk about shortcuts in the Daily Usage section at https://laravel.com/docs/5.2/homestead#daily-usage.

Also, you should have made a new folder called ~/Code to be the base folder for all your projects. For example, my folder looks like this:

  • /Users/alfrednutile/Code/app1
  • /Users/alfrednutile/Code/app2

You can have multiple applications within your code folder. In this example, app1 is the root folder for the app1 application, and app2 is the root folder for the app2 code. Keep in mind that the Vagrant box will later on mount this Code folder into the /home/vagrant/Code folder inside the Vagrant client.

Tip

I do my migration and PHPUnit work inside Homestead using the ssh shortcut that the online documents show you. But I do a lot of work outside Homestead inside the Code/app1 directory, such as all the Git commands and much of composer. This creates a much faster workflow for the file intense commands.

There's more...

You can, of course, manually set up your own machine for Nginx, PHP, MySQL, and all the rest, but there are a lot of reasons why the preceding one is best. On a team or alone, having your environment contained like this makes upgrading your machine, going from desktop to laptop, pushing code to production, having up-to-date libraries for new apps and older libraries for legacy apps, and more so much easier.

Setting composer and PHP on your local machine for faster Workflows

In this section, we will cover some tips on using PHP and composer outside of the Homestead box to help with your workflow.

Getting ready

As with the preceding sections, you will need to have a terminal and decent internet. I will cover this using a Mac, but Windows and Linux have their systems to install the software. By default, you can install Xcode on a Mac and get pretty far with PHP, but it tends to be an older version of PHP. Here, we will use Homebrew to install PHP 5.6. We will also use Homebrew later on in this book as well.

How to do it...

  1. Visit the http://brew.sh/ site, and run the command they show there to install Homebrew on your Mac.
  2. Follow the instructions at https://github.com/Homebrew/homebrew-php to get the PHP5.6 setup.
  3. After you are done with step 2, add to your ~/.bash_profile so that we can use this version of PHP:
    export PATH="$(brew --prefix php56)/bin:$PATH"
    
  4. Then, update your current session:
    >source ~/.bash_profile
    
  5. Then, we will make sure our PHP is set up properly:
    >which php
    

    You will see the/usr/local/opt/php56/bin/php output and type:

    >php –v 
    

    This will show that you are running 5.6.19 or a higher version.

  6. Set up mycrypt as follows:
    >brew install php56-mcrypt
    
  7. Then, we will install composer as seen at https://getcomposer.org/download/:
    >php -r "readfile('https://getcomposer.org/installer');" > composer-setup.php
    >php -r "if (hash('SHA384', file_get_contents('composer-setup.php')) === 
    
    625196afcca95a5abf44391188c695c6c1456e16154c75a211d238cc3bc5cb47') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
    >php composer-setup.php
    >php -r "unlink('composer-setup.php');"
    >sudo mv composer.phar /usr/local/composer
    

Tip

What is ~/? That is shorthand for your Home directory. When I use this, for example, >source ~/.bash_profile, your operating system will know it is in your home folder, for example, /Users/alfrednutile/.bash_profile.

How it works...

That was a lot of steps! Let's cover what we did and why. We began by installing Homebrew to make installing packages easier. We will periodically need to install packages such as Wget, Webdriver, and more as we progress through this book. Using the brew command supplied by Homebrew makes installing these packages a snap.

Then, we used Brew to make sure we have a current version of PHP on our Mac. But considering we already have Homestead, why do this? There is some work you do outside of Homestead, for example, getting and installing Laravel using composer, running envoy, and more. And some of these you can run in Homestead, but you will see some speed difference outside of it. So, you still need it on your machine, but in this case, we are not so worried about it being the wrong version for one of our many applications.

The mcrypt part of the installation took care of the extension that we need to run common commands such as php artisan key:generate and other commands in Laravel.

We finalized the PHP setup with Bash shortcuts, so when we open the terminal, we are ready to use PHP and not the version that comes with Xcode on Mac.

We then use PHP to download and install composer, the biggest advancement in PHP since I started 15 years ago in my opinion, and you will see more of composer shortly.

Finally, we are ready to download Laravel!

There's more...

You could, of course, use Brew to install MySQL and more. But for now, we are going to leave all of this inside the Homestead box that we set up earlier.

Setting up your first application in Homestead

In this section, we will download Laravel and set up our local site to use for the rest of our recipes taking advantage of Homestead.

Getting ready

We have Homestead installed. My home folder called ~/Code is where we will be working.

How to do it...

  1. Type the following in the terminal:
    >cd ~/Code
    
  2. Then, download Laravel to a new folder:
    >composer create-project --prefer-dist laravel/laravel recipes 
    
  3. Move into the directory for your new application:
    >cd recipes
    
  4. Now we need to tell Homestead about our new application:
    >cd ~/.homestead && subl Homestead.yml
    
  5. Once the editor pops open, you can add your new site:
    How to do it...
  6. Under database:
    How to do it...
  7. Click on Save and close the editor.
  8. Then, start up Homestead or just provision it:
    >homestead provision
    

    You may be asked for a password, which is your system admin password and not the Homestead password.

  9. Then, we need to edit our system Host file (in this case, our local computer and not Vagrant and Homestead); this will again ask for our system password:
    >sudo subl /etc/hosts
    

    Tip

    I will show a shortcut command in the How it works… section.

  10. Next, edit the host file to set your recipes.dev domain right next to the default Homestead IP of 192.168.10.10; then, save and close the editor:
    How to do it...
  11. Then, visit your site at https://recipes.dev! You may get an SSL warning but click on Advanced and Proceed:
How to do it...

How it works...

The composer command that we ran gets Laravel from its database of applications and libraries at https://packagist.org/ then download it. We began by installing Homebrew to make installing packages easier. We will periodically need to install packages such as Wget, Webdriver, and more as we progress through this book. Using the brew command supplied by Homebrew makes installing these packages a snap.

I also used some shortcuts. One shortcut was subl, which was what you get when you install http://www.sublimetext.com/. But you can use whatever editor you want.

Using some of the preceding tips will make a shortcut called hedit adding to my ~/.bash_profile:

alias homesteadedit='cd ~/.homestead && subl Homestead.yml'

So, we are editing the main file that Laravel uses for all its Homestead settings. You will be here a lot, so shortcuts really pay off.

The same is applicable with the hostedit command that I used:

alias hostedit='sudo subl /etc/hosts'

Here, we are adding to our .bash_profile a quick way to edit the file and add the needed domain recipes.dev and save and exit. Now, when you visit https://recipes.dev, your operating system will know that it is really for Homestead.

See also

Using .env for your local build

This was one of the best changes to Laravel from version 4 and 5 in my opinion. When I was doing Ruby on Rails work, it also had this feature, and this is the key to help create an application that falls in the Twelve Factor App workflow. You will learn how to use this file for setting keys to some recipes later on in the book. In this example, we will start using it to set up our database.

For the rest of this book, I will use PHPStorm for my editor, which helped me a ton to explore Laravel and PHP code when I first started. Make sure your editor has plugins to easily click and explore classes.

Getting ready

When you installed Laravel, it copied the .env file into place. So, just open your editor of choice and open the application directory.

How to do it...

  1. Open .env in your editor.
  2. Alter the file as follows, so the database name and the URL match what we would put in our Homestead setup file:
    How to do it...

How it works...

First of all, this is a hidden file. The . in front of it makes it hard to see in File Managers and even the command line. When at the command line, ls -a * is how to show this hidden file. Most code editors or IDEs will show you these.

Also note that Laravel comes with a .gitignore file that includes this file:

How it works...

We will have to consider the addition or changes of any settings in env, as we push this application to Production for everyone to see when we are done. I will cover this more in Chapter 10, Deploying Your App.

So, what did we change in this file? Most of what you see was already there; we just set two things:

  • DB_DATABASE=recipes_local
  • APP_URL=https://recipes.dev

This is it, really! If you look back, this is what we set in the Homestead.yml file. You can see what we called the database and domain name. So now we need to tell our application what database table to use and which Homestead is made for us. Yeah, for Homestead!

See also

Using sequel pro and connecting to local and remote databases

Soon, we will be doing migrations, saving data to the database, and other day-to-day workflows, but sometimes, it is nice to look into the database. For example, you may want to export Production and the environment that has your live data down to local to review some bug. This section will show how to use Sequel Pro to do secure, over SSH, connections to your database. This allows you to get to your database with almost zero risk other than SSH.

How to do it...

The following are the steps to connect sequel pro to local and remote databases:

  1. Download and install Sequel Pro from http://www.sequelpro.com/.
  2. Add a new connection to Homestead:
    How to do it...
  3. Add new connection to a remote Host:
    How to do it...

How it works...

In the past, I have used phpMyAdmin, and it was better than just the command line. But Sequel Pro really was a game changer. For one, I did not have to install phpMyAdmin on my servers and risk issues related to security. Second, it is a good interface and makes it really easy to check out data, tables, do queries, and so on when needed.

So, what you saw previously was simply a setup for Homestead using the Standard tab and Port 33060, which is what Homestead forwards its MySQL port to.

When we deploy our first server, it will have SSH port 22 open, but never will I have MySQL open only on 127.0.0.1.

Tip

For the most part, you only want three ports open on your server: 22 for SSH, 80 to redirect web requests to SSL/HTTPS, and 443 to serve your website.

So, to connect to this we select the SSH tab, and enter the information for the database on the server, since we will be on the server after the SSH step. Then, we enter the information for SSH; in this case, I had to go into my home folder to use my SSH public key. If you did not set up a key on your server, then most likely, you are using a password, so enter that instead.

Tip

If you do not have the a.ssh folder in your Home directory (~/), then take a moment to create it. From the command line, run ssh-keygen –t rsa and just answer yes to all the questions. Do not add a password. You now have a public key.

It is really this simple. Now, you have this great UI to look into your database once in a while; though after using Laravel with php artisan migrate and eloquent, I am not in the database often.

See also

Setting up Gulp and Elixir

Elixir is new in Laravel 5.x and is a wrapper around Gulp, which is a well-known JavaScript build system. We will use it to manage assets later on in the book. For now, we just want to get it installed.

How to do it...

  1. SSH into Homestead:
    >homestead ssh
    >cd ~/Code/recipes
  2. Install Gulp using the following:
    >npm install
  3. When it is done, try the following:
    > node_modules/gulp/bin/gulp.js -v

How it works...

Once again, Homestead is making our work easier; NPM is already installed! So, we can use this to not only install Gulp but also other JavaScript libraries that we need. How to do this? If you look into the Laravel application that we downloaded, you will now see the =package.json file:

How it works...

This is the file that NPM uses, just as PHP uses composer.json, to know what to install. Here, it is getting Elixir, Gulp, and Sass for us. Elixir is new in Laravel 5.x and is a wrapper around Gulp, which is a well-known JavaScript build system.

Notice too that if you type which gulp, it is installed globally, thanks to Homestead again!

Elixir will come in later, but for now, note gulpfile.js that is right above the package.json file; this is where we will set up our asset workflow later on.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Leverage the amazing new features of Laravel 5.x to create cutting-edge responsive PHP applications.
  • Create apps with interoperability features and extend these features to your existing applications as well.
  • Over 60 recipes that combine tried and tested Laravel tips for getting your app working.

Description

Laravel is a prominent member of a new generation of web frameworks. It is one of the most popular PHP frameworks and is also free and an open source. Laravel 5 is a substantial upgrade with a lot of new toys, at the same time retaining the features that made Laravel wildly successful. It comes with plenty of architectural as well as design-based changes. The book is a blend of numerous recipes that will give you all the necessary tips you need to build an application. It starts with basic installation and configuration tasks and will get you up-and-running in no time. You will learn to create and customize your PHP app and tweak and re-design your existing apps for better performance. You will learn to implement practical recipes to utilize Laravel’s modular structure, the latest method injection, route caching, and interfacing techniques to create responsive modern-day PHP apps that stand on their own against other apps. Efficient testing and deploying techniques will make you more confident with your Laravel skills as you move ahead with this book. Towards the end of the book, you will understand a number of add-ons and new features essential to finalize your application to make it ready for subscriptions. You will be empowered to get your application out to the world.

Who is this book for?

The ideal target audience for this book is PHP developers who have some basic PHP programming knowledge. No previous experience with Laravel is required for this book.

What you will learn

  • Optimize Your Gulp and Elixir Workflow
  • Use Travis to run tests with every push
  • Build and test your view-based route in PHPUnit
  • Explore workflows for migrations and seeding
  • Implement Angular in your Laravel applications
  • Set up a user authentication system
  • Integrate the new Billing library and Stripe in your Laravel application
  • Use the Artisan command-line tool
  • Test your App in Production with Behat

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Sep 14, 2016
Length: 402 pages
Edition : 1st
Language : English
ISBN-13 : 9781786469656
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Sep 14, 2016
Length: 402 pages
Edition : 1st
Language : English
ISBN-13 : 9781786469656
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 103.97
Laravel 5.x Cookbook
€41.99
Laravel 5 Essentials
€24.99
Full-Stack Vue.js 2 and Laravel 5
€36.99
Total 103.97 Stars icon
Banner background image

Table of Contents

11 Chapters
1. Setting Up and Installing Laravel Chevron down icon Chevron up icon
2. Using Composer Packages Chevron down icon Chevron up icon
3. Routing Chevron down icon Chevron up icon
4. Building Views and Adding Style Chevron down icon Chevron up icon
5. Working with Data Chevron down icon Chevron up icon
6. Adding Angular to Your App Chevron down icon Chevron up icon
7. Authentication, Security, and Subscriptions Chevron down icon Chevron up icon
8. Testing and Debugging Your Application Chevron down icon Chevron up icon
9. Adding Advanced Features to Your App Chevron down icon Chevron up icon
10. Deploying Your App Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
(6 Ratings)
5 star 0%
4 star 16.7%
3 star 0%
2 star 50%
1 star 33.3%
Filter icon Filter
Top Reviews

Filter reviews by




Noel Falcon Apr 18, 2018
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Good content
Amazon Verified review Amazon
KaylaKaze Aug 10, 2017
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
Word of advice: if you're writing a programming book, make your screenshots using a WHITE background on your IDE.
Amazon Verified review Amazon
MJW Oct 11, 2016
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
I'd be able to give it more stars if I could read the code examples. Unfortunately they have been take from screenshots of a code editor with a dark profile, so some code is indistinguishable from the background.The content may be great, but in terms of print and presentation, is not as good as the O'Reilly 'Cookbook' series.
Amazon Verified review Amazon
RK Oct 03, 2017
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
The code printed is very small for a number of snippets with white text merged into black background.. Whose idea black ?..REALLY not readable. .
Amazon Verified review Amazon
Amazon Customer Aug 24, 2021
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I guess it was my fault really. I was looking for a good book for beginners but made a wrong purchase. The author is a programmer and not a teacher . Most of the content made assumptions that you were an advanced user of Laravel. The only concession wa the first chapter where you were introduced to the basic setup., and even for half of that chapter the reader is sent away somewhere or the other to get how-to information. I could have saved my money and downloaded free tutorials instead...which I eventually did . From the second chapter to the end the author was completely immersed in displaying his brilliance at programming and on his brilliant project he is working on in this Cookbook. It is a distressing book . I'd possibly eventually use it as a door stop. It is that heavy !
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.