laravel-actions icon indicating copy to clipboard operation
laravel-actions copied to clipboard

Ability to define resourceful Action routes

Open CWAscend opened this issue 4 months ago • 7 comments

I absolutely love this package, but the only thing I don't like is having to declare all of my Action routes individually, and losing the ability to define resourceful routes.

This PR attempts to achieve this, by leveraging the Route::resource() function and swaping out the Controller@action for the action class.

Example usage:

// By default, it will look for all actions in the App\Actions namespace
Route::actions('addresses');

// If we only want to to generate index/show routes
Route::actions('addresses')->only('index', 'show');

// If our action is not in the default namespace, we can specify the namespace
Route::actions('addresses', 'App\Actions\Addresses');

// Because we are leveraging the Route::resource() method, all other helpers are readily
// available for us to chain on to
Route::actions('addresses')->only('index')->middleware('signed');

The benefit of this is that it will force developers to have consistent routing AND consistent Action naming. Now the names of these are up for grabs, but I've gone for:

  • GetAddresses - this replaces Controller@index
  • ShowCreateAddress - this replaces Controller@create
  • ShowAddress - this replaces Controller@show
  • ShowEditAddress - this replaces Controller@edit
  • CreateAddress - this replaces Controller@store
  • UpdateAddress - this replaces Controller@update
  • DeleteAddress - this replaces Controller@destroy

Example - the following two route declarations:

Route::actions('addresses');
Route::actions('order-items');

produces the following routes:

image

As I mentioned above, if the Action is under a different namespace, we can specify that:

Route::actions('orders', 'App\Actions\Order')->only(['show', 'store']);

which produces:

image

CWAscend avatar Oct 16 '24 00:10 CWAscend