diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 108cc95d2..bff7f2bbb 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -92,11 +92,11 @@ class Router /** * Create a new router instance. * - * @param \Dingo\Api\Contract\Routing\Adapter $adapter - * @param \Dingo\Api\Contract\Debug\ExceptionHandler $exception - * @param \Illuminate\Container\Container $container - * @param string $domain - * @param string $prefix + * @param \Dingo\Api\Contract\Routing\Adapter $adapter + * @param \Dingo\Api\Contract\Debug\ExceptionHandler $exception + * @param \Illuminate\Container\Container $container + * @param string $domain + * @param string $prefix * * @return void */ @@ -117,9 +117,9 @@ public function __construct(Adapter $adapter, ExceptionHandler $exception, Conta * This method can be called without the third parameter, however, * the callback should always be the last parameter. * - * @param array|string $version - * @param array|callable $second - * @param callable $third + * @param array|string $version + * @param array|callable $second + * @param callable $third * * @return void */ @@ -139,8 +139,8 @@ public function version($version, $second, $third = null) /** * Create a new route group. * - * @param array $attributes - * @param callable $callback + * @param array $attributes + * @param callable $callback * * @return void */ @@ -176,8 +176,8 @@ public function group(array $attributes, $callback) /** * Create a new GET route. * - * @param string $uri - * @param array|string|callable $action + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -189,8 +189,8 @@ public function get($uri, $action) /** * Create a new POST route. * - * @param string $uri - * @param array|string|callable $action + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -202,8 +202,8 @@ public function post($uri, $action) /** * Create a new PUT route. * - * @param string $uri - * @param array|string|callable $action + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -215,8 +215,8 @@ public function put($uri, $action) /** * Create a new PATCH route. * - * @param string $uri - * @param array|string|callable $action + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -228,8 +228,8 @@ public function patch($uri, $action) /** * Create a new DELETE route. * - * @param string $uri - * @param array|string|callable $action + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -241,8 +241,8 @@ public function delete($uri, $action) /** * Create a new OPTIONS route. * - * @param string $uri - * @param array|string|callable $action + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -254,8 +254,8 @@ public function options($uri, $action) /** * Create a new route that responding to all verbs. * - * @param string $uri - * @param array|string|callable $action + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -269,9 +269,9 @@ public function any($uri, $action) /** * Create a new route with the given verbs. * - * @param array|string $methods - * @param string $uri - * @param array|string|callable $action + * @param array|string $methods + * @param string $uri + * @param array|string|callable $action * * @return mixed */ @@ -283,7 +283,7 @@ public function match($methods, $uri, $action) /** * Register an array of resources. * - * @param array $resources + * @param array $resources * * @return void */ @@ -303,9 +303,9 @@ public function resources(array $resources) /** * Register a resource controller. * - * @param string $name - * @param string $controller - * @param array $options + * @param string $name + * @param string $controller + * @param array $options * * @return void */ @@ -323,9 +323,9 @@ public function resource($name, $controller, array $options = []) /** * Add a route to the routing adapter. * - * @param string|array $methods - * @param string $uri - * @param string|array|callable $action + * @param string|array $methods + * @param string $uri + * @param string|array|callable $action * * @return mixed */ @@ -341,6 +341,14 @@ public function addRoute($methods, $uri, $action) $action = implode('@', $action); $action = ['uses' => $action, 'controller' => $action]; } + + // For named routes and syntax: + // $api->post('login', ['uses' => [LoginController::class, 'login'], 'as' => 'login'] + if (isset($action['uses']) && isset($action['as'])) { + $route_name = $action['as']; + $action = implode('@', $action['uses']); + $action = ['uses' => $action, 'controller' => $action, 'as' => $route_name]; + } } $action = $this->mergeLastGroupAttributes($action); @@ -363,7 +371,7 @@ public function addRoute($methods, $uri, $action) /** * Add the controller preparation middleware to the beginning of the routes middleware. * - * @param array $action + * @param array $action * * @return array */ @@ -377,7 +385,7 @@ protected function addControllerMiddlewareToRouteAction(array $action) /** * Merge the last groups attributes. * - * @param array $attributes + * @param array $attributes * * @return array */ @@ -393,8 +401,8 @@ protected function mergeLastGroupAttributes(array $attributes) /** * Merge the given group attributes. * - * @param array $new - * @param array $old + * @param array $new + * @param array $old * * @return array */ @@ -432,8 +440,8 @@ protected function mergeGroup(array $new, array $old) /** * Format an array based option in a route action. * - * @param string $option - * @param array $new + * @param string $option + * @param array $new * * @return array */ @@ -447,8 +455,8 @@ protected function formatArrayBasedOption($option, array $new) /** * Format the uses key in a route action. * - * @param array $new - * @param array $old + * @param array $new + * @param array $old * * @return string */ @@ -464,8 +472,8 @@ protected function formatUses(array $new, array $old) /** * Format the namespace for the new group attributes. * - * @param array $new - * @param array $old + * @param array $new + * @param array $old * * @return string */ @@ -483,8 +491,8 @@ protected function formatNamespace(array $new, array $old) /** * Format the prefix for the new group attributes. * - * @param array $new - * @param array $old + * @param array $new + * @param array $old * * @return string */ @@ -500,7 +508,7 @@ protected function formatPrefix($new, $old) /** * Dispatch a request via the adapter. * - * @param \Dingo\Api\Http\Request $request + * @param \Dingo\Api\Http\Request $request * * @throws \Exception * @@ -532,9 +540,9 @@ public function dispatch(Request $request) /** * Prepare a response by transforming and formatting it correctly. * - * @param mixed $response - * @param \Dingo\Api\Http\Request $request - * @param string $format + * @param mixed $response + * @param \Dingo\Api\Http\Request $request + * @param string $format * * @return \Dingo\Api\Http\Response */ @@ -574,7 +582,7 @@ protected function prepareResponse($response, Request $request, $format) /** * Gather the middleware for the given route. * - * @param mixed $route + * @param mixed $route * * @return array */ @@ -596,7 +604,7 @@ protected function requestIsConditional() /** * Set the conditional request. * - * @param bool $conditionalRequest + * @param bool $conditionalRequest * * @return void */ @@ -644,7 +652,7 @@ public function current() /** * Create a new route instance from an adapter route. * - * @param array|\Illuminate\Routing\Route $route + * @param array|\Illuminate\Routing\Route $route * * @return \Dingo\Api\Routing\Route */ @@ -656,7 +664,7 @@ public function createRoute($route) /** * Set the current route instance. * - * @param \Dingo\Api\Routing\Route $route + * @param \Dingo\Api\Routing\Route $route * * @return void */ @@ -694,7 +702,7 @@ public function getLastGroupPrefix() /** * Get all routes registered on the adapter. * - * @param string $version + * @param string $version * * @return mixed */ @@ -734,7 +742,7 @@ public function getAdapterRoutes() /** * Set the raw adapter routes. * - * @param array $routes + * @param array $routes * * @return void */ @@ -778,7 +786,7 @@ public function currentRouteName() /** * Alias for the "currentRouteNamed" method. * - * @param mixed string + * @param mixed string * * @return bool */ @@ -796,7 +804,7 @@ public function is() /** * Determine if the current route matches a given name. * - * @param string $name + * @param string $name * * @return bool */ @@ -842,7 +850,7 @@ public function uses() /** * Determine if the current route action matches a given action. * - * @param string $action + * @param string $action * * @return bool */