jwondrusch's avatar

Lumen and Illuminate\HTML?

Hey everyone,

Been playing around with Lumen on a small one-off project and I'm trying to include Illuminate\Html in order to use HTML::func in my views. Can anyone give me a quick 1, 2, 3 of how to make this accessible in views for Lumen?

0 likes
21 replies
jwondrusch's avatar

Thank you :) I had been using the Illuminate package instead of that one. Now that I have that added to the project, I've registered it as a provider in my bootstrap/app.php file:

$app->register('Collective\Html\HtmlServiceProvider');

But I'm unsure of how to make the facade available in this step:

'aliases' => [
    'Form' => 'Collective\Html\FormFacade',
    'Html' => 'Collective\Html\HtmlFacade',
]

Thanks again for your help :)

bobbybouwmann's avatar

I think you can do something like this

$app->bind('form', function ($app) {
    return new Collective\Html\FormFacade($app);
});
jwondrusch's avatar

Thanks for the link, I had checked that out, maybe something isn't clicking in my brain.

Tried this:

$app->register('Illuminate\Html\HtmlServiceProvider');

$app->bind('Form', function ($app) {
    return new Collective\Html\FormFacade($app);
});

$app->bind('Html', function ($app) {
    return new Collective\Html\HtmlFacade($app);
});

And then in my view, I added this:

{{ Html::style('style.css') }}

And in my logs, it shows:

Class 'Html' not found in ~/dev/lumen/storage/framework/views/5dfbceab0278538b5d8e09c8cca96c49 on line 15

Thank you again for your help.

bobbybouwmann's avatar

Mmmh, haven't tried to import this kind of stuff into Lumen since I would only use it as an API ;)

bashy's avatar

Maybe need to composer dump-autoload? That's if you've confirmed the class exists.

bobbybouwmann's avatar

Maybe you need to try something like this:

$app->bind('form', function ($app) {
    return new Collective\Html\FormBuilder();
});

Or this:

{!! Collective\Html\FormBuilder->open(['method' => 'POST']) !!}
jwondrusch's avatar

@blackbird Yeah, I understand :) Haven't had any issue using this stuff w/ Laravel, just less info on it w/ Lumen. I'll probably stick w/ Laravel for this, it was just a quick 2-3 page app for sales offers, thought a microframework would be more than enough to handle it. I just manually placed the stylesheet, and that's good enough for now. Just wish I knew how/why to get those providers and their facades in there.

@bashy Verified the file's existence, updated composer and dumped autoload.

bobbybouwmann's avatar

@jwondrusch Did you look at my previous reply? I think we loaded the wrong class name since we tried to load the facade, but we need to load the class of course since we create the facade our self right now :P

jwondrusch's avatar

@blackbird Yeah, I saw it right after I replied.

Tried the following:

{{ Collective\Html\HtmlBuilder->style('style.css') }} and {!! Collective\Html\HtmlBuilder->style('style.css') !!}

And got another error:

syntax error, unexpected '->' (T_OBJECT_OPERATOR) in ~/dev/lumen/storage/framework/views/5dfbceab0278538b5d8e09c8cca96c49 on line 16

Corrected the $app->register call to refer to the collective class, but that didn't do the trick, with the full path or the facade.

bobbybouwmann's avatar

So I have been looking around in the code of Lumen and I found this

public function withFacades()
{
    Facade::setFacadeApplication($this);

    if ( ! static::$aliasesRegistered) {
        static::$aliasesRegistered = true;
        class_alias('Illuminate\Support\Facades\App', 'App');
        class_alias('Illuminate\Support\Facades\Auth', 'Auth');
        class_alias('Illuminate\Support\Facades\Bus', 'Bus');
        class_alias('Illuminate\Support\Facades\DB', 'DB');
        class_alias('Illuminate\Support\Facades\Cache', 'Cache');
        class_alias('Illuminate\Support\Facades\Crypt', 'Crypt');
        class_alias('Illuminate\Support\Facades\Log', 'Log');
        class_alias('Illuminate\Support\Facades\Mail', 'Mail');
        class_alias('Illuminate\Support\Facades\Queue', 'Queue');
        class_alias('Illuminate\Support\Facades\Request', 'Request');
        class_alias('Illuminate\Support\Facades\Schema', 'Schema');
        class_alias('Illuminate\Support\Facades\Session', 'Session');
        class_alias('Illuminate\Support\Facades\Storage', 'Storage');
        class_alias('Illuminate\Support\Facades\Validator', 'Validator');
    }
}

So the next thing you need to do is uncomment $app->withFacades() in your bootstrap/app.php file and do something like this in your config

class_alias('Collective\Html\HtmlBuilder', 'Html');

Note: I didn't test this but you can give it a try ;)

jwondrusch's avatar

I did have facades uncommented - and adding in that line gave a different error - which is progress, right?!

Non-static method Collective\Html\HtmlBuilder::style() should not be called statically, assuming $this from incompatible context

Here's what I have right now:

// bootstrap/app.php
$app->register('Collective\Html\HtmlServiceProvider');

class_alias('Collective\Html\HtmlBuilder', 'Html');

$app->bind('Html', function ($app) {
    return new Collective\Html\HtmlFacade($app);
});

// layout/master.blade.php
...
{{ Html::style('style.css') }}
...
bobbybouwmann's avatar

I think you don't need the bind anymore when you use class_alias, but I'm not sure!

If I have some time left tomorrow I will look into it for you. No promises ;)

jwondrusch's avatar

@blackbird Removed the bind and had the same non-static error, so you're probably right on not needing it. Thank you again for the feedback on this. Most appreciated.

jrean's avatar

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

Looking to install this package in Lumen? First of all, making this package compatible with Lumen will require some core changes to Lumen, which we believe would dampen the effectiveness of having Lumen in the first place. Secondly, it is our belief that if you need this package in your application, then you should be using Laravel anyway.

:)

ktquez's avatar

You have already achieved? I managed to make it work, put the solution in moments.

ktquez's avatar

I would talk about this same package, the Artisans Laravel BR. Thank you!

paulredmond's avatar

You could do something like this in a provider:

if (!self::$aliasesRegistered) {
    self::$aliasesRegistered = true;
    class_alias('Illuminate\Support\Facades\Redis', 'Redis');
}

I've only started testing it, but it should work. If you throw class_alias(); calls into bootstrap/app.php you are going to get ErrorException: Cannot redeclare class when you run tests.

Please or to participate in this conversation.