Andrew M
Andrew M

Reputation: 4288

Laravel Controller Routes with Parameters

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);
}

Upvotes: 3

Views: 1881

Answers (1)

Miguel Borges
Miguel Borges

Reputation: 7659

Route::get('api/book/(:num?)', 'API_Book_Controller@get_index');

Upvotes: 1

Related Questions