Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for controller input and method in an array with the n… #1730

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 67 additions & 59 deletions src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -283,7 +283,7 @@ public function match($methods, $uri, $action)
/**
* Register an array of resources.
*
* @param array $resources
* @param array $resources
*
* @return void
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -341,6 +341,14 @@ public function addRoute($methods, $uri, $action)
$action = implode('@', $action);
$action = ['uses' => $action, 'controller' => $action];
}

// For named routes and syntax:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't $action already be changed by the above ? So it won't be the same as in the routes file anymore ?

// $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);
Expand All @@ -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
*/
Expand All @@ -377,7 +385,7 @@ protected function addControllerMiddlewareToRouteAction(array $action)
/**
* Merge the last groups attributes.
*
* @param array $attributes
* @param array $attributes
*
* @return array
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand All @@ -596,7 +604,7 @@ protected function requestIsConditional()
/**
* Set the conditional request.
*
* @param bool $conditionalRequest
* @param bool $conditionalRequest
*
* @return void
*/
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -694,7 +702,7 @@ public function getLastGroupPrefix()
/**
* Get all routes registered on the adapter.
*
* @param string $version
* @param string $version
*
* @return mixed
*/
Expand Down Expand Up @@ -734,7 +742,7 @@ public function getAdapterRoutes()
/**
* Set the raw adapter routes.
*
* @param array $routes
* @param array $routes
*
* @return void
*/
Expand Down Expand Up @@ -778,7 +786,7 @@ public function currentRouteName()
/**
* Alias for the "currentRouteNamed" method.
*
* @param mixed string
* @param mixed string
*
* @return bool
*/
Expand All @@ -796,7 +804,7 @@ public function is()
/**
* Determine if the current route matches a given name.
*
* @param string $name
* @param string $name
*
* @return bool
*/
Expand Down Expand Up @@ -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
*/
Expand Down