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
Route names must be unique #9237
Conversation
@@ -77,6 +77,12 @@ public function add(Route $route, array $options = []) | |||
|
|||
// Explicit names | |||
if (isset($options['_name'])) { | |||
if(isset($this->_named[$options['_name']])){ |
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.
Expected 1 space after IF keyword; 0 found
Expected "if (...) {\n"; found "if(...){\n"
Expected 1 space after closing parenthesis; found 0
However I am not sure I agree. E.g. I was yesterday planning a flexible (plugin-ized) app in my head and thought "okay why not route all without AND with plugin prefixes, if there is a collision (non unique routes) the first route will match. The later defined routes will go with plugin prefixes. So the docs should clearly state how to catch the Exception to allow this. And/or there should be some way to allow collisions (priority then taking effect) |
Current coverage is 94.93% (diff: 83.33%)@@ master #9237 diff @@
==========================================
Files 369 410 +41
Lines 27035 28030 +995
Methods 3241 3357 +116
Messages 0 0
Branches 0 0
==========================================
+ Hits 25656 26610 +954
- Misses 1379 1420 +41
Partials 0 0
|
@@ -77,6 +77,12 @@ public function add(Route $route, array $options = []) | |||
|
|||
// Explicit names | |||
if (isset($options['_name'])) { | |||
if (isset($this->_named[$options['_name']])) { | |||
throw new MissingRouteException([ |
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.
This would no be a missing route exception, I think
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.
DuplicateRouteException
?
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.
@lorenzo
there is not something else,i agree with you
or should be add a new exception into Core
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.
A new exception sounds better
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.
Ye, that's what I meant by my comment above. Sorry for not being clear
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.
Also tests please 😄
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.
DuplicateRouteException is good?
Or other name?
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.
On a second thought I think that name can be confusing...
DuplicateRouteNameException
is more clear
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.
This case: DuplicateNamedRouteException
If in general the Router should become more strict now allowing to declare the same Route twice DuplicateRouteException
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.
👍 to DuplicateNamedRouteException
.
@@ -15,6 +15,7 @@ | |||
namespace Cake\Routing; | |||
|
|||
use Cake\Routing\Exception\MissingRouteException; | |||
use Cake\Routing\Exception\DuplicateNamedRouteException; |
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.
Use classes must be in alphabetical order. Was expecting Cake\Routing\Exception\MissingRouteException
if (isset($this->_named[$options['_name']])) { | ||
throw new DuplicateNamedRouteException([ | ||
'url' => $options['_name'], | ||
'message' => 'A named route was found for "%s" that is already used, route names must be unique across your entire application.' |
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.
Do you need the message in both places? If we don't need both copies of the message, can we remove one?
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.
Yes , you can
foreach (Router::routes() as $route): | ||
if(isset($route->options['_name']) && $url===$route->options['_name']){ | ||
echo '<tr class="error">'; | ||
}else{ |
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.
You're missing spaces around else
and if
here. Also try to avoid using a mix of {
and alternative-syntax.
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.
Do we already have a code guideline for what/when to use alternative syntax in template files (such as, use regular syntax at the head if you pre-process a lot of data, use alternative syntax in the body of a template only?)
Does it make sense to add such a guideline if it is not there?
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.
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected $_messageTemplate = 'A named route was found for "%s" that is already used, route names must be unique across your entire application.'; |
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.
Can this be made shorter? My suggestion:
Named routes must be unique; duplicate found ("%s").
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.
Duplicate named route "%s" but must be unique.
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.
@jadb maybe,but big message can help to developer even if found that in error.log file
some times developers will added route runtime(when site is run) and big message can helpful to that
@markstory please see new commit
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.
Am not against long ones, neither am I an expert in english. but "was found" and "is already used" mean the same thing, no? And "across your entire application", not sure is necessary.
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'd prefer a long message that contains what are the 2 conflicting routes. If we don't tell the user what is the other route with the same name, then we are not really helping solve the issue
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.
Duplicate named route x conlicts with y?
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.
Would displaying the template of the conflicting route be enough context for folks?
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.
Isn't this for named routes only? Then I'd expect the name, no? Or is that what you mean by "template" @markstory?
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.
The template of a route would be something like /articles/:year/:month/*
. It is the first argument to connect()
.
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 it would need to show the template of both conflicting routes.
} | ||
foreach (Router::routes() as $route) : | ||
if (isset($route->options['_name']) && $url === $route->options['_name']) : | ||
echo '<tr class="error">'; |
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.
@lorenzo conflicting named routes show with this line
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.
Would't it be better to have that information in the exception object instead of fetching it dynamically 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.
how?
currently we tell that with "%s" in message
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.
You can pass the info to the exception constructor or with a setter.
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'm sorry
I did not understand what you mean
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've moved the duplicate route into a table all of its own. I don't think we need the entire route list when we know which one is the duplicate.
Throw exceptions when duplicate named routes are connected. Refs #9237
Instead of relying on row highlighting, bubble the duplicate route into its own table at the top. Refs #9237
Merged into master in a7ac286 |
No description provided.