You're reading from Laravel 5.x Cookbook
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.
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.
Follow the steps to build an API / JSON route for searching:
Add our controller:
> php artisan make:controller SearchComics
Add our route:
Fill in the controller:
And in the
ComicClientInterface
class, I handle thecomics
method like this. I will go into more details in the How it works… section:Show it working at the UI level http://recipes.dev/api/v1/search:
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...
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.
The previous recipes will help show how to lay the foundation. In this one, I will continue from there.
Follow these steps to test your route:
Make a test:
> php artisan make:test SearchComicsApiTest
Then, edit the file
tests/SearchComicsApiTest.php
:And run
phpunit
:> phpunit --filter=api_results_from_search_verify_format
Now let's make it do something by hitting that API:
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:
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.
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.
Let's add another route for this:
Then, make a view to handle that route
resources/views/examples/route_view.blade.php
:And give it a look http://recipes.dev/example_view:
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...
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.
If you follow the above recipe, you will have an example page in place. From here we will start with the testing.
Follow the listed steps for testing your view based route:
As usual, make a test:
>php artisan make:test ExampleViewTest
Then, in that file
tests/ExampleViewTest.php
, I begin to write a test:Run the test and it passes:
> phpunit --filter=example_view
Now, to see how we can deal with authentication, add this to the route:
All we have to do is add
$this->actingAs()
:And we are back to passing. If you are using
$this->call()
instead of$this->visit()
then you can use$this->be()
instead:
From the beginning, even in a small project, I highly suggest naming routes. I will cover a few examples of how and why here.
A base install of Laravel is great. I will use some routes and views from above but you can easily follow along.
Follow the listed steps for creating named routes:
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.
Notice the
name
section; that is what we are aiming for here.Note the
admin
sectionadmin.memberships
andadmin.users
in myapp/routes.php
file:See the
->name()
method at the end of theRoute
facade, it is the cause of this magic.Also notice the dot notation naming to help organize a bit more. This is optional but I like it for grouping like routes.
Here is where it comes in handy though, open up the test and swap out the path for the route method:
See the following route...