So, I have a problem. It seems that using middleware from within a controller becomes a problem when paired with my current routing setup...
Currently, I am handling a bunch of external endpoints with the following setup:
Route::match(['get', 'post'], '/api/{api_key}', function ($api_key) {
if (file_exists(app_path("Http/Controllers/ApiEndpoints/{$api_key}.php"))) {
return app("\\App\\Http\\Controllers\\ApiEndpoints\\{$api_key}")->receive();
} else {
return response('Please provide a valid API key', 403);
}
});
A third party provides a "key" which is simply translated to an actual controller name, then the request is routed to the controller (more precisely, the function receive()) - if it exists.
The problem is that every api key should access unique logic and have the ability to use different middleware. However, that does not work.
This is an excerpt from one such controller (these are located in app/Http/Controllers/ApiEndpoints):
namespace App\Http\Controllers\ApiEndpoints;
use App\Http\Middleware\CustomMiddlewareDummyName;
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;
class test implements HasMiddleware
{
public static function middleware(): array
{
return [
new Middleware(CustomMiddlewareDummyName::class),
];
}
public function receive()
{
//do stuff
}
}
The middleware is never loaded, the request is simply sent to receive() with no apparent consequences. I can make it work on a controller from the default controller folder.
I am hopeless about the specifics on namespaces which I think is where the problem stems from. Any and all help is greatly appreciated. So is alternative solutions.