Skip to content

Corrected wrong class used in "Locating Resources" #11969

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

Merged
merged 1 commit into from
Sep 20, 2019

Conversation

filip-t
Copy link
Contributor

@filip-t filip-t commented Jul 16, 2019

HttpKernel does not contain a locateResource method.

@javiereguiluz
Copy link
Member

@filip-t thanks for fixing this bug! You are right that HttpKernel class doesn't contain this method, but the interface class doesn't contain it either. The method is in the Kernel class (see https://github.com/symfony/symfony/blob/e5bd6ff59e1bf32b4af644d8025827900bdbc276/src/Symfony/Component/HttpKernel/Kernel.php#L254) So, could you please update your pull request to use Kernel instead of KernelInterface? Thanks.

@filip-t
Copy link
Contributor Author

filip-t commented Jul 17, 2019

But HttpKernel/Kernel is an abstract class. I tried to use it, but it didn't work. Using KernelInterface fixed the problem. I'm not sure it's the correct way to do it, but this is the only way I could make it work.

@filip-t
Copy link
Contributor Author

filip-t commented Jul 24, 2019

I checked again and using the Kernel class gives this exception. It even suggests using KernelInterface instead.

image

@maxhelias
Copy link
Contributor

An interface is not an instantiable object. You should use the Kernel of the project : \App\Kernel

@filip-t
Copy link
Contributor Author

filip-t commented Aug 8, 2019

Firstly - I just realised I forked Symfony 3.4, not 4.3, as I intended to do. I don't know how that happened. Looks like this applies to both versions.
Should I make a separate pull request for 4.3 then?
Secondly = I think I can see what I was doing wrong here. In my code, I was injecting KernelInterface into a service through the constructor to access a resource file. Here. a Kernel object is being created. I completely blanked on it. I could swear this code example looked different when I forked it, but I even checked history and nothing has changed.

@OskarStark OskarStark added this to the 3.4 milestone Aug 15, 2019
@OskarStark
Copy link
Contributor

@nicolas-grekas this looks good to me can you confirm please? Thank you


// ...
$kernel = new HttpKernel($dispatcher, $resolver);
$kernel = new Kernel($dispatcher, $resolver);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that cannot be correct: Kernel doesn't take these as arguments

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm you are right... 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The more I look at this, the more problems I see with my request. Maybe this code example should be rewritten from scratch? I don't really see anybody creating a new Kernel instance just to get a path to a config file.

javiereguiluz added a commit that referenced this pull request Sep 20, 2019
…p-t)

This PR was squashed before being merged into the 3.4 branch (closes #11969).

Discussion
----------

Corrected wrong class used in "Locating Resources"

HttpKernel does not contain a locateResource method.

<!--

If your pull request fixes a BUG, use the oldest maintained branch that contains
the bug (see https://symfony.com/roadmap for the list of maintained branches).

If your pull request documents a NEW FEATURE, use the same Symfony branch where
the feature was introduced (and `master` for features of unreleased versions).

-->

Commits
-------

0a7b27a Corrected wrong class used in \"Locating Resources\"
@javiereguiluz javiereguiluz merged commit 0a7b27a into symfony:3.4 Sep 20, 2019
@javiereguiluz
Copy link
Member

@filip-t in practice you need to get the entire kernel instance to use the locateResource(). I haven't seen used this much in real apps ... but when I saw it, they injected the entire kernel service. So, you either create the whole kernel yourself or get it injected. After re-reading this article, I saw that the $kernel variable was created in a previous section showing how to create the kernel ... so I reworded this example as:

$path = $kernel->locateResource('@AppBundle/Resources/config/services.xml');

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

Successfully merging this pull request may close these issues.

6 participants