Skip to content
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

[8.x] Allow Blade's service injection to inject services typed using class name resolution #36356

Merged
merged 1 commit into from
Feb 23, 2021

Conversation

crnkovic
Copy link
Contributor

Right now, Blade supports service injection via @inject directive. Example:

@inject ('service', 'App\SomeService')

Due to the fact that the contents of the expression were parsed/segmented then passed into the app() helper but wrapped into quotes, doing something like:

@inject ('service', App\SomeService::class)

Was not possible as the compiled output would be: $service = app('App\SomeService::class') resulting in an error. This PR adds the ability to inject services using this syntax, as well as keeping the old syntax. The way it's solved is that, instead of preg_replacing quotes, we'd pass the exact expression into the app() helper without wrapping into quotes, while the variable part ($service) is trimmed of all whitespace as well as quotes.

Any breaking changes

The only potentially breaking change I can think of would be if someone accidentally added a whitespace after opening quotation mark:

@inject ('service', ' App\SomeService')

This would result in calling app(' App\SomeService'), while previously this would call app('App\SomeService'), but I guess that could be fixed by preg_replacing all whitespace. Let me know.

@taylorotwell taylorotwell merged commit 11d7c7e into laravel:8.x Feb 23, 2021
@jasonmccreary
Copy link
Contributor

jasonmccreary commented Mar 23, 2021

Reported via Shift support, this does produce a breaking change. Previously the following was allowed:

@inject('stats', App\Helpers\DashboardStats)

After this merged, it produces an error:

Exception 'Facade\Ignition\Exceptions\ViewException' with message 'Undefined constant 'App\Helpers\LiveDashboardStats'

This was a rather odd (undocumented) syntax. So I am not advocating this be reverted. Only adding it here for reference in case future users run into this error.

If you were using this syntax, you may update the code to:

@inject('stats', App\Helpers\DashboardStats::class)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants