03.12.2015 Views

laravel4cookbook

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

API 132<br />

1 'Event' => 'Illuminate\Support\Facades\Event',<br />

.<br />

This was extracted from app/config/app.php.<br />

I changed the key of that entry to Events but you can really call it anything you like.<br />

.<br />

It’s often just easier to change the name of the classes you create than it is to alter the aliases array.<br />

There are many Laravel 4 tutorials that will refer to these classes by their alias, so you’ll need to<br />

keep a mental log-book of the aliases you’ve changed and where they might be referred to.<br />

Testing Endpoints<br />

It’s not always easy to test REST API’s simply by using the browser. Often you will need to use an<br />

application to perform the different request methods. Thankfully modern *nix systems already have<br />

the Curl library, which makes these sorts of tests easier.<br />

You can test the index endpoint with the console command:<br />

1 curl http://dev.tutorial-laravel-4-api/event<br />

.<br />

Your host (domain) name will differ based on your own setup.<br />

.<br />

You should only be seeing a JSON response - if you’re seeing HTML there’s a good chance it’s a<br />

Laravel error page. Check your application logs for more details.<br />

Unless you’ve manually populated the event table, or set up a seeder for it; you should see an empty<br />

JSON array. This is a good thing, and also telling of some Laravel 4 magic. Our index() action returns<br />

a collection, but it’s converted to JSON output when passed in a place where a Response is expected.<br />

Let’s add a new event:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!