Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lunaticsz's avatar

Checking requests using middleware before it even touching Controller method.

Hi guys,

I've been learning Laravel since 5.0 but then I had left programming work for sometime now so I was kind of forgot everything about Laravel. I'm now in the process of learning it again using Laravel 5.1 and I need your help to refresh my memory.

So basically I want to create some article publishing panel, let say for example it will post to a Wordpress blog using API (XMLRPC or manually build the request (curl: login -> new post -> etc.)? ). I will "register" these "API" url into my panel, and I will have input fields to fill the article contents and other necessary data. Basically it will call Route::('posttoblog', 'PostController@submit') , the problem here is that I don't even know if the "API" url is even up, so I'm about to create some mechanism to check it even before the "input fields" data reached the PostController. I have an idea to check the "API" url via a Middleware, something like: public function handle($request, Closure $next) { if( ! is_api_url_online($request->api_url)) return redirect()->back()->with('status', 'The API URL is down, article submission aborted');

return $next($request); // let the PostController@submit do the job. }

And yeah I think it will definitely work, but can anyone suggest me a better approach?

0 likes
3 replies
Snapey's avatar

probably should be confined to a service provider.

don't use middleware just because it exists. Middleware should be like a thin wrapper around your application and not a fundamental part of a specific function.

also, i'm puzzled? why restart learning on 5.1 when that is a couple of years old?

lunaticsz's avatar

I'll look into it and try to implement. I'm restarting from 5.1 because all of my old projects were built using that version and I'm too lazy to look into the "changes" between 5.1 to 5.3 so I'd prefer to stay on 5.1 for sometime until I get back my understanding of Laravel. Thanks for your help.

Snapey's avatar

If you post questions here, remember to post the version because folks won't be expecting an older version.

Please or to participate in this conversation.