0
votes

Laravel routing functionality allows you to name a resource and name a controller to go with it. I am new to Laravel and would like to know if anyone knows how to extend the resources method in the route class provided.

Basically say I have: (which works fine)

/invoices

But say I want:

/invoices/status/unpaid

How is this achievable?

To see the basics of what I am doing check: http://laravel.com/docs/controllers#resource-controllers

1

1 Answers

8
votes

Resource controllers tie you into a specific URLs, such as:

  • GET|POST /invoices
  • GET|PUT /invoices/{$id}
  • GET /invoices/create
  • and so on as documented.

Since, by convention, GET /invoices is used to list all invoices, you may want to add some filtering on that:

/invoices?status=unpaid - which you can then use in code

<?php

class InvoiceController extends BaseController {

    public function index()
    {
        $status = Input::get('status');
        // Continue with logic, pagination, etc
    }

}

If you don't want to use filtering via a query string, in your case, you may be able to do something like:

// routes.php
Route::group(array('prefix' => 'invoice'), function()
{
    Route::get('status/unpaid', 'InvoiceController@filter');
});

Route::resource('invoice', 'InvoiceController');

That might work as the order routes are created matter. The first route that matches will be the one used to fulfill the request.