Auto Deploy Docs to Laravel Forge

If you use to generate documentation and Laravel Forge, you can set up auto deployment and publishing quite easily. Setup the site in Forge, making sure you have a composer.json and an empty public directory in your repo.

You can then use this deploy script to build, move and publish your changes automatically after you’ve added a webhook too:

Laravel & Lumen: Return Correct Error Responses for Requests

I’ve seen a few pieces of API code dotted around the web recently where people have written simple JSON APIs but their error handling still returns HTML instead of JSON for 404 errors and the likes. This is pretty simple to solve with Laravel or Lumen and easily testable too. The cleanest way I’ve seen of handling this was written by Paul Redmond in his book ‘Writing APIs with Lumen’ which you can purchase on Leanpub.

In your error handler, typically located at app/Exceptions/Handler.php, you’ll need to add a check for wantsJson() on the Request object, like this:

Pretty simple and effective, you can handle more cases within your handler as well, but that’s enough to illustrate the idea. You can test the above is working by using something similar to this test:

A quick and simple tip, but one that seems to be overlooked often.


Laravel Blade Stacks

One thing that I often forget about is a nice feature in Laravel that allows you to push scripts or other blocks of code into another part of your templates, so for example:

You can do this multiple times and each one will be pushed on to the stack, when you’re ready to dump and display them, you can dump to view using @stack:

Nothing ground breaking here, but incredibly useful. You can read more about it on the Laravel documentation.

Laravel Blade Set Class by Current Route Name

If you need to set an active class on a list item for a menu, you can do something like this below:

This will only work if you name all your routes, it could easily be adjusted to handle non-named routes as well.