0

I'm fairly new to Laravel. I'm having a problem with routing.

Route::group(['prefix'=>'api/v1'],function(){
  Route::resource('results','RequestController');
  Route::get('results/getByName/{name}','RequestController@getByName');
  Route::get('results/getLastTen','RequestController@getLastTen');
});

The problem is that the last route under prefix api/v1 does not work. When I call it it shows nothing, not even any error.

The code at the requestController is:

public function getLastTen(){
        $results=DB::table('latest_random_trends')->limit(10)->get();
        return $results;
    }

Everything is alright with the code on the controller since it works when I call it from the routes.php file outside of the prefix 'api/v1' like this:

    Route::get('results/getLastTen','RequestController@getLastTen');

but when it is inside the prefix it does not work unless I add a variable to it like this:

    Route::get('results/getLastTen/{var}','RequestController@getLastTen');
3
  • Are you going to the url /api/v1/results/getLastTen? Commented Oct 18, 2017 at 14:26
  • Yes, there is nothing wrong with the url. It does not display anything when I call it. However, when I put the route under another prefix like api/v2 it works fine Commented Oct 18, 2017 at 14:30
  • What happens if you remove the resource routes? Does it work? If so, I think you might have a conflict between the resource routes and your custom ones. Commented Oct 18, 2017 at 14:31

1 Answer 1

2

Since you have a Route::resource above it, I think what's happening is that the show method on Resource Controller is getting the route instead of the one you wrote.

Try one the following:

  • Exclude the show method if you're not going to use it

    Route::resource('results','RequestController', ['except' => 'show']);
    
  • Move your custom route above the resource route

    Route::group(['prefix'=>'api/v1'],function(){
        Route::get('results/getLastTen','RequestController@getLastTen');
        Route::resource('results','RequestController');       
        Route::get('results/getByName/{name}', 'RequestController@getByName');
    
    });
    

For more information, check out the show action on Laravel Docs

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

5 Comments

I was thinking the same but the last defined routes doesn't override previous routes in case of conflict?
@Camilo No, the first route to match, the first it will be executed
The reason because works when adding {var} to the end of the route is the same reason of getByName/{var} currently works under the resource route. It's because the pattern no longer matches the default show action on resource controllers.
Excuse me for my inexperience, but why would it go to the show method when I specify it that it goes at the getLastTen method?
Because the show method in resource controller determines the url as results/{something}. That something var, in this case, is getLastTen. It's a regular expression and the show method matches that. Since the resource routes were defined before the getLastTen, show route was triggered and searched a result for the id getLastTen. I guess you had the show method empty and that's why it didn't show anything before moving the route up

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.