Using API Guard with Laravel 5.2

January 29, 2016

If you’ve been having problems with using chrisbjr’s ApiGuard library in your project after upgrading to Laravel 5.2, I’ve outlined the steps to take in order to have it working; particularly if you are getting the error below:

exception 'BadMethodCallException' with message 'Method [beforeFilter] does not exist.'

The error above appears because the beforeFilter method was deprecated in Laravel 5.1, and removed in 5.2; Laravel now uses Middleware.

This change was merged into the master branch of the ApiGuard module, but for some reason is not available when you do composer update. The fix involves updating your composer.json file to use dev-master.

First, update your composer.json file as follows:

...
"require": {
    ...
    "laravel/framework": "5.2.*",
    "chrisbjr/api-guard": "dev-master",
    ...
},
...

Next, run composer update for your project.

Finally, add the apiguard middleware to the $routeMiddleware array in app/Http/Kernel.php as shown below:

<?php
protected $routeMiddleware = [
    ...
    'apiguard' => \Chrisbjr\ApiGuard\Http\Middleware\ApiGuard::class,
    ...
];
?>

That’s it! You should be able to get back to creating amazing apps with Laravel! Till next time, bye!

I wouldn’t have been able to figure this out without the resources below; Asanteni sana!: