Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Laravel 5.x Cookbook

You're reading from  Laravel 5.x Cookbook

Product type Book
Published in Sep 2016
Publisher Packt
ISBN-13 9781786462084
Pages 402 pages
Edition 1st Edition
Languages
Authors (2):
Terry Matula Terry Matula
Profile icon Terry Matula
Alfred Nutile Alfred Nutile
Profile icon Alfred Nutile
View More author details

Table of Contents (17) Chapters

Laravel 5.x Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
1. Setting Up and Installing Laravel 2. Using Composer Packages 3. Routing 4. Building Views and Adding Style 5. Working with Data 6. Adding Angular to Your App 7. Authentication, Security, and Subscriptions 8. Testing and Debugging Your Application 9. Adding Advanced Features to Your App 10. Deploying Your App Index

Chapter 9. Adding Advanced Features to Your App

In this chapter, we will cover the following topics:

  • Building an Artisan command

  • Creating scheduler to notify users of new comics

  • Setting up e-mail notices

  • Adding clean URLs for the users profile page

  • Using pusher for live notifications

  • Adding a blog area to update users on new features

Introduction


In this chapter, I will tackle some common needs of application building that are a bit more difficult than the rest. We will see how cool it is to have Artisan commands, and we will understand how to use them. We will also cover setting up notification e-mails so that we can notify users when new comics come in and more.

Building an Artisan command


First, we will build an Artisan command. This is my favorite feature of Laravel. It is a nice wrapper around the Symfony console, making it easy to make commands. Commands are a key solution to long-running background processes or to those commands which you need to maintain your site and more.

For example, many projects that I am working on use this for creating Oauth tokens, stub data for a complex data set, and more; it is also used to run code optimizations using tools such as php-cs-fixer.

Here, we will build a command to search for the latest comics of user favorites that we will use later for scheduled commands.

Getting ready

A fresh install of Laravel is fine. I am still working inside the context of the comic book app. Also, note that this will assume you have a user in UserTableSeeder, as I did in Chapter 5, Working with Data.

How to do it...

  1. Make some seed data for the user to run the following:

    >php artisan make:seeder FavoritesSeeder
    
  2. Then, add this file...

Creating scheduler to notify users of new comics


Now is the time for another superpower feature built into Laravel—scheduling! OK, it does not sound that exciting, but the fact that I can set one cron job on my server to do all my tasks makes it a lot easier to move my features from server to server. I will show here one example of how to use it based on the console command that I made previously.

Getting ready

A base install of Laravel is fine. If you follow the preceding recipe, you will have a decent size command to run via this scheduling example.

How to do it…

  1. In the previous recipe, we ran php artisan make:console GetUsersLatestsFavoritesConsole to get a console command. Then, I added some query work over there to make it do its thing.

  2. I then updated the code in app/Console/Commands/GetUsersLatestsFavoritesConsole.php to catch any issues and put them in the logs. This way, I can see when things go wrong, since I will not be running this at the command line. I will talk about this more in...

Setting up e-mail notices


In this recipe, we are going to use the previous work to trigger an event that will send e-mails. E-mails are a slow process sometimes, so I will put them in a queue. In this case, it will be a database queue since it is just local communication. Once we are done, we will see how to send a "nice" looking e-mail.

Getting ready

A base Laravel install will do. I will be working from the previous work, but you can follow along.

How to do it…

  1. First, let's make our queue database tables:

    > php artisan queue:table && php artisan migrate
    
  2. Then, let's set this to sync in our .env file. Make sure that the QUEUE_DRIVER variable in your .env file looks like the example here:

    Queue driver setting in .env

  3. In the .enf file, we will set MAIL to log until we are ready:

  4. Then, we will make the job:

    > php artisan make:job SendFavoritesEmail
    
  5. Let's just add a placeholder there for now, it is app/Jobs/SendFavoritesE-mail.php:

  6. Now, our handler will react to the queue, which I will...

Adding clean URLS for the users profile page


There is nothing like sending a user to profile/44444-55555-6666-7777 or any page in our application. In this recipe, I will add some slugs to the user profile page.

Note

Slug? Semantic URL

Semantic URLs, also sometimes referred to as clean URLs, RESTful URLs, user-friendly URLs, or search engine-friendly URLs…

Refer to Wikipedia for more information: https://en.wikipedia.org/wiki/Semantic_URL and see an example slug in that URL.

Getting ready

A fresh install of Laravel can work for you with a database for making users for whom you can add a Slug.

How to do it…

  1. Install the needed library from https://packagist.org/packages/spatie/laravel-sluggable:

    >composer require spatie/laravel-sluggable 
    
  2. Update the database as needed, so we can save this slug for the user model:

  3. Now is the time for the code to do the alteration on the table:

  4. Then, run the migration:

    >php artisan migrate
    
  5. Update the code in app/User.php as per the package docs:

  6. Let's watch it work...

Using pusher for live notifications


Pusher is a great service and is a super simple way to start sending messages to the UI from the backend of your application. In this example, I will notify persons in the UI that they have a Favorite series that came out. I will cover setting up a pusher account, setting up Laravel to talk to pusher, and finally setting up Angular to show this message.

Getting ready

I have been building up a site throughout this book and will now plug this into the already built command to trigger the event that I will use to broadcast to pusher and Angular. Make sure you have Laravel freshly installed if you have not made it down to this part of the book.

How to do it…

  1. Get pusher info, go to https://pusher.com, and sign up.

  2. Then, create a new app, I will call it recipes; you will get a page like this:

    Pusher creating a new app

  3. Plug this info into Laravel in the config/broadcasting.php file where we see all of our env settings just waiting for the settings that we see in A in...

Adding a blog area to update users on new features


Laravel is not a CMS, but still, it is really nice to create a simple, markdown-based blog to quickly post updates about latest features and news for the comic site. In this section, I will show how we can use Slugs from the earlier recipe called Adding clean URLs for the users profile page in this chapter, and a markdown library, so we can make a blog area that is paginated and ready to show off our latest info written in markdown and converted into HTML.

Getting ready

A fresh install of Laravel is fine. I will continue on with the comic book site I have been making all along.

How to do it…

  1. Use a scaffolding library that I talked about in Chapter 5, Working with Data:

    >php artisan make:scaffold Blog --schema="title:string, mark_down:text, html:text, active:boolean:default(0), url:string"
    
  2. Make sure your migration ends up looking like this:

  3. Then, I add the controller to my app/Http/routes.php file:

  4. Then, I fix up the views the way I need them...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Laravel 5.x Cookbook
Published in: Sep 2016 Publisher: Packt ISBN-13: 9781786462084
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}