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

syntaxerron's avatar

How to validate route parameters?

I am having issues if there is an instance where the client request without route parameters like '/departments/delete/'. I get 405 Method Not Allowed error. How can I validate something like this where I can return a custom error message that no data has been passed?

0 likes
4 replies
neilstee's avatar

@erron you can create a route to /departments/delete/ and always return an error message that they need to pass the required parameter.

SilenceBringer's avatar

@erron it looks strange to me to expect route without required param. It this request comes from your application you should fix the reason - prevent the ability to send request without this param instead of trying to fix the result.

Or do you use it in api?

BRVK's avatar

My thought

route:- Route::get('/departments/delete/{par1?}/{par2?}', 'TestController@testing');

Controller:

function testing($par1, $par2) {

if ($par1 == '') return back()->with('error', 'Please parameter');

// else

your code

Please or to participate in this conversation.