Skip to content

Instantly share code, notes, and snippets.

@w1lldone
Last active February 11, 2020 13:02
Show Gist options
  • Select an option

  • Save w1lldone/cdefae38ce36b3326867b7368c48f42c to your computer and use it in GitHub Desktop.

Select an option

Save w1lldone/cdefae38ce36b3326867b7368c48f42c to your computer and use it in GitHub Desktop.
Laravel Full REST Mode

Laravel Setup For Full REST Mode

This is my setup for full REST model Laravel projects Source: Laracon US 2018: TJ Miller - APIs With Laravel @14:40

Remove middleware Files

Go To app/Http/Kernel.php and remove these middlewares.

  • \App\Http\Middleware\EncryptCookies::class
  • Redirect If Authenticated
  • \App\Http\Middleware\VerifyCsrfToken::class

Remove providers and aliases

Go to config/app.php and remove these providers and Aliases

Providers

  • Illuminate\Cookie\CookieServiceProvider::class
  • Illuminate\Session\SessionServiceProvider::class,

Aliases

  • 'Blade' => Illuminate\Support\Facades\Blade::class,

Route Service Provider

  • Get rid $this->mapWebRoutes();

Change Default Auth Guard

Go to config/auth.php. Change defaults.guard to api

Make Laravel Always return JSON

  • Create a middleware:
namespace App\Http\Middleware;

use Closure;

class AlwaysReturnJson
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $request->headers->set("Accept", "Application/json");
        
        return $next($request);
    }
}
  • Register the middleware to global middleware in app\Http\Kernel.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment