-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[5.4] Allow routes to be registered fluently #14354
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c6ebfd3
Remove request parameter. Unnecessary to run route.
JosephSilber f3ae7e4
Rename the router's middleware method to middlewareAlias
JosephSilber 32195c5
Allow routes to be registered fluently
JosephSilber f911e47
Add middleware registration to resources
JosephSilber ee245fe
Add name alias to RouteRegistrar
JosephSilber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
|
||
namespace Illuminate\Routing; | ||
|
||
use Closure; | ||
|
||
class RouteRegistrar | ||
{ | ||
/** | ||
* The router instance. | ||
* | ||
* @var \Illuminate\Routing\Router | ||
*/ | ||
protected $router; | ||
|
||
/** | ||
* The attributes to pass on to the router. | ||
* | ||
* @var array | ||
*/ | ||
protected $attributes = []; | ||
|
||
/** | ||
* The methods to dynamically pass through to the router. | ||
* | ||
* @var array | ||
*/ | ||
protected $passthru = [ | ||
'get', 'post', 'put', 'patch', 'delete', 'options', 'any', | ||
]; | ||
|
||
/** | ||
* The attributes that are aliased. | ||
* | ||
* @var array | ||
*/ | ||
protected $aliases = [ | ||
'name' => 'as', | ||
]; | ||
|
||
/** | ||
* Create a new route registrar instance. | ||
* | ||
* @param \Illuminate\Routing\Router $router | ||
* @return void | ||
*/ | ||
public function __construct(Router $router) | ||
{ | ||
$this->router = $router; | ||
} | ||
|
||
/** | ||
* Set the value for a given attribute. | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @return $this | ||
*/ | ||
public function attribute($key, $value) | ||
{ | ||
$this->attributes[array_get($this->aliases, $key, $key)] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Route a resource to a controller. | ||
* | ||
* @param string $name | ||
* @param string $controller | ||
* @param array $options | ||
* @return void | ||
*/ | ||
public function resource($name, $controller, array $options = []) | ||
{ | ||
$this->router->resource($name, $controller, $this->attributes + $options); | ||
} | ||
|
||
/** | ||
* Create a route group with shared attributes. | ||
* | ||
* @param \Closure $callback | ||
* @return void | ||
*/ | ||
public function group($callback) | ||
{ | ||
$this->router->group($this->attributes, $callback); | ||
} | ||
|
||
/** | ||
* Register a new route with the given verbs. | ||
* | ||
* @param array|string $methods | ||
* @param string $uri | ||
* @param \Closure|array|string|null $action | ||
* @return \Illuminate\Routing\Route | ||
*/ | ||
public function match($methods, $uri, $action = null) | ||
{ | ||
return $this->router->match($methods, $uri, $this->compileAction($action)); | ||
} | ||
|
||
/** | ||
* Register a new route with the router. | ||
* | ||
* @param string $method | ||
* @param string $uri | ||
* @param \Closure|array|string|null $action | ||
* @return \Illuminate\Routing\Route | ||
*/ | ||
protected function registerRoute($method, $uri, $action = null) | ||
{ | ||
if (! is_array($action)) { | ||
$action = array_merge($this->attributes, $action ? ['uses' => $action] : []); | ||
} | ||
|
||
return $this->router->{$method}($uri, $this->compileAction($action)); | ||
} | ||
|
||
/** | ||
* Compile the action into an array including the attributes. | ||
* | ||
* @param \Closure|array|string|null $action | ||
* @return array | ||
*/ | ||
protected function compileAction($action) | ||
{ | ||
if (is_null($action)) { | ||
return $this->attributes; | ||
} | ||
|
||
if (is_string($action) || $action instanceof Closure) { | ||
$action = ['uses' => $action]; | ||
} | ||
|
||
return array_merge($this->attributes, $action); | ||
} | ||
|
||
/** | ||
* Dynamically handle calls into the route registrar. | ||
* | ||
* @param string $method | ||
* @param array $parameters | ||
* @return \Illuminate\Routing\Route|$this | ||
*/ | ||
public function __call($method, $parameters) | ||
{ | ||
if (in_array($method, $this->passthru)) { | ||
return $this->registerRoute($method, ...$parameters); | ||
} | ||
|
||
return $this->attribute($method, $parameters[0]); | ||
} | ||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be done like here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rkgrep why???
The resource registrar has a lot of logic as to how it registers everything, so in some cases it makes sense to want to override it.
This route registrar serves the singular purpose of collecting route configuration before the route is actually created. Doesn't make much sense to override it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, perhaps I made a hasty decision on that )