Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@
"laravel": {
"providers": [
"Backpack\\CRUD\\CrudServiceProvider"
],
"aliases": {
"CRUD": "Backpack\\CRUD\\CrudServiceProvider"
}
]
}
}
}
144 changes: 0 additions & 144 deletions src/CrudRouter.php

This file was deleted.

42 changes: 40 additions & 2 deletions src/CrudServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Backpack\CRUD;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

class CrudServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -88,6 +89,12 @@ public function register()
return new CRUD($app);
});

// load a macro for Route,
// for developers to be able to load all routes for a CRUD resource in one line
if (! Route::hasMacro('crud')) {
$this->addRouteMacro();
}

// register the helper functions
$this->loadHelpers();

Expand All @@ -100,9 +107,40 @@ public function register()
}
}

public static function resource($name, $controller, array $options = [])
/**
* The route macro allows developers to generate the routes for a CrudController,
* for all operations, using a simple syntax: Route::crud().
*
* It will go to the given CrudController and get the routes() method on it.
*/
private function addRouteMacro()
{
return new CrudRouter($name, $controller, $options);
Route::macro('crud', function ($name, $controller) {
// put together the route name prefix,
// as passed to the Route::group() statements
$routeName = '';
if ($this->hasGroupStack()) {
foreach ($this->getGroupStack() as $key => $groupStack) {
if (isset($groupStack['name'])) {
if (is_array($groupStack['name'])) {
$routeName = implode('', $groupStack['name']);
} else {
$routeName = $groupStack['name'];
}
}
}
}
// add the name of the current entity to the route name prefix
// the result will be the current route name (not ending in dot)
$routeName .= $name;

// get an instance of the controller
$groupStack = $this->hasGroupStack() ? $this->getGroupStack()[0]['namespace'].'\\' : 'App\\';
$namespacedController = $groupStack.$controller;
$controllerInstance = new $namespacedController;

return $controllerInstance->routes($name, $routeName, $controller);
});
}

/**
Expand Down
29 changes: 19 additions & 10 deletions src/app/Http/Controllers/CrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,10 @@
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Backpack\CRUD\app\Http\Controllers\Operations\SaveActions;
use Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
use Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
use Backpack\CRUD\app\Http\Controllers\Operations\CloneOperation;
use Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
use Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
use Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
use Backpack\CRUD\app\Http\Controllers\Operations\ReorderOperation;
use Backpack\CRUD\app\Http\Controllers\Operations\RevisionsOperation;

class CrudController extends BaseController
{
use DispatchesJobs, ValidatesRequests;
use CreateOperation, CloneOperation, DeleteOperation, ListOperation, ReorderOperation, RevisionsOperation, SaveActions, ShowOperation, UpdateOperation;

public $data = [];
public $request;
Expand Down Expand Up @@ -53,4 +43,23 @@ public function __construct()
public function setup()
{
}

/**
* Load routes for all operations.
* Allow developers to load extra routes by creating a method that starts with setupRoutesFor.
*
* @param string $segment Name of the current entity (singular).
* @param string $routeName Route name prefix (ends with .).
* @param string $controller Name of the current controller.
*/
public function routes($segment, $routeName, $controller)
{
preg_match_all('/(?<=^|;)setup([^;]+?)Routes(;|$)/', implode(';', get_class_methods($this)), $matches);

if (count($matches[1])) {
foreach ($matches[1] as $methodName) {
$this->{'setup'.$methodName.'Routes'}($segment, $routeName, $controller);
}
}
}
}
47 changes: 47 additions & 0 deletions src/app/Http/Controllers/Operations/BulkCloneOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Backpack\CRUD\app\Http\Controllers\Operations;

use Illuminate\Support\Facades\Route;

trait BulkCloneOperation
{
/**
* Define which routes are needed for this operation.
*
* @param string $segment Name of the current entity (singular). Used as first URL segment.
* @param string $routeName Prefix of the route name.
* @param string $controller Name of the current CrudController.
*/
protected function setupBulkCloneRoutes($segment, $routeName, $controller)
{
Route::post($segment.'/bulk-clone', [
'as' => $routeName.'.bulkClone',
'uses' => $controller.'@bulkClone',
]);
}

/**
* Create duplicates of multiple entries in the datatabase.
*
* @param int $id
*
* @return Response
*/
public function bulkClone()
{
$this->crud->hasAccessOrFail('clone');
$this->crud->setOperation('clone');

$entries = $this->request->input('entries');
$clonedEntries = [];

foreach ($entries as $key => $id) {
if ($entry = $this->crud->model->find($id)) {
$clonedEntries[] = $entry->replicate()->push();
}
}

return $clonedEntries;
}
}
45 changes: 45 additions & 0 deletions src/app/Http/Controllers/Operations/BulkDeleteOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Backpack\CRUD\app\Http\Controllers\Operations;

use Illuminate\Support\Facades\Route;

trait BulkDeleteOperation
{
/**
* Define which routes are needed for this operation.
*
* @param string $segment Name of the current entity (singular). Used as first URL segment.
* @param string $routeName Prefix of the route name.
* @param string $controller Name of the current CrudController.
*/
protected function setupBulkDeleteRoutes($segment, $routeName, $controller)
{
Route::post($segment.'/bulk-delete', [
'as' => $routeName.'.bulkDelete',
'uses' => $controller.'@bulkDelete',
]);
}

/**
* Delete multiple entries in one go.
*
* @return string
*/
public function bulkDelete()
{
$this->crud->hasAccessOrFail('delete');
$this->crud->setOperation('delete');

$entries = $this->request->input('entries');
$deletedEntries = [];

foreach ($entries as $key => $id) {
if ($entry = $this->crud->model->find($id)) {
$deletedEntries[] = $entry->delete();
}
}

return $deletedEntries;
}
}
Loading