@@ -20,13 +20,6 @@ class Router extends \Illuminate\Routing\Router {
2020 */
2121 protected $ apiCollections = [];
2222
23- /**
24- * Indicates if newly added routes are API routes.
25- *
26- * @var bool
27- */
28- protected $ apiRouting = false ;
29-
3023 /**
3124 * The default API version.
3225 *
@@ -48,6 +41,13 @@ class Router extends \Illuminate\Routing\Router {
4841 */
4942 protected $ exceptionHandler ;
5043
44+ /**
45+ * Array of cached API requests.
46+ *
47+ * @var array
48+ */
49+ protected $ cachedApiRequests = [];
50+
5151 /**
5252 * Register an API group.
5353 *
@@ -62,21 +62,19 @@ public function api($options, Closure $callback)
6262 throw new BadMethodCallException ('Unable to register API without an API version. ' );
6363 }
6464
65- $ this ->enableApiRouting ();
66-
6765 // Once we have the version from the options we'll check to see if an API
6866 // collection already exists for this verison. If it doesn't then we'll
6967 // create a new collection.
7068 $ version = $ options ['version ' ];
7169
70+ $ options ['api ' ] = true ;
71+
7272 if ( ! isset ($ this ->apiCollections [$ version ]))
7373 {
7474 $ this ->apiCollections [$ version ] = $ this ->newApiCollection ($ version , array_except ($ options , ['version ' ]));
7575 }
7676
7777 $ this ->group ($ options , $ callback );
78-
79- $ this ->disableApiRouting ();
8078 }
8179
8280 /**
@@ -107,36 +105,23 @@ public function dispatch(Request $request)
107105 // If an exception is caught and we are currently routing an API request then
108106 // we'll handle this exception by building a new response from it. This
109107 // allows the API to gracefully handle its own exceptions.
110- if (( $ this ->routingForApi () or $ this -> requestTargettingApi ($ request) ) and ! $ request instanceof InternalRequest)
108+ if ($ this ->requestTargettingApi ($ request ) and ! $ request instanceof InternalRequest)
111109 {
112110 $ response = $ this ->handleException ($ exception );
113111 }
114112
115113 // If the request was an internal request then we will rethrow the exception
116114 // so that developers can easily catch them and adjust ther esponse
117- // themselves. We also disable API routing so that the parent
118- // request isn't treated as an API request.
115+ // themselves.
119116 else
120117 {
121- $ this ->disableApiRouting ();
122-
123118 throw $ exception ;
124119 }
125120 }
126121
127- if ($ this ->routingForApi () or $ this -> requestTargettingApi ($ request ))
122+ if ($ this ->requestTargettingApi ($ request ))
128123 {
129124 $ response = Response::makeFromExisting ($ response )->morph ();
130-
131- // If the request that was dispatched is an internal request then we need to
132- // disable the API routing so that the parent request is not treated in
133- // the same way. This prevents it from generating an Api Response for
134- // the parent request. Another internal request will still result
135- // in API routing being enabled.
136- if ($ request instanceof InternalRequest)
137- {
138- $ this ->disableApiRouting ();
139- }
140125 }
141126
142127 return $ response ;
@@ -186,7 +171,7 @@ protected function addRoute($methods, $uri, $action)
186171 {
187172 $ route = $ this ->createRoute ($ methods , $ uri , $ action );
188173
189- if ($ this ->routingForApi ())
174+ if ($ this ->routingForApi ($ route ))
190175 {
191176 $ route ->before ('api ' );
192177
@@ -226,7 +211,7 @@ protected function createRoute($methods, $uri, $action)
226211 // see if the controller is one of the API controllers. If it is then we
227212 // need to check if the method is protected and get any scopes
228213 // associated with the method.
229- if ($ this ->routingForApi () and $ this ->routingToController ($ action ))
214+ if ($ this ->routingForApi ($ route ) and $ this ->routingToController ($ action ))
230215 {
231216 list ($ class , $ method ) = explode ('@ ' , $ route ->getActionName ());
232217
@@ -317,7 +302,7 @@ protected function controllerMethodProtected($route, $controller, $method)
317302 */
318303 protected function findRoute ($ request )
319304 {
320- if ($ this ->routingForApi () or $ this -> requestTargettingApi ($ request ))
305+ if ($ this ->requestTargettingApi ($ request ))
321306 {
322307 $ version = $ this ->getRequestVersion ($ request );
323308
@@ -345,12 +330,22 @@ protected function findRoute($request)
345330 */
346331 public function requestTargettingApi ($ request )
347332 {
348- if (empty ($ this ->apiCollections )) return false ;
333+ if (empty ($ this ->apiCollections ))
334+ {
335+ return false ;
336+ }
349337
350- return array_first ( $ this -> apiCollections , function ( $ key , $ value ) use ( $ request )
338+ if ( in_array ( $ request , $ this -> cachedApiRequests ) )
351339 {
352- return $ value ->matches ($ request );
340+ return true ;
341+ }
342+
343+ $ collection = array_first ($ this ->apiCollections , function ($ key , $ collection ) use ($ request )
344+ {
345+ return $ collection ->matches ($ request );
353346 }, false );
347+
348+ return $ collection instanceof ApiCollection;
354349 }
355350
356351 /**
@@ -388,37 +383,16 @@ public function getApiCollection($version)
388383 }
389384
390385 /**
391- * Determine if the current request is an API request .
386+ * Determine if a route is an API route .
392387 *
388+ * @param \Illuminate\Routing\Route
393389 * @return bool
394390 */
395- public function routingForApi ()
396- {
397- return $ this ->apiRouting ;
398- }
399-
400- /**
401- * Enable API request.
402- *
403- * @return \Dingo\Api\Routing\Router
404- */
405- public function enableApiRouting ()
391+ public function routingForApi ($ route )
406392 {
407- $ this ->apiRouting = true ;
408-
409- return $ this ;
410- }
411-
412- /**
413- * Disable API request.
414- *
415- * @return \Dingo\Api\Routing\Router
416- */
417- public function disableApiRouting ()
418- {
419- $ this ->apiRouting = false ;
393+ $ action = $ route ->getAction ();
420394
421- return $ this ;
395+ return isset ( $ action [ ' api ' ]) and $ action [ ' api ' ] === true ;
422396 }
423397
424398 /**
0 commit comments