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

I followed the doc of 4.2. MicroKernelTrait throws error #10858

Closed
featuriz opened this issue Jan 10, 2019 · 7 comments
Closed

I followed the doc of 4.2. MicroKernelTrait throws error #10858

featuriz opened this issue Jan 10, 2019 · 7 comments
Labels
bug hasPR A Pull Request has already been submitted for this issue. Status: Needs Review
Milestone

Comments

@featuriz
Copy link
Contributor

I'm following a doc of symonfy 4.2 MicroKernelTrait
Dated: Jan 10 2019 08:45 AM India ISD.
This causing 2 error.

(1/2) ArgumentCountError

Too few arguments to function Symfony\Component\HttpKernel\Kernel::__construct(), 0 passed in /home/sudhakar/SudhakarK/NB/2019/FZFW/vendor/symfony/http-kernel/Controller/ControllerResolver.php on line 133 and exactly 2 expected

(2/2) InvalidArgumentException

Controller "Kernel" has required constructor arguments and does not exist in the container. Did you forget to define such a service?

How to reproduce:
I followd the turorial of the mentioned page with 4.2 symfony version.

The error is when running http://localhost:8000/random/10
But http://localhost:8000/ works

Thanks.

@featuriz
Copy link
Contributor Author

The same error is spotted here

Here the solution is creating a service.
But in learning starting point creating a service for newcome is ??? not good.

@featuriz
Copy link
Contributor Author

I solved this problem with this code. But I don't know this is correct or not . But now its working. I added the construct()

`<?php

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;

require DIR . '/vendor/autoload.php';

class Kernel extends BaseKernel
{
use MicroKernelTrait;

public function __construct(){
    parent::__construct('dev', true);
}

public function registerBundles()
{
    return array(
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
    );
}

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
{
    // PHP equivalent of config/packages/framework.yaml
    $c->loadFromExtension('framework', array(
        'secret' => 'S0ME_SECRET',
    ));
}

protected function configureRoutes(RouteCollectionBuilder $routes)
{
    // kernel is a service that points to this class
    // optional 3rd argument is the route name
    $routes->add('/random/{limit}', 'Kernel::randomNumber');
}

public function randomNumber($limit)
{
    return new JsonResponse(array(
        'number' => random_int(0, $limit),
    ));
    // return new Response("WElcome");
}

}

$kernel = new Kernel('dev', true);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);`

@xabbuh
Copy link
Member

xabbuh commented Jan 10, 2019

The issue is the route configuration:

protected function configureRoutes(RouteCollectionBuilder $routes)
{
    // kernel is a service that points to this class
    // optional 3rd argument is the route name
    $routes->add('/random/{limit}', 'Kernel::randomNumber');
}

The controller resolver does not pick the service from the container but tries to instantiate the Kernel class here which then fails as no arguments will be passed.

@featuriz Can you confirm that changing the route configuration to $routes->add('/random/{limit}', 'kernel::randomNumber'); solves this?

@xabbuh xabbuh added this to the 4.1 milestone Jan 10, 2019
@xabbuh xabbuh added the bug label Jan 10, 2019
@featuriz
Copy link
Contributor Author

featuriz commented Jan 13, 2019

The issue is the route configuration:

protected function configureRoutes(RouteCollectionBuilder $routes)
{
    // kernel is a service that points to this class
    // optional 3rd argument is the route name
    $routes->add('/random/{limit}', 'Kernel::randomNumber');
}

The controller resolver does not pick the service from the container but tries to instantiate the Kernel class here which then fails as no arguments will be passed.

@featuriz Can you confirm that changing the route configuration to $routes->add('/random/{limit}', 'kernel:randomNumber'); solves this?

No Its Not working. I have used this in the previous post itself.

Yes you are Right. I found that the error is in 2nd argument. It can't find the method in the same file.

@xabbuh
Copy link
Member

xabbuh commented Jan 13, 2019

Are you sure? In the previous post you use Kernel (notice the uppercased K and two colons).

@guillaumesmo
Copy link
Contributor

guillaumesmo commented Jan 17, 2019

Had the same issue, using kernel in lowercase fixed it for me indeed, both with single and double semicolons. Which one is the preferred solution? I'll make a PR

@xabbuh
Copy link
Member

xabbuh commented Jan 17, 2019

Thank you for the confirmation @guillaumesmo! Let's use two colons as we use this notation everywhere else too (I updated my comment above accordingly).

@xabbuh xabbuh added the hasPR A Pull Request has already been submitted for this issue. label Jan 17, 2019
javiereguiluz added a commit that referenced this issue Jan 17, 2019
This PR was merged into the 4.1 branch.

Discussion
----------

Update micro_kernel_trait.rst

Fixes #10858

this was previously (in 4.0) kernel:randomNumber, it was changed to Kernel::randomNumber but that doesn't work

<!--

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
-------

337c59c Update micro_kernel_trait.rst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug hasPR A Pull Request has already been submitted for this issue. Status: Needs Review
Projects
None yet
Development

No branches or pull requests

4 participants