1

I'm trying to create a simple login. I created my Entity and had the form displaying on the page. This is the link I followed: http://symfony.com/doc/2.8/cookbook/security/form_login_setup.html

Error

This is the error I get when I hit the controller:

Error: Call to a member function get() on a non-object

After a bit of research and digging I discovered that because I am extending to the symfony controller that then in turn extends ContainerAware. I'm guessing that this is the issue?

Controller

<?php

namespace AppBundle\Controller\Security;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class SecurityController extends Controller
{
public function loginAction(Request $request)
{
    $authenticationUtils = $this->get('security.authentication_utils');

    // get the login error if there is one
    $error = $authenticationUtils->getLastAuthenticationError();

    // last username entered by the user
    $lastUsername = $authenticationUtils->getLastUsername();

    return $this->render(
        'AppBundle:Loginpage:index.html.twig',
        array(
            // last username entered by the user
            'last_username' => $lastUsername,
            'error'         => $error,
        )
    );
}

public function loginCheckAction()
{
    //no logic needed
}
}

Service

<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
    <service id="controller.site.loginpage" class="AppBundle\Controller\Security\SecurityController">
        <argument type="service" id="service_container" />
        <argument type="service" id="templating"/>
    </service>
</services>

Routing

<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/routing  http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="loginpage" path="/login">
    <default key="_controller">
        controller.site.loginpage:loginAction
    </default>
</route>
6
  • can you put the line number of the error Commented Jan 6, 2016 at 15:07
  • Are you defining the controller as a service? If so then you need to use setContainer in your service definition. Commented Jan 6, 2016 at 15:09
  • in vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php at line 391 Commented Jan 6, 2016 at 15:14
  • Show us the route to controller, as @Cerad said, it's really possible that you call controller via service, that's why you haven't container injected. Commented Jan 6, 2016 at 16:09
  • Ahhhh okay ill add it in above - i am using my controller as a service, wasnt aware that i had to inject the container tho, not entirely sure how its done, is it an argument on the service? Commented Jan 6, 2016 at 16:30

1 Answer 1

4

Check the createController() method in Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver.

It will give you a quick idea how your controller is initialized/returned. If you defined it as a service, the setContainer() method will not be called and get() method will be called on a non-object (null).

When you define controller as a service, you usually do NOT want to inject the whole container, but only specific services.

If you are just beginning to use Symfony2 (like I assume), I would suggest not defining it as a service, so you can keep things simple.

Sign up to request clarification or add additional context in comments.

2 Comments

Add calls: [[setContainer, ['@service_container']]] to your service definition to inject the controller. I disagree that with the statement they you would not normally inject the whole container. But that is a different discussion.
I hope my answer helped. I studied a lot about controllers in Symfony. Yes, there is a lot to discuss about injecting container, and I agree, it's a different topic... and like I said "usually". :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.