3

Is it possible to create a route, that on the same URL calls different actions in different controllers based on the Content-Type header?

I do NOT need this for testing purposes. I need something to use in the api.php file.

D.R.
  • 2,540
  • 3
  • 24
  • 49
  • 2
    Possible duplicate of [How to load routes based on application/json header in Laravel](https://stackoverflow.com/questions/22770750/how-to-load-routes-based-on-application-json-header-in-laravel) – Blue Jan 12 '18 at 20:41
  • @FrankerZ No, it's not. I've seen this question. I do NOT need to test it. I need to DEFINE such a route. – D.R. Jan 12 '18 at 20:44
  • 2
    https://laravel.io/forum/02-12-2014-route-requests-based-on-http-headers – Blue Jan 12 '18 at 20:47
  • @FrankerZ yes, this is close to what I was looking for. I'll try to use bindings. Please, upwote my question or I'll be forced to delete it)) I don't want you to lost your reputation, cause you have helped me)) – D.R. Jan 12 '18 at 20:52
  • @D.R. Please do not delete your question because it has a negative score and please do not ask for upvotes. If people find you question interesting clear and helpful the upvotes will come in. Please improve your question instead of deleting is. It is very important that others can also view this question and it's answers so it can help them! – milo526 Jan 12 '18 at 20:59
  • If people think that this is a duplicate of something and you know it is not, it may help if you explicitly state this *inside* the question. Add something like "The question is different from /link-to-another-question/ in that ..." – Honza Zidek Jan 12 '18 at 23:13
  • Do it inside you controller. Don't make your route confusing. – Elias Soares Oct 24 '19 at 10:35

1 Answers1

0

I was looking for this approach, finally i register my routes base on special header that i want to check it. In my case, i want to use same URL base and action base same base on Content-Type as you. I take all my header in web.php and you can use it in api.php and check this to find out that Content-Type is set or not as below:


$headers = apache_request_headers();
// check headers Content-Type is application json for any call base on json request
if(array_key_exists('Content-Type', $headers) && strtolower($headers['Content-Type']) == 'application/json'){

    // register routes that need Content-Type header
    Route::get('/dashboard', "AuthController@dashboardWithHeader");

}else{

    // register route that dont need this special header
    Route::get('/dashboard', "AuthController@dashboard");

}

by this approach if Content-Type header is set, you related routes registerd and vise versa.

behnam shateri
  • 1,223
  • 13
  • 18
  • Although it works, I disagree with the method. I think this logic is responsibility of controller. – Elias Soares Oct 24 '19 at 10:36
  • you are right. but in some special case that we need one route with very very different behaviar we have to do that, for my case i use this approach when want to use react in laravel, if we dont set content type, we catch all route and dispatch to client side by `Route::view` to one view that client decide which page must be loaded and ... – behnam shateri Oct 24 '19 at 10:40
  • 2
    Keep in mind that this approach will not work with route cache. Also, the solution for this "complex" behavior is simple: put api routes under api.php, and non api routes on web.php. – Elias Soares Oct 24 '19 at 10:43
  • yes but we base on some special reason we decide to set this route in web and some route in api. but thank for your suggestion dear Elias – behnam shateri Oct 24 '19 at 10:44
  • Makes no sense to me, but different approaches are valid. Just consider finding a way to use route: cache, it make requests faster. – Elias Soares Oct 24 '19 at 10:46