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

Easier Access to Helpers within Helpers #1021

Closed
bayleedev opened this issue Nov 15, 2013 · 10 comments
Closed

Easier Access to Helpers within Helpers #1021

bayleedev opened this issue Nov 15, 2013 · 10 comments

Comments

@bayleedev
Copy link
Member

Within views you can access a helper via $this->name but within a helper you cannot access another one the same way but you can do it via $this->_context->helper('name').

When is it useful? A good use case is a breadcrumb helper. Here, you might need the Route helper or a String or Html helper. An Asset helper might need the html helper. There are dozens of other possible reasons to need them.

Would anybody be up for a similar syntax in helpers? I assume the pr wouldn't be difficult.

public function __get($name) {
    return $this->_context->helper($name);
}
@nateabele
Copy link
Member

I'm fine with short-hand, but I'd be pretty wary of putting a __get() in Helper. Without that, though, I'm not sure how much better than $this->helper('name') you could get. Worth it or no?

@bayleedev
Copy link
Member Author

I'm mostly looking for consistency. If we added a helper() method on the helper, would it be alright to add a same-named method to the view object. I'm honestly not sure what object the view is executed in (File maybe?).

@tmaiaroto
Copy link
Contributor

If you extend helpers you can also use parent and I don't think you need to go as far as $this->_context->helper('name') you should be able to just use $this->_context->helperName->method() no? Is that any better for you to write it that way? Then you aren't calling a method to get the helper to call its method. Even a getter would impose another function call just to get something you should already have access to call directly, no? (I could be wrong).

@mpiasta-ca
Copy link

The more important question is -- how do you use a helper in a controller?

@ericcholis
Copy link
Contributor

@mpiasta-ca To me, that's outside the scope of what Controllers are intended for. I do my best to keep that sort of logic in the Views. Actually, I can't think of a case where I'd want to use a Helper in my Controllers.

I agree with @nateabele, adding a __get() in a helper feels odd. But, calling $this->Html within a helper is more consistent with the behavior in views.

@nateabele
Copy link
Member

Maybe you could declare the helpers you want to use in a protected property, and Helper can give you direct property access to them at initialization time. Also, yeah, no... no helpers in controllers, that's just doing it rong.

@ericcholis
Copy link
Contributor

I like that solution @nateabele, kind of follows the $_classes pattern used elswhere

@minhna
Copy link

minhna commented Jan 14, 2014

I think $this->_context->helperName->method() is enough.

@bayleedev
Copy link
Member Author

@minhna I was aiming for consistency. Remember the each place for $this->_context->name->method() is difficult, especially when you hop between languages and frameworks a few times a day.

@mariuswilms
Copy link
Member

I'm with @minhna on this topic. $this->_context->helperName->method() is not too long and already provides full access. Anyway - @blainesch thank you for starting the discussion and everybody else for commenting.

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

No branches or pull requests

7 participants