1 | <?php |
||
8 | class RouteRegistrar |
||
9 | { |
||
10 | /** |
||
11 | * @var Router |
||
12 | */ |
||
13 | protected $router; |
||
14 | |||
15 | /** |
||
16 | * @var string controller to handle generic resources |
||
17 | */ |
||
18 | protected $controller; |
||
19 | |||
20 | /** |
||
21 | * Create a new RouteRegistrar instance. |
||
22 | * |
||
23 | * @param Router $router |
||
24 | * @param string $controller |
||
25 | */ |
||
26 | public function __construct(Router $router, $controller) |
||
31 | |||
32 | /** |
||
33 | * Add the resourceful routes for the given uri and resource. |
||
34 | * |
||
35 | * @param string $uri |
||
36 | * @param string $resource |
||
37 | * @param array $options |
||
38 | */ |
||
39 | public function addRoute($uri, $resource, array $options = []) |
||
40 | { |
||
41 | // The $router->resource() method doesn't allow custom route attributes |
||
42 | // in the $options array. So, while the group() may look redundant here, |
||
43 | // we need it to attach the relevant resource classname to each of the |
||
44 | // individual restful routes being defined. |
||
45 | // This is so when we come to resolve the controller, we |
||
46 | // can easily tell what type of resource we need, i.e. which model. |
||
47 | $this->router->group( |
||
48 | $this->groupOptions($resource, $options), |
||
49 | function($router) use ($uri, $options) { |
||
50 | |||
51 | $router->resource( |
||
52 | $uri, |
||
53 | $this->controller, |
||
54 | $this->routeOptions($options) |
||
55 | ); |
||
56 | |||
57 | $router->put($uri, $this->controller.'@updateAll'); |
||
58 | $router->delete($uri, $this->controller.'@destroyAll'); |
||
59 | } |
||
60 | ); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Get the options for the resource route. |
||
65 | * |
||
66 | * @param array $options |
||
67 | * @return array |
||
68 | */ |
||
69 | protected function routeOptions(array $options) |
||
78 | |||
79 | /** |
||
80 | * Get the options for the route group. |
||
81 | * |
||
82 | * @param string $resource |
||
83 | * @param array $options |
||
84 | * @return array |
||
85 | */ |
||
86 | protected function groupOptions($resource, array $options) |
||
90 | |||
91 | /** |
||
92 | * Get the name of the resource currently being accessed. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | public function getCurrentResource() |
||
104 | } |
||
105 |