Last active
August 29, 2015 14:05
-
-
Save acairns/fc75cd171022a865e1cc to your computer and use it in GitHub Desktop.
Abstract for Resource Filter
This file contains 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
<?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 | |
} | |
} |
This file contains 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
<?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