Reader small image

You're reading from  Laravel 5.x Cookbook

Product typeBook
Published inSep 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781786462084
Edition1st Edition
Languages
Tools
Right arrow
Authors (2):
Terry Matula
Terry Matula
author image
Terry Matula

Terry Matula is a web developer and Laravel advocate based in Austin, TX. He's been a passionate computer enthusiast since he first played Oregon Trail on an Apple//e. He started programming in BASIC at a young age, making simple Scott Adams-like games on a Commodore Vic-20. Since then, he's worked as a developer using Flash/ActionScript, ASP.NET, PHP, and numerous PHP frameworks, with Laravel being his favorite by far. He blogs web development tips and tricks at his website http://terrymatula.com
Read more about Terry Matula

Alfred Nutile
Alfred Nutile
author image
Alfred Nutile

Alfred Nutile is an Enterprise Architect and Laravel lead based in Western Massachusetts. He's been working in the industry since the mid 90's. He started in PHP and MySQL back around that time and has worked with Ruby on Rails, Drupal, and Angular along the way. He introduced Laravel into an enterprise web stack, where he is currently contracted at, as Laravel proved itself as an amazing framework to build API's and Angular heavy application. You can read more about him and checkout his blog at http://www.alfrednutile.info/ or follow him on Twitter at https://twitter.com/alnutile Make sure to checkout the discount LaraCasts is offering those who buy the book! Get a coupon for 50% on your first bill. Make it a yearly subscription and save $43! Coupon Code: LaracastsLovesPackt https://laracasts.com/signup?plan=yearly&coupon=LaracastsLovesPackt.
Read more about Alfred Nutile

View More author details
Right arrow

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 2016Publisher: PacktISBN-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.
undefined
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 $15.99/month. Cancel anytime

Authors (2)

author image
Terry Matula

Terry Matula is a web developer and Laravel advocate based in Austin, TX. He's been a passionate computer enthusiast since he first played Oregon Trail on an Apple//e. He started programming in BASIC at a young age, making simple Scott Adams-like games on a Commodore Vic-20. Since then, he's worked as a developer using Flash/ActionScript, ASP.NET, PHP, and numerous PHP frameworks, with Laravel being his favorite by far. He blogs web development tips and tricks at his website http://terrymatula.com
Read more about Terry Matula

author image
Alfred Nutile

Alfred Nutile is an Enterprise Architect and Laravel lead based in Western Massachusetts. He's been working in the industry since the mid 90's. He started in PHP and MySQL back around that time and has worked with Ruby on Rails, Drupal, and Angular along the way. He introduced Laravel into an enterprise web stack, where he is currently contracted at, as Laravel proved itself as an amazing framework to build API's and Angular heavy application. You can read more about him and checkout his blog at http://www.alfrednutile.info/ or follow him on Twitter at https://twitter.com/alnutile Make sure to checkout the discount LaraCasts is offering those who buy the book! Get a coupon for 50% on your first bill. Make it a yearly subscription and save $43! Coupon Code: LaracastsLovesPackt https://laracasts.com/signup?plan=yearly&coupon=LaracastsLovesPackt.
Read more about Alfred Nutile