Skip to content

Instantly share code, notes, and snippets.

@lagbox
Created March 5, 2016 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lagbox/dcffd8367d51cc52aa85 to your computer and use it in GitHub Desktop.
Save lagbox/dcffd8367d51cc52aa85 to your computer and use it in GitHub Desktop.
Adding where conditions to resource parameters
<?php
public function register($name, $controller, array $options = [])
{
...
// 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment