3

I have a Laravel 3 application that has several REST-ful controllers.

The controllers that take no parameters (e.g. a controller that handles the URL /api/books) works fine, but when I try and access the URL of a controller that takes parameters (e.g. /api/book/1), it doesn't work. However, if I append the method name to the URL (e.g. /api/book/index/1), it does work properly.

Is there a way to not be required to use the keyword "index" on a controller?

An example of one of the non-functioning controllers--

<?php
class API_Book_Controller extends Base_Controller {

/**
 * Indicates the controller is RESTful
 * @var boolean
 */
public $restful = true;

/**
 * Fetch a book by ID
 * @param  integer $id ID number of the book
 * @return Response    HTTP response
 */
public function get_index($id = null){
    $book = Book::find($id);

    if(is_null($book)){
        return Response::error('404');
    }

    return Response::eloquent($book);
}
Andrew M
  • 4,208
  • 11
  • 42
  • 67

1 Answers1

1
Route::get('api/book/(:num?)', 'API_Book_Controller@get_index');
Miguel Borges
  • 7,549
  • 8
  • 39
  • 57
  • You mean 'API_Book@index'. The get is already declared by the Route type, and the controller suffix is not required. – David Barker May 23 '13 at 12:28
  • 1
    Yeah, I was afraid I'd have to explicitly define the routes. Preferably, since I'm using controller routing (e.g. `Route::controller('home')`), there'd be a way to simply ignore the `index` action name and use that as the route... – Andrew M May 23 '13 at 16:42