diff --git a/src/CrudRouter.php b/src/CrudRouter.php index 1573422636..f46e78380c 100644 --- a/src/CrudRouter.php +++ b/src/CrudRouter.php @@ -68,6 +68,11 @@ public function __construct($name, $controller, $options) 'as' => 'crud.'.$this->name.'.bulkClone', 'uses' => $this->controller.'@bulkClone', ]); + + // catch-all route for custom operations + Route::any($this->name.'/do/{function}', $this->controller.'@callPublicFunction') + ->where('function', '(.*)') + ->name('crud.'.$this->name); } /** diff --git a/src/app/Http/Controllers/CrudController.php b/src/app/Http/Controllers/CrudController.php index 3200deb491..11ae80ad58 100644 --- a/src/app/Http/Controllers/CrudController.php +++ b/src/app/Http/Controllers/CrudController.php @@ -2,6 +2,7 @@ namespace Backpack\CRUD\app\Http\Controllers; +use ReflectionMethod; use Backpack\CRUD\CrudPanel; use Illuminate\Http\Request; use Illuminate\Foundation\Bus\DispatchesJobs; @@ -47,6 +48,25 @@ public function __construct() } } + public function callPublicFunction(...$params) + { + $parameters = func_get_args()[0]; + $httpVerb = \Request::method(); + $input = \Request::input(); + $functionName = explode('/', $parameters)[0]; + $functionExists = method_exists($this, $functionName); + $functionIsPublic = false; + + if ($functionExists) { + $reflection = new ReflectionMethod($this, $functionName); + $functionIsPublic = $reflection->isPublic(); + } + + if ($functionIsPublic) { + return $this->{$functionName}(...$params); + } + } + /** * Allow developers to set their configuration options for a CrudPanel. */