0

I cannot seem to register a custom view helper in zend Framework 2.02 I tried all solutions posted here and anything I can think I should do but I keep getting this error:

Fatal error: Class 'ModuleName\view\Helper\mylinkhelper' not found in C:\wamp\vhosts\projectName\vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php on line 177

And here's how my module.config.php looks like:

return array{
      'controllers'=>array(

           ....
       ),
      'view_manager' => array(
          'template_path_stack' => array(
             'ModuleName' => __DIR__ . '/../view',
           ),    
   ),
   'view_helpers' => array(  
            'invokables' => array(  
                 'mylink' => 'ModuleName\view\Helper\mylinkhelper',   
             ),  
       ),
};

in my view file, I have:

echo $this->mylink($someparameter); 

I appreciate any feedback on this. I don't really know what else to do here.

Kjuly
  • 34,476
  • 22
  • 104
  • 118
paxtor
  • 331
  • 5
  • 17
  • possible duplicate of [How to add custom view helpers to Zend Framework 2](http://stackoverflow.com/questions/11392624/how-to-add-custom-view-helpers-to-zend-framework-2) – vascowhite Oct 02 '12 at 15:36
  • It's one of the approaches I tried but no luck. – paxtor Oct 02 '12 at 22:49

5 Answers5

0

It looks like the View Helper is correctly added to the ServiceManager since invoking mylink() is trying to create ModuleName\view\Helper\mylinkhelper.

I'd make sure the class is creatable with new College\view\Helper\mylinkhelper(); from a controller, this is likely to throw up some clues. Also check the filename and classname are correct.

DrBeza
  • 2,241
  • 1
  • 16
  • 18
  • Thanks DrBeza. I'm gonna explore this option now since I can't think of any other way to solve this problem. – paxtor Oct 02 '12 at 15:11
0

Your approach is correct, but there might be two things which cause you this trouble:

  1. You talk about a top level namespace ModuleName, but in your example configuration you have the top level namespace College. When you have a ModuleName namespace and you try to load College, that obviously does not work

  2. Your view helper cannot be autoloaded. Are you sure the class name is correct (MyLinkHelper), the namespace is correct (College\View\Helper, see also above) and the file name is correct (MyLinkHelper.php). And have you enabled class-name autoloading for this module in your module class?

A third option might be the lower case "view" and "mylinkhelper" as usually you would write College\View\Helper\MyLinkHelper with a capital V, M, L and H. But since you are on Windows that should not matter afaik. I know for Linux you must be aware of case sensitiveness of class names.

Jurian Sluiman
  • 13,498
  • 3
  • 67
  • 99
  • Thanks for the reply, Jurian! I meant ModuleName instead of College. so point 1 isn't the problem. class name is correct, namespace is correct, and the file name is correct as well. when you say "have you enabled class-name autoloding for this module in your module class?" I assume you mean register the module or tell the ModuleManager about the module then I did that. About the capital and lower cases, I checked that as well and it doesn't seem to be the problem. Please, if you have any other thought, just let me know. – paxtor Oct 02 '12 at 15:09
  • The autloader means that you do not have to `require 'My/File/Name.php'` in your code, but if you do `new My\File\Name` your class gets loaded automatically. You can check this for example by using @DrBeza his method: in some code that gets run (a controller for example) you do `$test = new College\View\Helper\MyLinkHelper` to check if autoloading of the class works. If that doesn't: there is your problem. If it does: the problem is something else. – Jurian Sluiman Oct 03 '12 at 07:26
0
<?php
// ./module/Application/src/Application/View/Helper/AbsoluteUrl.php
namespace Application\View\Helper;

use Zend\Http\Request;
use Zend\View\Helper\AbstractHelper;

class AbsoluteUrl extends AbstractHelper
{
    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function __invoke()
    {
        return $this->request->getUri()->normalize();
    }
}

You’ll notice that this particular helper has a dependency — a Zend\Http\Request object. To inject this, we’ll need to set up a factory with the initialization logic for our view helper:

    <?php
    // ./module/Application/Module.php
    namespace Application;

    use Application\View\Helper\AbsoluteUrl;

    class Module
    {
        public function getViewHelperConfig()
        {
            return array(
                'factories' => array(
                    // the array key here is the name you will call the view helper by in your view scripts
                    'absoluteUrl' => function($sm) {
                        $locator = $sm->getServiceLocator(); // $sm is the view helper manager, so we need to fetch the main service manager
                        return new AbsoluteUrl($locator->get('Request'));
                    },
                ),
            );
        }


     // If copy/pasting this example, you'll also need the getAutoloaderConfig() method; I've omitted it for the sake of brevity.
}

That’s it! Now you can call your helper in your view scripts:

The full URL to the current page is: <?php echo $this->absoluteUrl(); ?>

thanks to evan to create this tutorial

Developer
  • 25,073
  • 20
  • 81
  • 128
  • I really appreciate the help. I have yet to try this solution but it does look good. I'll get back to you with the outcome soon. – paxtor Nov 12 '12 at 07:42
0

The problem is that the class file is not being loaded. Its supposed to be included in autoload_classmap.php.

<?php
return array(
    '{module}\View\Helper\{helper}' => __DIR__ . '\View\Helper\{helper}.php',
);
?>

I ran in the same issue and this page helped me.

As I'm new to ZF, i don't know if there is another way to add the paths in autoload_classmap, i think probably there is, but i just edited the file manually.

Fábio Carneiro
  • 115
  • 2
  • 11
-1

Got the same problem, found out by myself that view helper file was not included. while putting it into controller for testing it worked

e.g.: require_once('module/Pages/view/Helper/RenderNav.php');

why it has not been autoloaded ?

Sirko
  • 72,589
  • 19
  • 149
  • 183
To123
  • 1