-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Conversation
/cc @hipsterjazzbo |
@JosephSilber Did you want any particular comments from me? Cause my first impressions are:
|
return $this->macroCall($method, $parameters); | ||
} | ||
|
||
return (new RouteRegistrar($this))->attribute($method, $parameters[0]); |
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 )
633c8eb
to
02a0eea
Compare
16f7631
to
1811f96
Compare
1811f96
to
96d4375
Compare
96d4375
to
608c4bb
Compare
608c4bb
to
36f6973
Compare
36f6973
to
ee245fe
Compare
@JosephSilber Looks very nice. Though just out of curiosity how come there should be another class? Would it be more simple to have this contained in the Router class? That way you wouldn't need:
|
@garygreen what would those methods return? Take this example: Route::name('users.index')->middleware('auth')->get('users', function () {
// some closure action...
}); What class is that |
middleware would be a problem.. how would you call the existing |
@garygreen I'm not following what you're saying. If we want to be able to chain those methods, then the calls to What would you have it return instead? |
All I'm curious about is why there is need for a class for |
@garygreen I don't understand what you're saying, and we seem to be going in circles. Would you mind whipping up an example of the implementation you have in mind, so that we can look at actual code? Pseudo code should be enough. I think that way I'd have an easier time understanding this from your point of view. |
@JosephSilber super quick code just as an example of what I'm going on about. Sorry for not being clear 😄 <?php namespace Illuminate\Routing\Router;
class Router {
/**
* The attributes to pass on to the router.
*
* @var array
*/
protected $attributes = [];
protected $passthruAttributes = [
'name', 'as', 'middleware', ...
];
/**
* Create a new route instance.
*
* @param array|string $methods
* @param string $uri
* @param mixed $action
* @return \Illuminate\Routing\Route
*/
protected function createRoute($methods, $uri, $action)
{
// Existing code...
// In here call the attributes on the route.
return $route;
}
public function middlewareAlias($name, $middleware) // Renamed from middleware() so the magic method gets called and middleware passed onto route.
{
//...
}
/**
* Dynamically handle calls into the route registrar.
*
* @param string $method
* @param array $parameters
* @return $this|void
*/
public function __call($method, $parameters)
{
if (in_array($this->passthruAttributes, $method))
{
$this->attribute($method, $parameters[0]);
return $this;
}
}
} |
What is that You could reset it after every call to |
That's kind of how I thought this was working, I didn't realise it was creating a new |
Holding off on this for now. |
This enables stuff like:
...so that the calls to
name
andmiddleware
are not hanging off the end of the closure.Also, you can now use the
middleware
method with groups:Finally, you can now register middleware for resources directly: