Last active
January 8, 2021 10:36
-
-
Save jayminpanchal/368112003c778a0cda3ede37fac4385a to your computer and use it in GitHub Desktop.
Laravel Custom Resource Registrar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Illuminate\Routing\ResourceRegistrar; | |
use Illuminate\Support\Str; | |
class AppResourceRegistrar extends ResourceRegistrar | |
{ | |
// add data to the array | |
/** | |
* The default actions for a resourceful controller. | |
* | |
* @var array | |
*/ | |
protected $resourceDefaults = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy', 'deleted']; | |
public function register($name, $controller, array $options = []) | |
{ | |
if (isset($options['parameters']) && !isset($this->parameters)) { | |
$this->parameters = $options['parameters']; | |
} | |
// If the resource name contains a slash, we will assume the developer wishes to | |
// register these resource routes with a prefix so we will set that up out of | |
// the box so they don't have to mess with it. Otherwise, we will continue. | |
if (Str::contains($name, '/')) { | |
$this->prefixedResource($name, $controller, $options); | |
return; | |
} | |
// We need to extract the base resource from the resource name. Nested resources | |
// are supported in the framework, but we need to know what name to use for a | |
// place-holder on the route parameters, which should be the base resources. | |
$base = $this->getResourceWildcard(last(explode('.', $name))); | |
$defaults = $this->resourceDefaults; | |
foreach ($this->getResourceMethods($defaults, $options) as $m) { | |
$this->addWheres( | |
$this->{'addResource' . ucfirst($m)}($name, $base, $controller, $options), | |
$options | |
); | |
} | |
} | |
/** | |
* Add any parameter patterns to the route. | |
* | |
* @param \Illuminate\Routing\Route $route | |
* @param array $options | |
* @return \Illuminate\Routing\Route | |
*/ | |
protected function addWheres($route, array $options = []) | |
{ | |
if (empty($options['where'])) { | |
return $route; | |
} | |
if (isset($this->wheres)) { | |
$wheres = $this->wheres; | |
} else { | |
$wheres = $options['where']; | |
$keys = array_map(function ($item) { | |
return $this->getResourceWildcard($item); | |
}, array_keys($wheres)); | |
$this->wheres = $wheres = array_combine($keys, $wheres); | |
} | |
return $route->where($wheres); | |
} | |
protected function addResourceDeleted($name, $base, $controller, $options) | |
{ | |
$uri = $this->getResourceUri($name) . '/deleted'; | |
$action = $this->getResourceAction($name, $controller, 'deleted', $options); | |
return $this->router->get($uri, $action); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function boot() { | |
$registrar = new AppResourceRegistrar($this->app['router']); | |
$this->app->bind('Illuminate\Routing\ResourceRegistrar', function () use ($registrar) { | |
return $registrar; | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Route::resource('demo', 'DemoController', ['where' => ['demo' => '[0-9]+']]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful, thanks.