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

Inconsistent syntax for creating links #2426

Closed
samdark opened this issue Feb 14, 2014 · 30 comments
Closed

Inconsistent syntax for creating links #2426

samdark opened this issue Feb 14, 2014 · 30 comments

Comments

@samdark
Copy link
Member

@samdark samdark commented Feb 14, 2014

// helpers
Html::a($text, ['/site/view', 'id' => 100]);
Html::url(['/site/view', 'id' => 100]);

// controller
$this->redirect(['/site/view', 'id' => 100]);
$this->createAbsoluteUrl('/site/view', ['id' => 100]);
$this->createUrl('/site/view', ['id' => 100]);

// url manager
Yii::$app->getUrlManager()->createUrl('/site/view', ['id' => 100]);
Yii::$app->getUrlManager()->createAbsoluteUrl('/site/view', ['id' => 100]);

So we have 2 signatures for URLs. It's either ($route, [$params]) or ([$route, $params]). I think we'd better choose one and use it consistently.

@slavcodev
Copy link
Contributor

@slavcodev slavcodev commented Feb 14, 2014

I agree, and vote for using array to route
echo Html::url(['/site/view', 'id' => 100]);

and string to relative url
echo Html::url('/images/photo.jpg'); instead of echo Yii::app()->baseUrl . '/images/photo.jpg'

@Mirocow
Copy link

@Mirocow Mirocow commented Feb 14, 2014

i agree with this issue

@Ragazzo
Copy link
Contributor

@Ragazzo Ragazzo commented Feb 14, 2014

i am for the second too.

@mgrechanik
Copy link
Contributor

@mgrechanik mgrechanik commented Feb 14, 2014

The current situation is all right with me.
We just need to remember that there are two formats:

  1. "classic" for UrlManager().
  2. "advanced" for Html::url() and all methods that rely upon Html::url() such as controller->redirect()

@slavcodev

and string to relative url ... instead of ...

Html::url() already works like that

@samdark
Copy link
Member Author

@samdark samdark commented Feb 14, 2014

@mgrechanik what about controller's redirect vs createUrl?

@mgrechanik
Copy link
Contributor

@mgrechanik mgrechanik commented Feb 14, 2014

controller's redirect could redirect to any urls when controller's createUrl is used for a different purpose (creating some very specific set of urls).
So if the signatures here are different it is not very bad.

@samdark
Copy link
Member Author

@samdark samdark commented Feb 14, 2014

The question is now how bad it is, the question is how to make it better.

@mgrechanik
Copy link
Contributor

@mgrechanik mgrechanik commented Feb 14, 2014

Changing controller's method's signatures to ([$route, $params]) (as people above proposed) won't make it better at all because controller->crealeUrl will differ too much from UrlManager->crealeUrl.

As I see the question is mainly about redirect method, is it not?
What if teach it to accept both formats?

    public function redirect($url, $params = [], $statusCode = 302)
    {
        $url = (empty($params) || !is_string($url)) ? Html::url($url) : $this->createUrl($url, $params);
        //var_dump($url);
        return Yii::$app->getResponse()->redirect($url, $statusCode);
    }   

@samdark
Copy link
Member Author

@samdark samdark commented Feb 14, 2014

@mgrechanik well, it's about unifying ALL signatures. Leaving some with different format doesn't make sense.

@qiangxue
Copy link
Member

@qiangxue qiangxue commented Feb 14, 2014

I'm fine with unifying the format to be ['route', 'name1' => 'value1', 'name2' => 'value2', ...]. There are many places to be changed.

@samdark samdark added this to the 2.0 Beta milestone Feb 14, 2014
@samdark samdark self-assigned this Feb 14, 2014
@samdark
Copy link
Member Author

@samdark samdark commented Feb 15, 2014

What about URL parsing? Should format be changed there as well?

@qiangxue
Copy link
Member

@qiangxue qiangxue commented Feb 15, 2014

Why URL parsing is related with this?

On Saturday, February 15, 2014, Alexander Makarov notifications@github.com
wrote:

What about URL parsing? Should format be changed there as well?


Reply to this email directly or view it on GitHubhttps://github.com//issues/2426#issuecomment-35160790
.

@samdark
Copy link
Member Author

@samdark samdark commented Feb 15, 2014

It's not. Confused it with something else...

@samdark
Copy link
Member Author

@samdark samdark commented Feb 15, 2014

I'm not sure ['route', 'name1' => 'value1', 'name2' => 'value2', ...] is optimal. For example, forming parameters dynamically could require array_merge:

$params = [];
if ($sortBy) {
  $params['sortBy'] = $sortBy;
}
if ($limit) {
  $params['limit'] = $limit;
}
$this->createUrl(array_merge(['orders/index'], $params));

@zelenin
Copy link
Contributor

@zelenin zelenin commented Feb 15, 2014

maybe

$params = [];
if ($sortBy) {
  $params['sortBy'] = $sortBy;
}
if ($limit) {
  $params['limit'] = $limit;
}
$this->createUrl( 'orders/index', $params );

?

@mgrechanik
Copy link
Contributor

@mgrechanik mgrechanik commented Feb 16, 2014

@zelenin

maybe ?
$this->createUrl( 'orders/index', $params );

That is how it works now.
And we are discussing the possibility to "unifying the format to be ['route', 'name1' => 'value1', 'name2' => 'value2', ...]".

@zelenin
Copy link
Contributor

@zelenin zelenin commented Feb 16, 2014

@mgrechanik
yes, I propose a version where route is separated from the params. It's clearer and more readable I think.

@RdeWilde
Copy link

@RdeWilde RdeWilde commented Feb 16, 2014

I'd choose the first signature, because this is most clear (as in the signature itself, documentation and autocompletion). It makes clear the route is required, parameters are optional. Also, these two are complete different types of data to me, so seperating those seems logical.

I would unify them all.

For Html::a it would make more sense for the second argument to be the string url (result from the routing), as it could also be a manual URL that doesnt have to be routed.

@yupe
Copy link
Contributor

@yupe yupe commented Feb 19, 2014

I vote for Html::a('link', [...params])...and same signature for other methods

@Mirocow
Copy link

@Mirocow Mirocow commented Feb 19, 2014

yes i vote to like this format Html::a('link', [...params], [...options])...
and in the params write such ['lik', [...params], [...options]]
and in the img like this Html:img('link', [...params], [...options])

and all except the reference should not be required

@samdark
Copy link
Member Author

@samdark samdark commented Feb 20, 2014

I'm thinking about ['route', [$params]] for all cases.

@samdark
Copy link
Member Author

@samdark samdark commented Feb 20, 2014

// helpers
Html::a($text, ['/site/view', ['id' => 100]]);
Html::url(['/site/view', ['id' => 100]]);

// controller
$this->redirect(['/site/view', ['id' => 100]]);
$this->createAbsoluteUrl(['/site/view', ['id' => 100]]);
$this->createUrl(['/site/view', ['id' => 100]]);

// url manager
Yii::$app->getUrlManager()->createUrl(['/site/view', ['id' => 100]]);
Yii::$app->getUrlManager()->createAbsoluteUrl(['/site/view', ['id' => 100]]);

@cebe
Copy link
Member

@cebe cebe commented Feb 20, 2014

I'm thinking about ['route', [$params]] for all cases.

Why do we need the brackets around params in this case?

@samdark
Copy link
Member Author

@samdark samdark commented Feb 20, 2014

In order to be able to pass an array of parameters w/o doing any merging.

@qiangxue
Copy link
Member

@qiangxue qiangxue commented Feb 20, 2014

I think this format requires more typing and is more error prone.
It's much more common to create a URL from scratch by writing parameters one by one than modifying an existing parameter array.

@samdark
Copy link
Member Author

@samdark samdark commented Feb 20, 2014

OK. Implementing ['/site/view', 'id' => 100] variant then.

@samdark samdark closed this in 43c17d9 Feb 21, 2014
samdark added a commit that referenced this issue Feb 21, 2014
Fixes #2426: Changed URL creation method signatures to be consistent
@RdeWilde
Copy link

@RdeWilde RdeWilde commented Feb 22, 2014

It requires more typing this way, is difficult to phpdoc and makes no sense, for example if you just have an url with no params your creating an Array object for nothing, not efficient at all.

@cebe
Copy link
Member

@cebe cebe commented Feb 22, 2014

@samdark is '/site/view' also a valid URL in your implementation?

@samdark
Copy link
Member Author

@samdark samdark commented Feb 22, 2014

Yes.

@RusAlex
Copy link
Contributor

@RusAlex RusAlex commented Feb 22, 2014

On my opinion, current way (yii1.x) is good.
Sometimes i just mess how to write "route to module" , but i never
getting mistakes in route array
['/controller/action','param1'=>'value1',...] .
Current way is very easy to remember.

Thanks

qiansen1386 pushed a commit to qiansen1386/yii2 that referenced this issue Mar 9, 2014
qiansen1386 pushed a commit to qiansen1386/yii2 that referenced this issue Mar 9, 2014
Fixes yiisoft#2426: Changed URL creation method signatures to be consistent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.