I have set up a project and written some routes in the api.php route file. I created a middleware, registered the middleware in the Kernel.php file, and then implemented the middleware in the api.php route group. However, when I hit the route, the request is not being received in the middleware. I have even followed the implementation steps from the Laravel documentation, but the problem remains the same – the request is not being received in the middleware.
routes/api.php
Route::group(["middleware" => "api_auth"], function () {
Route::get("/categories", ["uses" => "ApiController@getCategories"]);
Route::get("/child/categories", ["uses" => "ApiController@getChildCategories"]);
});
Another method for implementing middleware in api.php routes
Route::middleware(\App\Http\Middleware\ApiAuthentication::class)->group(function () {
Route::get("/categories", ["uses" => "ApiController@getCategories"]);
Route::get("/child/categories", ["uses" => "ApiController@getChildCategories"]);
});
app/Http/Kernel.php
protected $routeMiddleware = [
'api_auth' => \App\Http\Middleware\ApiAuthentication::class,
];
app/Providers/RouteServiceProvider.php
public function boot() {
$this->configureRateLimiting();
$this->routes(function () {
Route::middleware('api')
->middleware("throttle:30:1")// 60requests in 2 mins
->namespace('App\Http\Controllers')
->prefix('api')
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace('App\Http\Controllers\Admin')
->group(base_path('routes/web.php'));
});
}
app/Http/Middleware/ApiAuthentication.php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class ApiAuthentication {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next) {
$locale = "ar";
\Log::info("-- middware locale setting --{$locale}");
app('translator')->setLocale($locale);
return $next($request);
}
}
I have spent a significant amount of time on this issue but have been unable to identify the problem.
try