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

Adding Url helper class #2630

Closed
qiangxue opened this issue Mar 5, 2014 · 39 comments
Closed

Adding Url helper class #2630

qiangxue opened this issue Mar 5, 2014 · 39 comments
Assignees
Milestone

Comments

@qiangxue
Copy link
Member

@qiangxue qiangxue commented Mar 5, 2014

We currently have several URL-related functions scattering in different classes. How about introducing a Url helper class to contain these functions and provide more commonly ones? What functions should be provided in this class? What about the followings?

  • action(): like Controller::createUrl()
  • asset(): prefix a relative URL with base URL, e.g. Url::asset('images/logo.gif')
  • How to deal with Html::url() which does both of the above?
@samdark
Copy link
Member

@samdark samdark commented Mar 5, 2014

Do you want to move methods from other places to Url or leave these as is?

@qiangxue
Copy link
Member Author

@qiangxue qiangxue commented Mar 5, 2014

UrlManager::createUrl() and Controller::createUrl() should be kept (one is to create url from absolute route, the other supports relative route).

All other URL creation methods should be moved to Url helper, including Html::url().

@samdark
Copy link
Member

@samdark samdark commented Mar 5, 2014

Sounds good. Especially considering the fact that logically URL has nothing to do with Html helper.

@qiangxue
Copy link
Member Author

@qiangxue qiangxue commented Mar 5, 2014

We could have Url::to() to replace Html::url().
These methods are mainly commonly used shortcut methods.

@cebe
Copy link
Member

@cebe cebe commented Mar 5, 2014

what is Url::to()? I would expect Url::create().

@cebe cebe added this to the 2.0 Beta milestone Mar 5, 2014
@Ragazzo
Copy link
Contributor

@Ragazzo Ragazzo commented Mar 5, 2014

why moving this to helper if it is depending on url-manager settings? what exactly methods do you want to move? createUrl/createAbsoluteUrl are fin in url-manager and in controller, what are other methods?

@samdark
Copy link
Member

@samdark samdark commented Mar 5, 2014

I'd also expect Url::create.

@Ragazzo
Copy link
Contributor

@Ragazzo Ragazzo commented Mar 5, 2014

for what? it anyway will be a calling url-manager, because of differetn url-routes settings.

@qiangxue
Copy link
Member Author

@qiangxue qiangxue commented Mar 5, 2014

@Ragazzo The purpose of this helper class is to provide a set of shortcuts to commonly used methods. It's fine it has dependency on other components.

@Ragazzo
Copy link
Contributor

@Ragazzo Ragazzo commented Mar 6, 2014

but i dont see any good things in it, why it is better then using url-manager? i think it can confuse user, because now he has already 3 start points: controller, url-manager, Html::url(), and it will be added one more. Not good as for me, also it is better when user explicitly see where and how url are generated rather then browsing through many wrappers.

@samdark
Copy link
Member

@samdark samdark commented Mar 6, 2014

It won't be one more. Html::url() will be replaced with Url::create.

@drenty
Copy link
Contributor

@drenty drenty commented Mar 6, 2014

I think a shortcut to do this :

Yii::$app->user->setReturnUrl(Yii::$app->getRequest()->getUrl());

would be cool if it doesn't exist already.

@qiangxue
Copy link
Member Author

@qiangxue qiangxue commented Mar 6, 2014

@drenty: perhaps Url::remember($url = null) ?

@samdark
Copy link
Member

@samdark samdark commented Mar 6, 2014

@qiangxue no, that's too generic.

@drenty
Copy link
Contributor

@drenty drenty commented Mar 7, 2014

@qiangxue @samdark Url::mark($url = null)?

@samdark
Copy link
Member

@samdark samdark commented Mar 7, 2014

Too generic as well. Url::setReturn or Url::setBack should be OK.

@lucianobaraglia
Copy link
Contributor

@lucianobaraglia lucianobaraglia commented Mar 7, 2014

@samdark if I first see Url::setReturn or Url::setBack I would think it's about setting a boolean...
Would prefer Url::setReturnUrl or Url::setBackUrl even if it sounds repetitive...

@dyegonery
Copy link

@dyegonery dyegonery commented Mar 7, 2014

I agree with Url::setReturnUrl

@samdark
Copy link
Member

@samdark samdark commented Mar 9, 2014

@qiangxue I can handle it if you haven't started yet.

@qiangxue
Copy link
Member Author

@qiangxue qiangxue commented Mar 9, 2014

Sure, go ahead.

On Sunday, March 9, 2014, Alexander Makarov notifications@github.com
wrote:

@qiangxue https://github.com/qiangxue I can handle it if you haven't
started yet.


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

@samdark
Copy link
Member

@samdark samdark commented Mar 9, 2014

Should we move the following to the helper?

  1. yii\web\controller\createUrl($params) to Url::action($params).
  2. yii\web\controller\createAbsoluteUrl($params) to Url::absoluteAction($params) or Url::action($params, true).
  3. yii\web\controller\getCanonicalUrl to Url::canonical.
@Ragazzo
Copy link
Contributor

@Ragazzo Ragazzo commented Mar 9, 2014

i think action should be route.

@samdark
Copy link
Member

@samdark samdark commented Mar 9, 2014

It doesn't return route, it returns URL pointing to an action.

@Ragazzo
Copy link
Contributor

@Ragazzo Ragazzo commented Mar 9, 2014

route sounds beter, Url::absoluteAction as for not good, since route can have only controller or module/controller without action.

@samdark
Copy link
Member

@samdark samdark commented Mar 9, 2014

Route is site/index or post/view. It's not URL so it will definitely be confusing that a method named route actually returns URL.

@Ragazzo
Copy link
Contributor

@Ragazzo Ragazzo commented Mar 9, 2014

so absoluteAction does not sounds confusing to you?

@samdark
Copy link
Member

@samdark samdark commented Mar 9, 2014

route sounds much more confusing. Do you have a name better than action?

@Ragazzo
Copy link
Contributor

@Ragazzo Ragazzo commented Mar 9, 2014

  1. Url::create();
  2. Url::createAbsolute();
  3. Url::createCanonical().
@samdark
Copy link
Member

@samdark samdark commented Mar 9, 2014

So you suggest dropping yii\web\controller\createUrl($params) and yii\web\controller\createAbsoluteUrl($params) without adding any alternative?

@Ragazzo
Copy link
Contributor

@Ragazzo Ragazzo commented Mar 9, 2014

why dropping? make them use Url helper, i thought it was your initial idea, no?

@qiangxue
Copy link
Member Author

@qiangxue qiangxue commented Mar 9, 2014

We should keep yii\web\controller\createUrl($params) and
yii\web\controller\createAbsoluteUrl($params) .

Make 'Url' a wrapper of them, not the other way around.

On Sunday, March 9, 2014, Mark notifications@github.com wrote:

why dropping? make them use Url helper, i thought it was your initial
idea, no?


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

@Ragazzo
Copy link
Contributor

@Ragazzo Ragazzo commented Mar 9, 2014

why then you are adding new helper? anyway i provided as i see names of those methods and how they can be used with controller, other is for you ofcourse))

@samdark
Copy link
Member

@samdark samdark commented Mar 9, 2014

@Ragazzo currently create is old Html::url that is not the same as yii\web\controller\createUrl($params).

@Ragazzo
Copy link
Contributor

@Ragazzo Ragazzo commented Mar 9, 2014

yes, i know, i mean can we more or less make it similar and use this helper ? anyway as i said, i am not for dropping them. what about names, are they ok?

@samdark
Copy link
Member

@samdark samdark commented Mar 9, 2014

Overall done. Resulted in the following methods:

Url::create($params);
Url::createAbsolute($params);
Url::base('files/source.zip');
Url::rememberReturn();
Url::getCanonical();
Url::getHome();
@cebe
Copy link
Member

@cebe cebe commented Mar 9, 2014

Url::base('files/source.zip');

We may also make $url param optional to allow usage like this in view files:

<img src="<?= Url::base() ?>/images/myimage.png" />

Looks good to me overall.

@samdark
Copy link
Member

@samdark samdark commented Mar 9, 2014

@cebe done.

@cebe
Copy link
Member

@cebe cebe commented Mar 9, 2014

Hm... maybe naming of getCanonical and getHome should be adjusted to be consistent with base:

Url::base()
Url::canonical()
Url::home()

or

Url::getBase()
Url::getCanonical()
Url::getHome()

I'd prefer the first case.

samdark added a commit that referenced this issue Mar 10, 2014
#2630: `yii\heplers\Html::url` moved to new `yii\helpers\Url::create`
@samdark samdark closed this Mar 10, 2014
@qiangxue
Copy link
Member Author

@qiangxue qiangxue commented Mar 11, 2014

👍

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.

None yet
7 participants
You can’t perform that action at this time.