2

Is it unconventional and therefore ill-advised to use the match() method to determine which controller method to use under 1 uniformed named route? I have this code:

Route::match(['get', 'post'], '/add/lecture/{course}', [
    'as' => 'addLecture',
    'uses' => Request::isMethod('post') ? 'Main@addLecture':'Main@showAddLecture'
]);

Which works as expected. But I just want to know if this is a feasible solution, or if I should stop being lazy and create two separate routes (I am not using Route::resource() for a particular reason, so please don't advise me to use that for basic CRUD). I don't mean for this question to be subjective, I presume there is an objective reason as to why this isn't employed very often?

1 Answer 1

3

It looks like hack. It's not readable and can stop working after random minor Laravel update. In my opinion it's better to create two explicit routes.

Sign up to request clarification or add additional context in comments.

2 Comments

It looking like a 'hack' was my interpretation as well... Why would minor updates affect this though? It seems unlikely Laravel is going to change the parameters for the Route class any time soon, and if they did I wouldn't consider that a 'minor' change. I'll stick to two explicit routes though, just to be safe (and definitely improve readability). Thanks
Well, they did similar changes many times already and sometimes they even didn't tell about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.