0

I'm trying to create a RESTful API using Laravel.

In my routes.php:

Route::get('/accounts/(:any?)', array('as'=>'account_index', 'uses'=>'accounts@index'));

My controller:

class Accounts_Controller extends Base_Controller {
public $restful = true;

public function get_index($id = null) {
    if(!$id)
        return Response::json(Account::all());
    return Response::json(Account::find($id));
}

I get 404 responses when I try any request accounts/##, but accounts works just fine. When I change my route to something that isn't accounts like:

Route::get('/accts/(:any?)'

My routing works as expected, and on top of that requests sent to accounts still work as well. Is it because I'm using get_index for my function name, so that it reverts to using the standard http://localhost/controller/method/arguments?

EDIT I have controllers being auto-detected:

Route::controller(Controller::detect());
2
  • What are your other routes? Are you using Route::controller? If so it needs to be defined after your other routes. Commented Jan 14, 2013 at 20:56
  • The only other route I have defined is the default home page that comes with Laravel, and Controllers are being registered with Route::controller(Controller::detect()); Commented Jan 14, 2013 at 20:57

1 Answer 1

4

When you define routes, the order in which these routes are defined matters. Laravel uses regular expressions to match the requested URI against these patterns, and the first one to match is used with no further processing.

Route::controller('accounts') is effectively matching accounts/(:any?)/(:any?)/(:any?) etc. If you were to test the url accounts/index/12 You would get the expected result.

Route::get('/accounts/(:any?)', array('as'=>'account_index', 'uses'=>'accounts@index'));
Route::controller( Controller::detect() );

Hope this helps.

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

3 Comments

This should become a comment under the question. If you feel that the last sentence answers the original question, I suggest elaborating on it and moving the initial questions to comments.
Sorry about that George, it was meant to be a comment and I was not paying attention. After getting more information from actaeon This is a real answer.
Ah, so registering a controller actually enforces the http://localhost/controller/method/arguments scheme. If I wanted to only do the routing by hand I shouldn't put in Controller:detect(). I took the line out. Thanks for your help!

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.