Skip to content

Instantly share code, notes, and snippets.

@acairns
Last active August 29, 2015 14:05
Show Gist options
  • Save acairns/fc75cd171022a865e1cc to your computer and use it in GitHub Desktop.
Save acairns/fc75cd171022a865e1cc to your computer and use it in GitHub Desktop.
Abstract for Resource Filter
<?php
class FooFilter extends ResourceFilter
{
public function index()
{
// Custom filtering for index();
}
public function store()
{
// Custom filtering for store();
}
public function show($id)
{
// Access to $id, just like the controller
}
}
<?php
use Illuminate\Routing\Router;
abstract class ResourceFilter
{
protected $router;
public function __construct(Router $router)
{
$this->router = $router;
}
public function filter()
{
$action = $this->currentAction();
$params = $this->currentParams();
if (method_exists($this, $action)) {
return call_user_func_array(array($this, $action), $params);
}
}
private function currentAction()
{
$route = $this->router->currentRouteAction();
return substr($route, strpos($route, "@") + 1);
}
private function currentParams()
{
return $this->router->current()->parameters();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment