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 3. Routing

In this chapter, we will cover the following topics:

  • Building an API / JSON based route for searching

  • Testing your route in PHPUnit

  • Building a view based route

  • Testing your view based route in PHPUnit

  • Creating named routes

Introduction


In this chapter, we will cover routing. One thing, in my opinion, that makes a great framework is easy-to-use routing. When I first started in Laravel, coming from Drupal and then Rails, I was really glad to see how easy it is to try ideas in a route and quickly see results.

We will cover testing your routes, API based routes, and naming. By the time you are done I think you will see just how easy routing is in Laravel.

Building an API / JSON based route for searching


In Chapter 6, Adding Angular to Your App we will cover Angular and in there I needed to make an API for the widget to talk to. Let's review that again here in more detail.

Getting ready

A fresh install of Laravel will do.

How to do it...

Follow the steps to build an API / JSON route for searching:

  1. Add our controller:

    > php artisan make:controller SearchComics
    
  2. Add our route:

  3. Fill in the controller:

  4. And in the ComicClientInterface class, I handle the comics method like this. I will go into more details in the How it works… section:

  5. Show it working at the UI level http://recipes.dev/api/v1/search:

    Figure 1 Example of JSON Response from Search

How it works…

First, I will go over what I did above but then I will show another example to simplify it more.

As usual, I made the controller and plugged in the route. Note I use api/v1/ as a route prefix. This just helps me later on do a breaking API change at /api/v2. There are other ways to do this but for now...

Testing your route in PHPUnit


As seen in the preceding recipe, I used the browser to show that the API is working. Here, I want to show how you can speed up your workflow by using PHPUnit to do this and then, more importantly, you end up with long term testing.

Getting ready

The previous recipes will help show how to lay the foundation. In this one, I will continue from there.

How to do it…

Follow these steps to test your route:

  1. Make a test:

    > php artisan make:test SearchComicsApiTest
    
  2. Then, edit the file tests/SearchComicsApiTest.php:

  3. And run phpunit:

    > phpunit --filter=api_results_from_search_verify_format
    
  4. Now let's make it do something by hitting that API:

  5. And let's verify some data, first, I will dump the data to my terminal to get a sense of what I can look for:

    We will get the following output:

  6. Then, I decide to assert a few things:

  7. That's it, I now know my route is working, without going in the browser, and can now start working with the AngularJS code to display this.

How it works…

Pretty...

Building a view based route


In the preceding recipe, we showed how to build a JSON-based API response in a route with a real view. I will show here just how simple it is in routes to output a View. Typically, I would use a controller but in this case I will not, just to show how you can experiment in a route at different levels.

Getting ready

A base install will be fine for this one.

How to do it…

  1. Let's add another route for this:

  2. Then, make a view to handle that route resources/views/examples/route_view.blade.php:

  3. And give it a look http://recipes.dev/example_view:

How it works…

Pretty nice how simple this is! I have worked in other frameworks where making routes is a chore in abstraction and speed, or lack of speed.

In this case, we can quickly play with ideas and show results in a view right from the route. You could build an entire API in the route for small microservices.

In this case I just make an array, put it into the view using the php compact syntax, and display it. Notice too the dot notation...

Testing your view based route in PHPUnit


Using the above recipe, I was able to make a view right from the route, now I want to test my view and make sure it is doing what is expected. Typically, I would use Behat but in this example, I will use PHPUnit.

Getting ready

If you follow the above recipe, you will have an example page in place. From here we will start with the testing.

How to do it…

Follow the listed steps for testing your view based route:

  1. As usual, make a test:

    >php artisan make:test ExampleViewTest
    
  2. Then, in that file tests/ExampleViewTest.php, I begin to write a test:

  3. Run the test and it passes:

    > phpunit --filter=example_view
    
  4. Now, to see how we can deal with authentication, add this to the route:

  5. And what was a passing test is now failing:

  6. All we have to do is add $this->actingAs():

  7. And we are back to passing. If you are using $this->call() instead of $this->visit() then you can use $this->be() instead:

How it works…

Pretty nice how Laravel brings all of this into PHPUnit...

Creating named routes


From the beginning, even in a small project, I highly suggest naming routes. I will cover a few examples of how and why here.

Getting ready

A base install of Laravel is great. I will use some routes and views from above but you can easily follow along.

How to do it…

Follow the listed steps for creating named routes:

  1. To begin with, let's look at some routes I have:

    > php artisan route:list
    

    You will see something like this if you followed along so far, else you will just see the default Laravel route.

  2. Notice the name section; that is what we are aiming for here.

  3. Note the admin section admin.memberships and admin.users in my app/routes.php file:

  4. See the ->name() method at the end of the Route facade, it is the cause of this magic.

  5. Also notice the dot notation naming to help organize a bit more. This is optional but I like it for grouping like routes.

  6. Here is where it comes in handy though, open up the test and swap out the path for the route method:

  7. See the following route...

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}