2

I am trying to simply use a custom view helper that is located in /library/my/view/helpers/friends.php

I have this in the application.ini:

resources.view.helperPath.My_View_Helper = "/my/view/helpers"

This is the helper class:

class My_View_Helper_Friends extends Zend_View_Helper_Abstract {
    public function friends() {
        $str = "hello world";
        return $str;
    }
}

This is in the view file:

<?php echo $this->friends(); ?>

I get an error on this line saying it can't find the plugin in a path that it is already in.

The error:

Plugin by name 'Friends' was not found in the registry; used paths: My_View_Helper_: /My/View/Helpers/ Zend_View_Helper_: Zend/View/Helper/;C:/http/xampplite/htdocs/zf-tutorial/application/views\helpers/

Looks like its using the right path and the file is there. I don't understand why it can't find it?

drew010
  • 68,777
  • 11
  • 134
  • 162
Andy N
  • 1,013
  • 9
  • 25
  • I've always had to use `$this->helper()` to make it work. Also my view helpers are all `Zend_View_Helper_MyHelper` (maybe try a rename) although I don't put them in the library, I keep them in `/views/helpers` to keep things simple. You didn't mention if you added the `autoloaderNamespaces[] = MY_` to your application.ini. Not sure if that would be a factor or not. – RockyFord Mar 31 '12 at 07:03
  • @RockyFord It may be that you always have to use `$this->helper()` because you are naming your classes `Zend_View_Helper_XXX` (if I read that correctly). Only classes officially belonging to ZF should be prefixed with `Zend_` or `ZendX_` (See [ZF Naming Conventions](http://framework.zend.com/manual/en/coding-standard.naming-conventions.html)). It might have to do with the autoloader looking in the Zend directories for your helper classes. – drew010 Mar 31 '12 at 07:15
  • @drew010 I should said `$this->MyHelper()` prefixing with `Zend_View_Helper_XXX` is the default. So I don't have to register a new helper path. They are stored in any of my `/views/helpers` directories. Someday I'll put them all in one place, when I cleanup the application. Also the question has been cleaned up since I posted, so my post makes less sense :) – RockyFord Mar 31 '12 at 09:28
  • I have also tried putting the Friends helper in /application/views/helpers/ and still get the same error. I renamed the class to Zend_View_Helper_Friends. Do I have to include anything in the helper file? – Andy N Apr 01 '12 at 14:45

1 Answers1

2

I believe the problem is that the incorrect path is being used.

...used paths: My_View_Helper_: /My/View/Helpers/

Note how the path is /My/View/Helpers. It is using an absolute path. The other issue is that the folder should be named Helper instead of helpers.

Then change the config line in your application.ini to this:

resources.view.helperPath.My_View_Helper = APPLICATION_PATH "/../library/my/view/helper"

and make sure your class is called My_View_Helper_Friends and the file is named Friends.php. Case matters. The directory should really be called My/View/Helper with caps.

EDIT: Assuming library is in your include_path, you could also use the line:

resources.view.helperPath.My_View_Helper = "My/View/Helper"

Notice how it doesn't have the leading /. This will search all locations in your include_path for a folder My/View/Helper.

drew010
  • 68,777
  • 11
  • 134
  • 162
  • I did exactly as you recommended for the first option. Added that path constant, capitalized my dir names and made sure the helper class name is right. Now I get this error: Plugin by name 'Friends' was not found in the registry; used paths: My_View_Helper_: C:\http\xampplite\htdocs\zf-tutorial\application../library/my/view/helper/ Zend_View_Helper_: Zend/View/Helper/;C:/http/xampplite/htdocs/zf-tutorial/application/views\helpers/ – Andy N Mar 31 '12 at 20:26
  • I am using windows 7. Notice that application constant is spitting out back slashes and the rest of the path is forward slashes. I think that is the problem. Looks like all the paths in the application.ini that use that constant is mix forward and backward slashes. – Andy N Mar 31 '12 at 20:29
  • Why is the path in appliation.ini all lowercase if the actual dir names is capital case? I am not concerned about naming convention at this point. Just what works. – Andy N Mar 31 '12 at 20:32
  • In the case of the first application.ini line I gave you, I left off a slash. It should be `resources.view.helperPath.My_View_Helper = APPLICATION_PATH "/../library/my/view/helper"` which is now reflected in the post. See if that works for you. The path is still incorrect given my original example which resulted in `C:\http\xampplite\htdocs\zf-tutorial\application../library/my/view/helper/` – drew010 Apr 01 '12 at 00:09
  • Now I get this error: Plugin by name 'Friends' was not found in the registry; used paths: My_View_Helper_: C:\http\xampplite\htdocs\zf-tutorial\application/../library/my/view/helper/ Zend_View_Helper_: Zend/View/Helper/;C:/http/xampplite/htdocs/zf-tutorial/application/views\helpers/. That path looks strange with forward and backward slashes. How does that work? I read somewhere about adding 'My_' to the autoloader. Tried autoloaderNamespaces[] = "My_" in the .ini and that didn't work either. – Andy N Apr 01 '12 at 01:35
  • The mixed slashes shouldn't be a problem generally. It does appear to be looking for classes named `My_View_Helper_XXX` in `C:\http\xampplite\htdocs\zf-tutorial\application/../library/my/view/helper/` Can you confirm if that is the correct path to the library folder and then your view helpers? Maybe try correcting the case so it says `C:\http\xampplite\htdocs\zf-tutorial\application/../library/My/View/Helper/` if possible. – drew010 Apr 01 '12 at 03:46
  • If not, try just using this line instead: `resources.view.helperPath.My_View_Helper = "My/View/Helper"` – drew010 Apr 01 '12 at 03:47
  • Does the ../ mean go up one dir from application dir? Tried both methods and still get the error. The .ini file even has this: includePaths.library = APPLICATION_PATH "/../library" – Andy N Apr 01 '12 at 06:27
  • @drew010 Was using a plain text editor and failed to notice no open – Andy N Apr 01 '12 at 15:26