If using zftool you must run classmap generate and other things

Home Forums Learn ZF2 If using zftool you must run classmap generate and other things

This topic contains 4 replies, has 3 voices, and was last updated by  matt 2 years, 10 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #284

    jchlu
    Participant

    Just following the book is not enough to get a working Debug module with zftool.
    I ran:

    ./vendor/zftool/zf.php create module Debug

    and followed along with the book very carefully; re-loading my application got an error:

    Uncaught exception 'Zend\\Loader\\Exception\\InvalidArgumentException' with message 'Map file provided does not exist.

    until I ran:

    ./vendor/zftool/zf.php classmap generate module/Debug/

    Which got it working again.

    There is also no automatically created onBootstrap() method in the Debug\Module.php, so if you follow the book and get to the point where it says:

    Open the Module.php file of the Debug module. Replace the onBootstrap method with the following code

    there is no such method to replace.

    (I am using the Kindle version of the book BTW)

    Johnny.

    #287

    Slavey Karadzhov
    Participant

    Hi Johnny,
    > There is also no automatically created onBootstrap() method in the Debug\Module.php
    You are right. If you create the module using zftool in the newly created Module class inside the Debug\Module.php file there will be no onBootstrap method. What you can do is to create such a method and add one parameter called event. Something like this

    public function onBootstrap($event)
    {
    }

    >until I ran:
    >
    > ./vendor/zftool/zf.php classmap generate module/Debug/
    > Which got it working again.
    You are right too. We may think of including these extra instructions for those that decide to use zftool to create their module.

    Thanks for the hints and I hope you enjoy reading the book.

    #315

    matt
    Participant

    Disregard. Looks like the ZFTool added close to nothing. I’m adding code in manually. Can’t believe zftool has been released for many months now.

    ———

    After adding the onBootstrap method to Debug/Module.php and calling an invalid url, I’d get

    Catchable fatal error: Argument 1 passed to Debug\Module::onBootstrap() must be an instance of Debug\MvcEvent, instance of Zend\Mvc\MvcEvent given in /var/www/learnzf2/module/Debug/Module.php on line 9

    I added

    use Zend\Mvc\MvcEvent;

    and the exception goes away. However, I don’t think that is the solution as the MvcEvent being passed around returns null for getController, getParam(‘error’) et al.

    Any advice is appreciated.

    • This reply was modified 2 years, 10 months ago by  matt.
    #324

    Slavey Karadzhov
    Participant

    So Matt, if you want to read the controller, action or other routing parameters you need to do something like this:

    public function onBootstrap(MvcEvent $event) {

    1. From the MvcEvent get a route match

    $match = $event->getRouteMatch();

    2. And then if there is a match from it you can get the different parameters:

    if(!$match) {
       return;
    }
    
    $controller = $match->getParam('controller');
    $action     = $match->getParam('action');

    Take a look at the example source code from the github repository.
    https://github.com/slaff/learnzf2

    #327

    matt
    Participant

    Thanks for the reply.

    I will have to circle back to this.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.