Skip to content

Instantly share code, notes, and snippets.

@JacobBennett
JacobBennett / blog.md
Last active December 21, 2015 21:26
Return a custom Unauthorized page for Laravel Form Requests

By default, when using a Laravel Form Request, you have a handy option to authorize the request before passing it along to validate against your set of rules. As expected, if false is returned from the authorize method, you will receive a 403 response status code. The problem with the response that Laravel provides is that it skips right past your App\Exceptions\Handler and instead is caught in the Illuminate\Routing\Route class. What this means is that any custom error pages that you might be returning from your Handler class for a 403 / Unauthorized Request are ignored.

A quick fix to this is to override the forbiddenResponse method by placing the following method on your abstract App\Http\Requests class.

public function forbiddenResponse()
{
    return abort(403);
}
@JacobBennett
JacobBennett / blog.md
Last active August 30, 2022 15:25
Setting up a $5 Staging Server with Forge, Envoyer, and Nginx

Ever wanted to have your very own staging server? Here is how to make that happen for $5 bucks a month (assuming you are already using Forge and Envoyer).

1. Spin up a new server with Forge on Digital Ocean for $5 bucks.

For first time Forge users, I would highly suggest this great blog post from the one and only Matt StaufferUp and running with Forge

If you learn better through video, check out the free Forge series on Laracasts - Server Management With Forge

2. Set up Envoyer to auto-deploy from your staging branch

<?php
// (string) $message - message to be passed to Slack
// (string) $room - room in which to write the message, too
// (string) $icon - You can set up custom emoji icons to use with each message
public static function slack($message, $room = "general", $icon = ":longbox:") {
$data = json_encode(array(
"channel" => "#{$room}",
"text" => $message,
"icon_emoji" => $icon
@JacobBennett
JacobBennett / QueueRetryAllCommand.php
Created November 11, 2015 05:32
Laravel Queue:Retry-Multiple and Queue:Retry-All
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class RetryAllCommand extends Command
{
/**
* The name and signature of the console command.
@JacobBennett
JacobBennett / blog.md
Last active August 26, 2021 09:28
Recovering from Laravel failed-job Hell

Update: After a PR, this has been pulled into Laravel 5.1. Now retry multiple jobs with queue:retry 1 2 3 or queue:retry all. See the new retry command.

Recently, the Amazon SES region we use to send email went down for approx 45 minutes. During that time, any emails that we had queued to run failed. This left us with a massive list of failed jobs which all needed to be released to the queue to run again.

The Problem

Currently the only way to accomplish this is to run the artisan queue:retry command for each and every failed job. This task would be made much easier if there were a way to release multiple jobs back onto the queue using a single command. Let’s make that happen! (or just skip to the final commands)

RTFM

To get started, take a look at the source of the current retry command over at the [laravel/framework](https:

@JacobBennett
JacobBennett / blog.md
Last active June 4, 2023 05:50
Sharing Laravel Cookies across subdomains

I recently ran into a problem where I had a suite of applications hosted on a set of subdomains that all needed to be able to share a single cookie. Any cookies other than the shared cookie needed to stay specific to their subdomain, but this one shared cookie needed to be accessible to any of them. I also wanted to accomplish this using Laravel's Cookie facade.

The Problem

To accomplish this, there were two issues to solve.

  1. All cookies created by the Laravel framework are encrypted by default. (link)
  2. I needed to figure out how to set a domain on the shared cookie that was different than the domain it was being set from.

Setting Non-Encrypted Cookies

In Laravel 5.1, a feature was added which allows you to add a list of cookie names that should not be encrypted to the EncryptCookies Middleware under the $except array. Check out [the docs](http://laravel.com/docs/5.1/responses#attaching-cookies

@JacobBennett
JacobBennett / blog.MD
Last active February 15, 2016 21:01
How SQL orders results when using equals

The end goal was to order a query by a list of specific values for a column, but then also order those by a created_at date in ascending order and then by id in ascending order

This proved to be more of a challenge than I originally thought. The solution I came up with is as follows.

SELECT id, clientId, created_at 
FROM `claims` 
WHERE `clientId` IN ('FOO', 'BAR', 'BAZ', 'FIZZ') 
ORDER BY clientId = 'FIZZ', clientId = 'BAZ', clientId = 'BAR', clientId = 'FOO', 
created_at ASC, id ASC
server {
listen 80;
server_name www.default.com;
return 301 $scheme://default$request_uri;
}
$( function()
{
var targets = $( '[rel~=tooltip]' ),
target = false,
tooltip = false,
title = false;
targets.bind( 'mouseenter', function()
{
target = $( this );
@JacobBennett
JacobBennett / gist:edcda5625098fe3b6bab
Last active October 6, 2015 05:24
Setup Node and Gulp on a Digital Ocean Droplet for Elixir
sudo apt-get update
curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install -y nodejs
npm install -g gulp
ALSO...
https://www.digitalocean.com/community/tutorials/how-to-get-started-with-gulp-js-on-your-vps
https://www.digitalocean.com/community/tutorials/how-to-install-an-upstream-version-of-node-js-on-ubuntu-12-04