getherbert / herbert

The WordPress Plugin Framework:
http://getherbert.com/
632 stars 94 forks source link

Argument 3 passed to ... must be an instance of Herbert\\Framework\\Http #77

Closed jeremyzahner closed 9 years ago

jeremyzahner commented 9 years ago

Hi there

Im currently stuck at a problem i just somehow can not manage to resolve.

I have a function inside my controller which basically returns a twig view.

    /**
     * Create a new beer label view.
     */
    public function createScreen($headline, $bottomline, Http $http)
    {

        if (!isset($headline)) $headline = 'BEER';
        if (!isset($bottomline)) $bottomline = 'AMBER';
        if (!isset($imageurl)) $imageurl = Helper::assetUrl('/stich.png');

        return view('@BierEtikettenGenerator/generator/generator_screen.twig', [
            'headline' => $headline,
            'bottomline' => $bottomline,
            'labelsvg_file' => Helper::assetUrl('/beer_etikette_web.svg'),
            'bgimage_file' => $imageurl
        ]);
    }

Then again i use a route AND a shortcode to access the exact same controller function.

Route:

$router->get([
    'as'   => 'BeerLabelCreateScreen',
    'uri'  => '/beerlabel/create/screen',
    'uses' => __NAMESPACE__ . '\Controllers\BeerLabelController@createScreen',
]);

Shortcode:

$shortcode->add(
    'showgenerator',
    __NAMESPACE__ . '\Controllers\BeerLabelController@createScreen',
    [
        'headline' => 'headline',
        'bottomline' => 'bottomline'
    ]
);

Every time i try to access it it says:

PHP Catchable fatal error:  Argument 3 passed to BierEtikettenGenerator\\Controllers\\BeerLabelController::createScreen() must be an instance of Herbert\\Framework\\Http, none given in /Users/jzahner/Workflow/misc/bieretiketten-generator-plugin/app/Controllers/BeerLabelController.php on line 72

Actually it does it only if i access it by the direct route. If i get it from the shortcode it seems to work just fine. Also changing the amount of arguments does not make a difference.

My first guess: Twig Cache.

jeremyzahner commented 9 years ago

Okay, another guess.

Do i need to somehow pass $headline and $bottomline in any case? Because right now it only gets passed by the shortcode, not by the route.

jeremyzahner commented 9 years ago

So as far as i can see, there is some sort of conflict when using the same Controller for either shortcodes or routes. At least when passing arguments. For me this is resolved. But of course it might get in the way when writing DRY code. =)

ConnorVG commented 9 years ago

@jeremyzahner you're requesting two required variables that the router doesn't know how to provide. Try making them default as null instead. That should solve your issue.

    /**
     * Create a new beer label view.
     */
    public function createScreen($headline = null, $bottomline = null, Http $http)
    {

        $headline = $headline ?: 'BEER';
        $bottomline = $bottomline ?: 'AMBER';
        if (!isset($imageurl)) $imageurl = Helper::assetUrl('/stich.png');

        return view('@BierEtikettenGenerator/generator/generator_screen.twig', [
            'headline' => $headline,
            'bottomline' => $bottomline,
            'labelsvg_file' => Helper::assetUrl('/beer_etikette_web.svg'),
            'bgimage_file' => $imageurl
        ]);
    }
jeremyzahner commented 9 years ago

Sorted out. Thank you!