laravel-4 – Extender Resource Controllers

Question:

I'm doing what I want in a way and I'm looking for alternatives or better ways to do it. I'm using Resource Controllers in my application. I'm also using softdelete on several models, so my routes look like this:

Route::get('users/deleted', array('uses' => 'UserController@trash'));
Route::put('users/{id}/restore', array('uses' => 'UserController@restore'));
Route::resource('users', 'UserController');

The first route is to display the deleted ones. The second one allows me to restore these deleted elements. The third maps the traditional methods (create, edit, update etc).

What happens is that since I have several controllers that work exactly the same way, I would like to know if there is any way to tell laravel that when I have a Resource Controller, the restore and trash methods work on requests in the patterns above.

It's possible? Or can you do it better than what I'm doing?

Answer:

Based on this Laravel forum post you can solve this.

class MyRouter extends \Laravel\Routing\Router {}

app/config/app.php

'aliases'   => array( 
//--//
Route'  => 'MyRoute'
//--//
);

I haven't tested the solution to see if it works 100%, but I believe you can create all the methods you want this way.

Scroll to Top