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

Route names must be unique #9237

Closed
wants to merge 6 commits into from
Closed

Route names must be unique #9237

wants to merge 6 commits into from

Conversation

saeideng
Copy link
Member

@saeideng saeideng commented Aug 9, 2016

No description provided.

@@ -77,6 +77,12 @@ public function add(Route $route, array $options = [])

// Explicit names
if (isset($options['_name'])) {
if(isset($this->_named[$options['_name']])){
Copy link
Contributor

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

@htstudios
Copy link
Contributor

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)

@codecov-io
Copy link

codecov-io commented Aug 9, 2016

Current coverage is 94.93% (diff: 83.33%)

Merging #9237 into master will increase coverage by 0.03%

@@             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          

Powered by Codecov. Last update 3d3e354...673550e

@@ -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([
Copy link
Member

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

Copy link
Contributor

Choose a reason for hiding this comment

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

DuplicateRouteException?

Copy link
Member Author

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

Copy link
Member

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

Copy link
Contributor

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

Copy link
Member

Choose a reason for hiding this comment

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

Also tests please 😄

Copy link
Member Author

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?

Copy link
Contributor

@Xymanek Xymanek Aug 9, 2016

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...
DuplicateRouteNameExceptionis more clear

Copy link
Contributor

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

Copy link
Member

Choose a reason for hiding this comment

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

👍 to DuplicateNamedRouteException.

@markstory markstory modified the milestones: 3.2.14, 3.3.1 Aug 13, 2016
@@ -15,6 +15,7 @@
namespace Cake\Routing;

use Cake\Routing\Exception\MissingRouteException;
use Cake\Routing\Exception\DuplicateNamedRouteException;
Copy link
Contributor

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.'
Copy link
Member

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?

Copy link
Member Author

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{
Copy link
Member

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.

Copy link
Contributor

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?

Copy link
Member

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.';
Copy link
Contributor

@jadb jadb Aug 14, 2016

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"). 

Copy link
Contributor

@htstudios htstudios Aug 14, 2016

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.

Copy link
Member Author

@saeideng saeideng Aug 15, 2016

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

Copy link
Contributor

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.

Copy link
Member

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

Copy link
Contributor

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?

Copy link
Member

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?

Copy link
Contributor

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?

Copy link
Member

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().

Copy link
Member

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.

@markstory markstory self-assigned this Aug 15, 2016
}
foreach (Router::routes() as $route) :
if (isset($route->options['_name']) && $url === $route->options['_name']) :
echo '<tr class="error">';
Copy link
Member Author

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

Copy link
Member

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?

Copy link
Member Author

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

Copy link
Member

@lorenzo lorenzo Aug 18, 2016

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.

Copy link
Member Author

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

Copy link
Member

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.

markstory added a commit that referenced this pull request Aug 19, 2016
Throw exceptions when duplicate named routes are connected.

Refs #9237
markstory added a commit that referenced this pull request Aug 19, 2016
Instead of relying on row highlighting, bubble the duplicate route
into its own table at the top.

Refs #9237
@markstory
Copy link
Member

Merged into master in a7ac286

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants