scottie320
scottie320

Reputation: 173

Symfony: "Call to a member function get () on null" error after autowire implementation

When I changed to the automatic wiring setting in Symfony 3.4, the following error occurred.
The work I did was $this->get('security.authorization_checker') to $authChecker and composer.json psr-4 from " ":"src/" to"App\\":"src/".
I changed the route of twig accordingly. Also, I did composer dump-autoload.
Are there any other settings to change?
It seems that generateUrl other than SecurityController.php is not working either.
bin/console debug:router seems to be fine.
Error

Call to a member function get() on null

in ControllerTrait.php line 87
at Controller->generateUrl('app_ahi_sp_admin_hq_default_index', array(), 0)
in SecurityController.php line 40

SecurityController.php

namespace App\Ahi\Sp\AdminBundle\Controller;

class SecurityController extends BaseController
{

    /**
     *
     * @Route("/login")
     * @Template("AppAhiSpAdminBundle:Security:login.html.twig")
     */
    public function loginAction(Request $request, AuthorizationCheckerInterface $authChecker, TranslatorInterface $translator)
    {
        // Redirected to TOP if logged in
        if ($authChecker->isGranted('ROLE_HQ_MANAGE')) {
            return $this->redirect($this->generateUrl('app_ahi_sp_admin_hq_default_index', array(), UrlGeneratorInterface::ABSOLUTE_URL));
        } elseif ($authChecker->isGranted('ROLE_SHOP_STAFF')) {
            return $this->redirect($this->generateUrl('app_ahi_sp_admin_shop_default_index', array(), UrlGeneratorInterface::ABSOLUTE_URL));
        }
    }

services.yml

services:
  # this makes public all the services defined in this file
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    App\:
        resource: '../../src/*'
        # you can exclude directories or files
        # but if a service is unused, it's removed anyway
        exclude: '../../src/{Entity,Repository,Ahi/Sp/AdminBundle/Resources, Ahi/Sp/CommonBundle/Resources, Ahi/Sp/PublicBundle/ }'

    App\Ahi\Sp\AdminBundle\Controller\:
        resource: '../../src/Ahi/Sp/AdminBundle/Controller'
        public: true
        tags: ['controller.service_arguments']

routing.yml

app_ahi_sp_admin:
  resource: '@AppAhiSpAdminBundle/Controller/'
  type: annotation
  prefix: /admin/
  schemes: [http]

Result of php bin/console router:match /admin/hq/

$php bin/console router:match /admin/hq/
 [OK] Route "app_ahi_sp_admin_hq_default_index" matches                     
                                                                            

+--------------+---------------------------------------------------------------------+
| Property     | Value                                                               |
+--------------+---------------------------------------------------------------------+
| Route Name   | app_ahi_sp_admin_hq_default_index                                   |
| Path         | /admin/hq/                                                          |
| Path Regex   | #^/admin/hq/$#sD                                                    |
| Host         | ANY                                                                 |
| Host Regex   |                                                                     |
| Scheme       | http                                                                |
| Method       | GET                                                                 |
| Requirements | NO CUSTOM                                                           |
| Class        | Symfony\Component\Routing\Route                                     |
| Defaults     | _controller: AppAhiSpAdminBundle:Hq\Default:index                   |
| Options      | compiler_class: Symfony\Component\Routing\RouteCompiler             |
| Callable     | App\Ahi\Sp\AdminBundle\Controller\Hq\DefaultController::indexAction |

Result of php bin/console router

$php bin/console router
app_ahi_sp_admin_hq_default_index    GET      https    ANY    /admin/hq/

Upvotes: 1

Views: 2233

Answers (1)

scottie320
scottie320

Reputation: 173

I solved it by setting Symfony\Bundle\FrameworkBundle\Controller\Controller, which is the extends destination of BaseController, to Symfony\Bundle\FrameworkBundle\Controller\AbstractController.

Upvotes: 1

Related Questions