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

Getting `Unresolvable dependency` exception #150

Open
aik099 opened this Issue Sep 29, 2014 · 46 comments

Comments

Projects
None yet
10 participants
@aik099
Contributor

aik099 commented Sep 29, 2014

I'm using https://github.com/barryvdh/laravel-ide-helper to generate auto-complete for PhpStorm. I guess it's invoking all services providers to work. As a result I'm getting following exception:

Exception: Cannot instantiate Twig extension 'TwigBridge\Extension\Laravel\Form': Unresolvable dependency resolving [Parameter #2 [ <required> $csrfToken ]] in class Illuminate\Html\FormBuilder
Skipping \TwigBridge\Facade\Twig.
A new helper file was written to _ide_helper.php

I guess this happens because the mentioned TwigBridge\Extension\Laravel\Form class has dependency on FormBuilder (see https://github.com/rcrowe/TwigBridge/blob/master/src/Extension/Laravel/Form.php#L34), which happens to have 3rd required argument called $csrfToken: https://github.com/laravel/framework/blob/4.2/src/Illuminate/Html/FormBuilder.php#L82

Used Laravel Version: 4.3

Related issue: barryvdh/laravel-ide-helper#119

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Sep 29, 2014

Contributor

Same happens when I try to create twig file and use it:

laravel_twigbrige_fatalerror

Contributor

aik099 commented Sep 29, 2014

Same happens when I try to create twig file and use it:

laravel_twigbrige_fatalerror

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Sep 29, 2014

Contributor

I guess it's Laravel itself who is trying to inject dependencies and fails. If that $csrfToken argument was there all the time, then I wonder how this could be working at all.

Contributor

aik099 commented Sep 29, 2014

I guess it's Laravel itself who is trying to inject dependencies and fails. If that $csrfToken argument was there all the time, then I wonder how this could be working at all.

@barryvdh

This comment has been minimized.

Show comment
Hide comment
@barryvdh

barryvdh Sep 29, 2014

Collaborator

It wasn't until 5.x

Collaborator

barryvdh commented Sep 29, 2014

It wasn't until 5.x

@barryvdh

This comment has been minimized.

Show comment
Hide comment
@barryvdh

barryvdh Sep 29, 2014

Collaborator

And as I said it doesn't have to do with the helper. Just publish the config and remove the form extension for now, until it's fixed, or create a PR to fix it.

Collaborator

barryvdh commented Sep 29, 2014

And as I said it doesn't have to do with the helper. Just publish the config and remove the form extension for now, until it's fixed, or create a PR to fix it.

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Sep 29, 2014

Contributor

If I'd only knew what was making it work in Laravel 4.2 in the first place knowing that constructor declaration of FormBuilder was the same all the time.

Contributor

aik099 commented Sep 29, 2014

If I'd only knew what was making it work in Laravel 4.2 in the first place knowing that constructor declaration of FormBuilder was the same all the time.

@barryvdh

This comment has been minimized.

Show comment
Hide comment
@barryvdh

barryvdh Sep 29, 2014

Collaborator

Not sure, the FormBuilder is created by the HtmlServiceProvider, so that instance should be passed on to the extension. You did include the HtmlServiceProvider right?

Collaborator

barryvdh commented Sep 29, 2014

Not sure, the FormBuilder is created by the HtmlServiceProvider, so that instance should be passed on to the extension. You did include the HtmlServiceProvider right?

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Sep 29, 2014

Contributor

I've echoed some debug into in Container::getDependencies method (https://github.com/laravel/framework/blob/4.2/src/Illuminate/Container/Container.php#L549) in both Laravel version to see what's actually being resolved and here are the results:

Laravel 4.2:

→ artisan ide-helper:generate
[4.2, getDependencies] TwigBridge\Extension\Loader\Facades(config)
[4.2, getDependencies] TwigBridge\Extension\Loader\Filters(config)
[4.2, getDependencies] TwigBridge\Extension\Loader\Functions(config)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Auth(auth)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Config(config)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Form(form)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Html(html)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Input(request)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Session(session)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Translator(translator)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Url(url)
A new helper file was written to _ide_helper.php

Laravel 4.3:

→ artisan ide-helper:generate 
[4.3, getDependencies] App\Console\InspireCommand()
[4.3, getDependencies] TwigBridge\Extension\Loader\Facades(config)
[4.3, getDependencies] TwigBridge\Extension\Loader\Filters(config)
[4.3, getDependencies] TwigBridge\Extension\Loader\Functions(config)
[4.3, getDependencies] TwigBridge\Extension\Laravel\Auth(auth)
[4.3, getDependencies] TwigBridge\Extension\Laravel\Config(config)
[4.3, getDependencies] TwigBridge\Extension\Laravel\Form(form)
[4.3, getDependencies] Illuminate\Html\FormBuilder(html, url, csrfToken)
[4.3, getDependencies] Illuminate\Html\HtmlBuilder(url)
[4.3, resolveNonClass] Parameter #2 [ <required> $csrfToken ]
Exception: Cannot instantiate Twig extension 'TwigBridge\Extension\Laravel\Form': Unresolvable dependency resolving [Parameter #2 [ <required> $csrfToken ]] in class Illuminate\Html\FormBuilder
Skipping \TwigBridge\Facade\Twig.
A new helper file was written to _ide_helper.php

The major change between both Laravel versions is fact that in Laravel 4.3 the Html component was moved out from the core. It maybe affecting what's being inspected by Laravel during ide-helper run.

I've placed this in the beginning of getDependencies method:

$backtrace = debug_backtrace(0);
echo '[4.2, getDependencies] ' . $backtrace[1]['args'][0] . '(' . $this->getParameterNames($parameters) . ')' . PHP_EOL;

and added this helper method:

/**
 * Returns names of parameters.
 *
 * @param array $parameters Parameters.
 *
 * @return string
 */
protected function getParameterNames(array $parameters)
{
    $ret = array();

    /** @var ReflectionParameter $parameter */
    foreach ( $parameters as $parameter ) {
        $ret[] = $parameter->name;
    }

    return implode(', ', $ret);
}
Contributor

aik099 commented Sep 29, 2014

I've echoed some debug into in Container::getDependencies method (https://github.com/laravel/framework/blob/4.2/src/Illuminate/Container/Container.php#L549) in both Laravel version to see what's actually being resolved and here are the results:

Laravel 4.2:

→ artisan ide-helper:generate
[4.2, getDependencies] TwigBridge\Extension\Loader\Facades(config)
[4.2, getDependencies] TwigBridge\Extension\Loader\Filters(config)
[4.2, getDependencies] TwigBridge\Extension\Loader\Functions(config)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Auth(auth)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Config(config)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Form(form)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Html(html)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Input(request)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Session(session)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Translator(translator)
[4.2, getDependencies] TwigBridge\Extension\Laravel\Url(url)
A new helper file was written to _ide_helper.php

Laravel 4.3:

→ artisan ide-helper:generate 
[4.3, getDependencies] App\Console\InspireCommand()
[4.3, getDependencies] TwigBridge\Extension\Loader\Facades(config)
[4.3, getDependencies] TwigBridge\Extension\Loader\Filters(config)
[4.3, getDependencies] TwigBridge\Extension\Loader\Functions(config)
[4.3, getDependencies] TwigBridge\Extension\Laravel\Auth(auth)
[4.3, getDependencies] TwigBridge\Extension\Laravel\Config(config)
[4.3, getDependencies] TwigBridge\Extension\Laravel\Form(form)
[4.3, getDependencies] Illuminate\Html\FormBuilder(html, url, csrfToken)
[4.3, getDependencies] Illuminate\Html\HtmlBuilder(url)
[4.3, resolveNonClass] Parameter #2 [ <required> $csrfToken ]
Exception: Cannot instantiate Twig extension 'TwigBridge\Extension\Laravel\Form': Unresolvable dependency resolving [Parameter #2 [ <required> $csrfToken ]] in class Illuminate\Html\FormBuilder
Skipping \TwigBridge\Facade\Twig.
A new helper file was written to _ide_helper.php

The major change between both Laravel versions is fact that in Laravel 4.3 the Html component was moved out from the core. It maybe affecting what's being inspected by Laravel during ide-helper run.

I've placed this in the beginning of getDependencies method:

$backtrace = debug_backtrace(0);
echo '[4.2, getDependencies] ' . $backtrace[1]['args'][0] . '(' . $this->getParameterNames($parameters) . ')' . PHP_EOL;

and added this helper method:

/**
 * Returns names of parameters.
 *
 * @param array $parameters Parameters.
 *
 * @return string
 */
protected function getParameterNames(array $parameters)
{
    $ret = array();

    /** @var ReflectionParameter $parameter */
    foreach ( $parameters as $parameter ) {
        $ret[] = $parameter->name;
    }

    return implode(', ', $ret);
}
@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Sep 29, 2014

Contributor

Here is the gist containing the config/app.php from both Laravel installations: https://gist.github.com/aik099/bfd82feb6c1680277500

Contributor

aik099 commented Sep 29, 2014

Here is the gist containing the config/app.php from both Laravel installations: https://gist.github.com/aik099/bfd82feb6c1680277500

@GrahamCampbell

This comment has been minimized.

Show comment
Hide comment
@GrahamCampbell

GrahamCampbell Sep 29, 2014

Contributor

If this is for laravel 5 (new name for 4.3), then the html stuff is setup wrong.

Contributor

GrahamCampbell commented Sep 29, 2014

If this is for laravel 5 (new name for 4.3), then the html stuff is setup wrong.

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Sep 29, 2014

Contributor

I found one post on a forum explaining how to connect back that HTML component that was removed from core in 4.3/5.0 : http://laravel.io/forum/09-20-2014-html-form-class-not-found-in-laravel-5 . I'm not sure if it's correct way of doing that because such migration wasn't mentioned in official docs.

Contributor

aik099 commented Sep 29, 2014

I found one post on a forum explaining how to connect back that HTML component that was removed from core in 4.3/5.0 : http://laravel.io/forum/09-20-2014-html-form-class-not-found-in-laravel-5 . I'm not sure if it's correct way of doing that because such migration wasn't mentioned in official docs.

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Sep 29, 2014

Contributor

If this is for laravel 5 (new name for 4.3), then the html stuff is setup wrong.

I guess you've looked at Gist I provided to say that it's something wrong with it.

Yeh. It's correct.

So what might be happening wrong when TwigBridge is connected?

Contributor

aik099 commented Sep 29, 2014

If this is for laravel 5 (new name for 4.3), then the html stuff is setup wrong.

I guess you've looked at Gist I provided to say that it's something wrong with it.

Yeh. It's correct.

So what might be happening wrong when TwigBridge is connected?

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Sep 29, 2014

Contributor

Debugging a bit more revealed that in Laravel 4.2 by the moment the TwigBridge\Extension\Laravel\Form class is inspected that instance of FormBuilder is already created under form alias (Container::aliases) and it's being reused.

However in Laravel 4.3/5.0 there is no such alias as form at all and attempt to instantiate FormBuilder class directly is made.

Contributor

aik099 commented Sep 29, 2014

Debugging a bit more revealed that in Laravel 4.2 by the moment the TwigBridge\Extension\Laravel\Form class is inspected that instance of FormBuilder is already created under form alias (Container::aliases) and it's being reused.

However in Laravel 4.3/5.0 there is no such alias as form at all and attempt to instantiate FormBuilder class directly is made.

@GrahamCampbell

This comment has been minimized.

Show comment
Hide comment
@GrahamCampbell

GrahamCampbell Sep 29, 2014

Contributor

Please stop saying 4.3/5.0. 4.3 doesn't exist. 5.0 will be the next version.

Contributor

GrahamCampbell commented Sep 29, 2014

Please stop saying 4.3/5.0. 4.3 doesn't exist. 5.0 will be the next version.

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Sep 29, 2014

Contributor

Please stop saying 4.3/5.0. 4.3 doesn't exist. 5.0 will be the next version.

I'm new to Laravel, so I wasn't sure what version to use because in forums people use both versions. Since everybody know that 4.3 is essentially 5.0, then no confusion happening anyway 😉

Contributor

aik099 commented Sep 29, 2014

Please stop saying 4.3/5.0. 4.3 doesn't exist. 5.0 will be the next version.

I'm new to Laravel, so I wasn't sure what version to use because in forums people use both versions. Since everybody know that 4.3 is essentially 5.0, then no confusion happening anyway 😉

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Sep 29, 2014

Contributor

I've updated gist with dump of aliases to the point where FormBuilder class is about to be inspected because of it's usage within TwigBridge\Form extension.

Contributor

aik099 commented Sep 29, 2014

I've updated gist with dump of aliases to the point where FormBuilder class is about to be inspected because of it's usage within TwigBridge\Form extension.

@barryvdh

This comment has been minimized.

Show comment
Hide comment
@barryvdh

barryvdh Sep 29, 2014

Collaborator

I'm on my phone so can't really check, but I think the form binding, as registered within the service provider was aliases to the FormBuilder class, in here: https://github.com/laravel/framework/blob/4.2/src/Illuminate/Foundation/Application.php (registerCoreContainerAliases or something)
In 5.x that is probably removed, so you have to make the alias yourself. So either that should be a extra step in migrating or the service provider should take care of that.

Collaborator

barryvdh commented Sep 29, 2014

I'm on my phone so can't really check, but I think the form binding, as registered within the service provider was aliases to the FormBuilder class, in here: https://github.com/laravel/framework/blob/4.2/src/Illuminate/Foundation/Application.php (registerCoreContainerAliases or something)
In 5.x that is probably removed, so you have to make the alias yourself. So either that should be a extra step in migrating or the service provider should take care of that.

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Sep 29, 2014

Contributor

Adding following to aliases array in config/app.php in Laravel 5.0 had no effect:

'form'      => 'Illuminate\Html\FormBuilder',
'html'      => 'Illuminate\Html\HtmlBuilder',

I've added them after

'Form'      => 'Illuminate\Html\FormFacade',
'HTML'      => 'Illuminate\Html\HtmlFacade',
Contributor

aik099 commented Sep 29, 2014

Adding following to aliases array in config/app.php in Laravel 5.0 had no effect:

'form'      => 'Illuminate\Html\FormBuilder',
'html'      => 'Illuminate\Html\HtmlBuilder',

I've added them after

'Form'      => 'Illuminate\Html\FormFacade',
'HTML'      => 'Illuminate\Html\HtmlFacade',
@GrahamCampbell

This comment has been minimized.

Show comment
Hide comment
@GrahamCampbell

GrahamCampbell Sep 29, 2014

Contributor

Better to not use these aliases and just use the full class names to be honest,

Contributor

GrahamCampbell commented Sep 29, 2014

Better to not use these aliases and just use the full class names to be honest,

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Sep 29, 2014

Contributor

But that's what I was doing initially and problem only came up when I tried to use TwigBridge. I'm kind of stuck at that point. I bet everybody attempting to use TwigBridge with Laravel 5.0 would be stuck as well.

Contributor

aik099 commented Sep 29, 2014

But that's what I was doing initially and problem only came up when I tried to use TwigBridge. I'm kind of stuck at that point. I bet everybody attempting to use TwigBridge with Laravel 5.0 would be stuck as well.

@barryvdh

This comment has been minimized.

Show comment
Hide comment
@barryvdh

barryvdh Sep 29, 2014

Collaborator

Those are different aliases, I don't mean the facades. You have to register the binding 'form' as an alias to the FormBuilder, something like $app->alias('form', 'Illuminate\Html\FormBuilder')
That way the app container knows it can resolve that shared binding when we ask for the FormBuilder.

Collaborator

barryvdh commented Sep 29, 2014

Those are different aliases, I don't mean the facades. You have to register the binding 'form' as an alias to the FormBuilder, something like $app->alias('form', 'Illuminate\Html\FormBuilder')
That way the app container knows it can resolve that shared binding when we ask for the FormBuilder.

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Sep 29, 2014

Contributor

I think I understand now why it worked in Laravel 4.2. The HtmlServiceProvider put a closure on form service name that would get current token from session and pass it as 3rd argument when creating service in https://github.com/laravel/framework/blob/4.2/src/Illuminate/Html/HtmlServiceProvider.php#L46

Thanks to the aliasing of FormBuilder to form whoever tried to instantiate FormBuilder were hitting that closure instead that filled-in that missing 3rd parameter ($csrfToken).

Now with Laravel 5.0 the form alias is gone so whoever has type hint of FormBuilder in the constructor (luckily it's the TwigBundle's Form class) tries to instantiate the FormBuilder class directly and doesn't hit designated form closure that HtmlServiceProvider registers.

Contributor

aik099 commented Sep 29, 2014

I think I understand now why it worked in Laravel 4.2. The HtmlServiceProvider put a closure on form service name that would get current token from session and pass it as 3rd argument when creating service in https://github.com/laravel/framework/blob/4.2/src/Illuminate/Html/HtmlServiceProvider.php#L46

Thanks to the aliasing of FormBuilder to form whoever tried to instantiate FormBuilder were hitting that closure instead that filled-in that missing 3rd parameter ($csrfToken).

Now with Laravel 5.0 the form alias is gone so whoever has type hint of FormBuilder in the constructor (luckily it's the TwigBundle's Form class) tries to instantiate the FormBuilder class directly and doesn't hit designated form closure that HtmlServiceProvider registers.

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Sep 29, 2014

Contributor

Those are different aliases, I don't mean the facades. You have to register the binding 'form' as an alias to the FormBuilder, something like $app->alias('form', 'Illuminate\Html\FormBuilder')

Different aliases, I wasn't expecting that. I guess it's the service provider that needs to do that. So adding ``before https://github.com/laravel/framework/blob/4.2/src/Illuminate/Html/HtmlServiceProvider.php#L46 line (similar forregisterHtmlBuilder) should fix the problem:

$this->app->alias('form', 'Illuminate\Html\FormBuilder');

I think that providers should be doing that all the time and not having half of registration happen in provider and other half in the registerCoreContainerAliases method. That misplaced code revealed that problem when Html component was moved away from core.

Contributor

aik099 commented Sep 29, 2014

Those are different aliases, I don't mean the facades. You have to register the binding 'form' as an alias to the FormBuilder, something like $app->alias('form', 'Illuminate\Html\FormBuilder')

Different aliases, I wasn't expecting that. I guess it's the service provider that needs to do that. So adding ``before https://github.com/laravel/framework/blob/4.2/src/Illuminate/Html/HtmlServiceProvider.php#L46 line (similar forregisterHtmlBuilder) should fix the problem:

$this->app->alias('form', 'Illuminate\Html\FormBuilder');

I think that providers should be doing that all the time and not having half of registration happen in provider and other half in the registerCoreContainerAliases method. That misplaced code revealed that problem when Html component was moved away from core.

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Sep 29, 2014

Contributor

By the way that fixed a problem.

But I'm unsure where to send a PR, because in master branch there is no such Html component anymore (https://github.com/laravel/framework/tree/master/src/Illuminate) and on the illuminate company on github all components are marked as read-only (https://github.com/illuminate/html).

Contributor

aik099 commented Sep 29, 2014

By the way that fixed a problem.

But I'm unsure where to send a PR, because in master branch there is no such Html component anymore (https://github.com/laravel/framework/tree/master/src/Illuminate) and on the illuminate company on github all components are marked as read-only (https://github.com/illuminate/html).

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Oct 2, 2014

Contributor

@rcrowe , this should solved after both following PR's will be merged:

Contributor

aik099 commented Oct 2, 2014

@rcrowe , this should solved after both following PR's will be merged:

@plmarcelo

This comment has been minimized.

Show comment
Hide comment
@plmarcelo

plmarcelo Oct 3, 2014

Hello,

aik099 I got it to work just applying your first PR and adding this to my app.php

...
'providers' => [
    ....
    'Illuminate\Html\HtmlServiceProvider',
    ....
],
...
'aliases' => [
    ....
    'Form'      => 'Illuminate\Html\FormFacade',
    'HTML'      => 'Illuminate\Html\HtmlFacade',
    ....
],
...

Hello,

aik099 I got it to work just applying your first PR and adding this to my app.php

...
'providers' => [
    ....
    'Illuminate\Html\HtmlServiceProvider',
    ....
],
...
'aliases' => [
    ....
    'Form'      => 'Illuminate\Html\FormFacade',
    'HTML'      => 'Illuminate\Html\HtmlFacade',
    ....
],
...
@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Oct 3, 2014

Contributor

Yes, then Unresolved dependency exception (during artisan usage) indeed go away after first PR merge. However if you try to actually create a twig template and view it then without 2nd PR you'll get same exception again.

Contributor

aik099 commented Oct 3, 2014

Yes, then Unresolved dependency exception (during artisan usage) indeed go away after first PR merge. However if you try to actually create a twig template and view it then without 2nd PR you'll get same exception again.

@plmarcelo

This comment has been minimized.

Show comment
Hide comment
@plmarcelo

plmarcelo Oct 3, 2014

Nope, Right now I'm rendering twig templates with form on them and everything works fine.

Nope, Right now I'm rendering twig templates with form on them and everything works fine.

@plmarcelo

This comment has been minimized.

Show comment
Hide comment
@plmarcelo

plmarcelo Oct 3, 2014

Sorry, but now stop working even with PR2 and without it. Something really weird.

Sorry, but now stop working even with PR2 and without it. Something really weird.

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Oct 3, 2014

Contributor

Because you need to run artisan clear-complied and artisan optimize after changing stuff in config. At least I do. I guess if you change classes that go into compilation that makes sense.

Contributor

aik099 commented Oct 3, 2014

Because you need to run artisan clear-complied and artisan optimize after changing stuff in config. At least I do. I guess if you change classes that go into compilation that makes sense.

@plmarcelo

This comment has been minimized.

Show comment
Hide comment
@plmarcelo

plmarcelo Oct 3, 2014

Now I started it in debug mode and it worked and started to work again even without debug mode. Don't get why :-(

Now I started it in debug mode and it worked and started to work again even without debug mode. Don't get why :-(

@plmarcelo

This comment has been minimized.

Show comment
Hide comment
@plmarcelo

plmarcelo Oct 6, 2014

Thanks aik099, It worked as you suggested with yours PR's.

Thanks aik099, It worked as you suggested with yours PR's.

@rcrowe

This comment has been minimized.

Show comment
Hide comment
@rcrowe

rcrowe Oct 9, 2014

Owner

So firstly, sorry I've been so quite, I've just changed jobs & which means I've been super busy.

I had been reading some of the Github notifications but had naively presumed it was a case of adding the illuminate\html package.

I don't have the time right now, but this did just fix it in a project of mine. Just add it to one of your service providers:

    public function register()
    {
        $this->app->bind('Illuminate\Html\FormBuilder', function () {

            return new \Illuminate\Html\FormBuilder(
                $this->app->make('Illuminate\Html\HtmlBuilder'),
                $this->app->make('Illuminate\Routing\UrlGenerator'),
                csrf_token()
            );

        });
    }

I'm reluctant to spend any great time on 5 at the moment as things seem to be changing daily. Hope this helps for now.

Owner

rcrowe commented Oct 9, 2014

So firstly, sorry I've been so quite, I've just changed jobs & which means I've been super busy.

I had been reading some of the Github notifications but had naively presumed it was a case of adding the illuminate\html package.

I don't have the time right now, but this did just fix it in a project of mine. Just add it to one of your service providers:

    public function register()
    {
        $this->app->bind('Illuminate\Html\FormBuilder', function () {

            return new \Illuminate\Html\FormBuilder(
                $this->app->make('Illuminate\Html\HtmlBuilder'),
                $this->app->make('Illuminate\Routing\UrlGenerator'),
                csrf_token()
            );

        });
    }

I'm reluctant to spend any great time on 5 at the moment as things seem to be changing daily. Hope this helps for now.

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Oct 9, 2014

Contributor

This is another way to do this, yes. Thanks for getting back on the subject. There were so much changes in Laravel 5 lately so I switched back to 4.2 to be on the safe side.

Contributor

aik099 commented Oct 9, 2014

This is another way to do this, yes. Thanks for getting back on the subject. There were so much changes in Laravel 5 lately so I switched back to 4.2 to be on the safe side.

@jeanbelhache

This comment has been minimized.

Show comment
Hide comment
@jeanbelhache

jeanbelhache Mar 12, 2015

Since the commit of 12 march 2015, on the branch 0,7 the register function should be :

public function register()
    {
        $this->app->bind('Collective\Html\FormBuilder', function () {
            return new \Collective\Html\FormBuilder(
                $this->app->make('Collective\Html\HtmlBuilder'),
                $this->app->make('Illuminate\Routing\UrlGenerator'),
                csrf_token()
            );
        });
    }

Since the commit of 12 march 2015, on the branch 0,7 the register function should be :

public function register()
    {
        $this->app->bind('Collective\Html\FormBuilder', function () {
            return new \Collective\Html\FormBuilder(
                $this->app->make('Collective\Html\HtmlBuilder'),
                $this->app->make('Illuminate\Routing\UrlGenerator'),
                csrf_token()
            );
        });
    }
@barryvdh

This comment has been minimized.

Show comment
Hide comment
@barryvdh

barryvdh Mar 12, 2015

Collaborator

Can you submit a PR?

Collaborator

barryvdh commented Mar 12, 2015

Can you submit a PR?

@mehrdd

This comment has been minimized.

Show comment
Hide comment
@mehrdd

mehrdd Mar 14, 2015

i made an update and :

InvalidArgumentException in ServiceProvider.php line 194:
Cannot instantiate Twig extension 'TwigBridge\Extension\Laravel\Html': Class Collective\Html\HtmlBuilder does not exist

mehrdd commented Mar 14, 2015

i made an update and :

InvalidArgumentException in ServiceProvider.php line 194:
Cannot instantiate Twig extension 'TwigBridge\Extension\Laravel\Html': Class Collective\Html\HtmlBuilder does not exist
@jeanbelhache

This comment has been minimized.

Show comment
Hide comment
@jeanbelhache

jeanbelhache Mar 14, 2015

Hi mehrdd,

you have to include "laravelcollective/html": "~5.0" into your composer.

see http://laravelcollective.com/docs/5.0/html

Hi mehrdd,

you have to include "laravelcollective/html": "~5.0" into your composer.

see http://laravelcollective.com/docs/5.0/html

@MyIgel

This comment has been minimized.

Show comment
Hide comment
@MyIgel

MyIgel Mar 29, 2015

@jeanbelhache With laravelcollective/html also an InvalidArgumentException is thrown: Cannot instantiate Twig extension 'TwigBridge\Extension\Laravel\Form': Unresolvable dependency resolving [Parameter #2 [ <required> $csrfToken ]] in class Collective\Html\FormBuilder

But with

$this->app->bind('Collective\Html\FormBuilder', function () {
            return new \Collective\Html\FormBuilder(
                $this->app->make('Collective\Html\HtmlBuilder'),
                $this->app->make('Illuminate\Routing\UrlGenerator'),
                csrf_token()
            );
        });

In a service provider it works 👊 😑

MyIgel commented Mar 29, 2015

@jeanbelhache With laravelcollective/html also an InvalidArgumentException is thrown: Cannot instantiate Twig extension 'TwigBridge\Extension\Laravel\Form': Unresolvable dependency resolving [Parameter #2 [ <required> $csrfToken ]] in class Collective\Html\FormBuilder

But with

$this->app->bind('Collective\Html\FormBuilder', function () {
            return new \Collective\Html\FormBuilder(
                $this->app->make('Collective\Html\HtmlBuilder'),
                $this->app->make('Illuminate\Routing\UrlGenerator'),
                csrf_token()
            );
        });

In a service provider it works 👊 😑

@jeanbelhache

This comment has been minimized.

Show comment
Hide comment
@jeanbelhache

jeanbelhache Mar 30, 2015

@MyIgel Yes, i mentioned that issue in a pull request #184

@MyIgel Yes, i mentioned that issue in a pull request #184

@enchance

This comment has been minimized.

Show comment
Hide comment
@enchance

enchance Mar 31, 2015

@MyIgel I followed your directions and modified register() in the ServiceProvider.php file but I'm getting a ReflectionException in Container.php line 776: Class twig.extension does not exist error. Did I miss anything? I'm using L5.

@MyIgel I followed your directions and modified register() in the ServiceProvider.php file but I'm getting a ReflectionException in Container.php line 776: Class twig.extension does not exist error. Did I miss anything? I'm using L5.

@amosmos

This comment has been minimized.

Show comment
Hide comment
@amosmos

amosmos Jul 28, 2015

Hi @aik099, Seemes like you're the expert on this issue so out of curiosity I would be happy if you could explain to me what happened here.

  1. It seems like the problem was with TwigBridge type hinting FormBuilder class, while the FormBuilder class constructor requires the $csrf_token paramater. But if I understand correctly type hinting works by using the name that you bind to the service container and not the actual class name. And since the htmlServiceProvider is binding the formBuilder to the name 'form' - wasn't TwigBridge needed to type hint form $form instead of FormBuilder $form to solve the issue?
  2. Even if aliasing 'form' to 'FormBuilder' class actually somehow solved TwigBridge issue with constructing a FormBuilder, why wasn't adding this alias to the config/app.aliases solved it? Why did it had to be aliased in the service provider?

Hope my questions made sense,
Thanks!

amosmos commented Jul 28, 2015

Hi @aik099, Seemes like you're the expert on this issue so out of curiosity I would be happy if you could explain to me what happened here.

  1. It seems like the problem was with TwigBridge type hinting FormBuilder class, while the FormBuilder class constructor requires the $csrf_token paramater. But if I understand correctly type hinting works by using the name that you bind to the service container and not the actual class name. And since the htmlServiceProvider is binding the formBuilder to the name 'form' - wasn't TwigBridge needed to type hint form $form instead of FormBuilder $form to solve the issue?
  2. Even if aliasing 'form' to 'FormBuilder' class actually somehow solved TwigBridge issue with constructing a FormBuilder, why wasn't adding this alias to the config/app.aliases solved it? Why did it had to be aliased in the service provider?

Hope my questions made sense,
Thanks!

@amosmos

This comment has been minimized.

Show comment
Hide comment
@amosmos

amosmos Jul 28, 2015

@aik099

The more I think about it I just don't understand why do TwigBridge's type hinting was made directly to the FormBuilder class and not to the name that got bind for it ("form").

It just seems to me that the addition to the illuminate/html package was not needed. Yes it did solved TwigBridge issue (which I actually don't even understand how as it still uses the "form" alias), but this should have been fixed on TwigBridge side.

But since so many experts were involved in this issue, I probably got something wrong and I just really wonder what it is....

Please let me know what you think,
Thanks again!

amosmos commented Jul 28, 2015

@aik099

The more I think about it I just don't understand why do TwigBridge's type hinting was made directly to the FormBuilder class and not to the name that got bind for it ("form").

It just seems to me that the addition to the illuminate/html package was not needed. Yes it did solved TwigBridge issue (which I actually don't even understand how as it still uses the "form" alias), but this should have been fixed on TwigBridge side.

But since so many experts were involved in this issue, I probably got something wrong and I just really wonder what it is....

Please let me know what you think,
Thanks again!

@aik099

This comment has been minimized.

Show comment
Hide comment
@aik099

aik099 Jul 28, 2015

Contributor

Sorry, but I'm not using Laravel anymore so I'm not aware if this issue was fixed or not.

Contributor

aik099 commented Jul 28, 2015

Sorry, but I'm not using Laravel anymore so I'm not aware if this issue was fixed or not.

@amosmos

This comment has been minimized.

Show comment
Hide comment
@amosmos

amosmos Jul 28, 2015

Got it :-)

Yes, It was fixed. But it was fix with the addition of a manual alias to the html class, while in my opinion the fix should have been on the TwigBridge class.

I'm just curious where I'm wrong in thinking the applied fix is not the right one....

Thanks!

amosmos commented Jul 28, 2015

Got it :-)

Yes, It was fixed. But it was fix with the addition of a manual alias to the html class, while in my opinion the fix should have been on the TwigBridge class.

I'm just curious where I'm wrong in thinking the applied fix is not the right one....

Thanks!

amosmos referenced this issue in illuminate/html Jul 28, 2015

Merge pull request #12 from aik099/register-container-aliases-fix
Creating missing container aliases from "registerCoreContainerAliases" method
@MyIgel

This comment has been minimized.

Show comment
Hide comment
@MyIgel

MyIgel Aug 11, 2015

@enchance the fix was to add a app/Providers/FormServiceProvider.php with the following content:

<?php namespace AppNamespace\Providers;

use Collective\Html\FormBuilder;
use Illuminate\Support\ServiceProvider;

/**
 * Class FormServiceProvider
 *
 * Fix for https://github.com/rcrowe/TwigBridge/issues/150
 *
 * @package AppNamespace\Providers
 */
class FormServiceProvider extends ServiceProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bindIf('Collective\Html\FormBuilder', function () {
            return new FormBuilder(
                $this->app->make('Collective\Html\HtmlBuilder'),
                $this->app->make('Illuminate\Routing\UrlGenerator'),
                csrf_token()
            );
        });
    }
}

MyIgel commented Aug 11, 2015

@enchance the fix was to add a app/Providers/FormServiceProvider.php with the following content:

<?php namespace AppNamespace\Providers;

use Collective\Html\FormBuilder;
use Illuminate\Support\ServiceProvider;

/**
 * Class FormServiceProvider
 *
 * Fix for https://github.com/rcrowe/TwigBridge/issues/150
 *
 * @package AppNamespace\Providers
 */
class FormServiceProvider extends ServiceProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bindIf('Collective\Html\FormBuilder', function () {
            return new FormBuilder(
                $this->app->make('Collective\Html\HtmlBuilder'),
                $this->app->make('Illuminate\Routing\UrlGenerator'),
                csrf_token()
            );
        });
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment