8000 Update multiple_kernels.rst by amine-betari · Pull Request #12724 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Update multiple_kernels.rst #12724

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
Oct 21, 2020
Merged

Update multiple_kernels.rst #12724

merged 1 commit into from
Oct 21, 2020

Conversation

amine-betari
Copy link
Contributor
@amine-betari amine-betari commented Nov 27, 2019

Error: Class App\ApiKernel contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (App\ApiKernel::configureRoutes)

N.B :
An abstract method can not contain a body, its presence simply indicates that each non-abstract daughter class will have to redefine this method by respecting its signature.

@OskarStark
Copy link
Contributor

Thanks for your contribution. Can you explain why we should add this? Thanks

@OskarStark OskarStark added this to the 4.3 milestone Nov 28, 2019
@amine-betari
Copy link
Contributor Author

@OskarStark

Because without the added code, we will have the following error :

Error: Class App\ApiKernel contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (App\ApiKernel::configureRoutes)

N.B :
An abstract method can not contain a body, its presence simply indicates that each non-abstract daughter class will have to redefine this method by respecting its signature.

@javiereguiluz
Copy link
Member

@amine-betari thanks for this contribution ... but I'm having some problems trying to understand the change.

In this example, the code extends from Kernel (https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/Kernel.php) which doesn't define these methods as abstract. When using the MicroKernelTrait (https://symfony.com/doc/current/configuration/micro_kernel_trait.html) you import the MicroKernelTrait (https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php) which defines an abstract configureContainer() method, but configureRouting () is not abstract.

@amine-betari
Copy link
Contributor Author

Hi @javiereguiluz

it's normal because your links pointenst to master which is branch 5.0 I think

On the other hand if you check the version by putting 4.3 or 4.4 you will have well the error

https://github.com/symfony/symfony/blob/4.4/src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php

@javiereguiluz
Copy link
Member

@amine-betari sorry! You are right.

However, in this article we don't use the MicroKernelTrait but the normal Kernel class, right?

@amine-betari
Copy link
Contributor Author
amine-betari commented Nov 29, 2019

Hi @javiereguiluz

For me, you just have to add
use MicroKernelTrait;

also add the configureRoutes method

Why ?

Because if you do not use the MicroKernelTrait and extend the Kernel class you will have another problem which is the following one:

Error: Class App\ApiKernel contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Symfony\Component\HttpKernel\KernelInterface::registerContainerConfiguration)

Because the MicroKernelTrait defines the method: registerContainerConfiguration

Exemple
`class ApiKernel extends BaseKernel
{
use MicroKernelTrait;
private const CONFIG_EXTS = '.{php,xml,yaml,yml}';

public function registerBundles(): iterable
{
    $contents = require $this->getProjectDir().'/config/bundles.php';
    foreach ($contents as $class => $envs) {
        if ($envs[$this->environment] ?? $envs['all'] ?? false) {
            yield new $class();
        }
    }
}

public function getProjectDir(): string
{
    return \dirname(__DIR__);
}

public function getCacheDir()
{
    return dirname(__DIR__).'/var/cache/api/'.$this->getEnvironment();
}

public function getLogDir()
{
    return dirname(__DIR__).'/var/log/api';
}

protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
    // load only the config files strictly needed for the API
    $confDir = $this->getProjectDir().'/config';
    $loader->load($confDir.'/api/*'.self::CONFIG_EXTS, 'glob');
    if (is_dir($confDir.'/api/'.$this->environment)) {
        $loader->load($confDir.'/api/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob');
    }
    $loader->load($confDir.'/{services}'.self::CONFIG_EXTS, 'glob');
}


protected function configureRoutes(RouteCollectionBuilder $routes): void
{
    $confDir = $this->getProjectDir().'/config';
    // load config routes
      .......
}

}
`

@OskarStark
Copy link
Contributor

Close/reopen because Travis was stuck

@OskarStark OskarStark closed this Dec 3, 2019
@OskarStark OskarStark reopened this Dec 3, 2019
@amine-betari
Copy link
Contributor Author

@ostark @javiereguiluz any news ?

@amine-betari
Copy link
Contributor Author

@ostark @javiereguiluz any news please ?

@ostark
Copy link
ostark commented Jan 13, 2020

FYI @amine-betari you used the wrong handle, I'm not Oskar 8000 .

@OskarStark
Copy link
Contributor

It’s me 😊

I am currently on a phone 📱 but will have a look later

@HeahDude
Copy link
Contributor

@amine-betari, thank you for working on this. I agree we should update the example to use the MicroKernelTrait instead.

But I propose the following changes:

public function registerBundles(): iterable
{
    $contents = require $this->getProjectDir().'/config/api_bundles.php';
    // ...
}

// ...

protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
    {
        $container->addResource(new FileResource($this->getProjectDir().'/config/api_bundles.php'));
        $container->setParameter('container.dumper.inline_factories', true);
        $confDir = $this->getProjectDir().'/config/api';

        $loader->load($confDir.'/{packages}/*'.self::CONFIG_EXTS, 'glob');
        $loader->load($confDir.'/{packages}/'.$this->environment.'/*'.self::CONFIG_EXTS, 'glob');
        $loader->load($confDir.'/{services}'.self::CONFIG_EXTS, 'glob');
        $loader->load($confDir.'/{services}_'.$this->environment.self::CONFIG_EXTS, 'glob');
    }

protected function configureRoutes(RouteCollectionBuilder $routes): void
{
    $confDir = $this->getProjectDir().'/config/api';
    // load specific api routes
}

We could even add the routes configuration since we fully implement configureContainer, for completeness?

@wouterj
Copy link
Member
wouterj commented Oct 4, 2020

Hi @amine-betari! Do you have any time/motivation to finish this PR based on the comments by @HeahDude?

(please also explicitly add use MicroKernelTrait; to the example, to make it clear that the example is based on the micro kernel)

@amine-betari
Copy link
Contributor Author

@wouterj
I'll do this ASAP (this week). We like community so we're ready to give our time :)

@amine-betari
Copy link
Contributor Author

FYI @amine-betari you used the wrong handle, I'm not Oskar.

Sorry man :)

@amine-betari
Copy link
Contributor Author

@wouterj done :)

@wouterj wouterj changed the base branch from 4.3 to 4.4 October 21, 2020 14:38
@wouterj
Copy link
Member
wouterj commented Oct 21, 2020

Thank you Amine! Fyi, I've tweaked the diff slightly during the merge.

@wouterj wouterj merged commit aad53f5 into symfony:4.4 Oct 21, 2020
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.

7 participants
0