Reputation: 144
I want to have a middleware in the constructor of my controller to filter a non AJAX requests:
public function __construct()
{
parent::__construct();
// Return 404 if not AJAX request
$this->middleware(function ($request, $next) {
if (!$request->ajax()) {
return abort(404);
}
return $next;
}, ['only' => ['list', 'publish', 'unpublish', 'delete']]);
}
There is a method in the controller:
public function list(Request $request)
{
die('OK');
}
When I make an AJAX request I got the exception: "Closure object cannot have properties". If I comment out the middleware, everything works fine.
Upvotes: 0
Views: 1009
Reputation: 8252
Your return statement is wrong, you have to return $next($request);
instead of return $next;
I would personally change your code to this:
use Illuminate\Http\Response;
public function __construct()
{
parent::__construct();
// Return 404 if not AJAX request
$this->middleware(function ($request, $next) {
if (! $request->ajax()) {
abort(Response::HTTP_NOT_FOUND);
}
return $next($request);
})->only(['list', 'publish', 'unpublish', 'delete']);
}
Upvotes: 1