Domenico
Domenico

Reputation: 82

Symfony2 "Fatal error: Call to a member function get() on a non-object in.." when call service in controller

I have 2 controllers that use a same service:

CONTROLLER 1

    namespace MO\FrontendBundle\Controller;
    use MO\FrontendBundle\Controller\SuperClass\MOSearchController;
    use Symfony\Component\HttpFoundation\JsonResponse;

    class ResultsController extends MOSearchController {

         public function detailAction() {

              $vm = $this->get("vehicles_manager"); // SERVICE IN QUESTION

              $result = $vm->getVehicleDetail($idVehicle);

         }

    }

CONTROLLER 2

namespace MO\FrontendBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MetatagsController extends Controller {

     public function metatagsController() {

          $vm = $this->get("vehicles_manager"); // SERVICE IN QUESTION

          $result = $vm->getVehicleDetail($idVehicle);

     }

}

The service is regularly declared in services.yml file:

services:
    vehicles_manager:
        class: MO\FrontendBundle\Service\VehiclesManager
        arguments: [@logger, %vehicles_manager.endpoint%, %vehicles_manager.scope%, %vehicles_manager.consumer%, %form_values.manual_gear_code%]
        tags:
             - { name: monolog.logger, channel: solrws }

    vehicles_memory:
        class: MO\FrontendBundle\Service\VehiclesMemory
        arguments: [@request_stack]

The problem is that while in the first controller there are no errors, in the second i get the error:

Fatal error: Call to a member function get() on a non-object in C:\Users\d.test\workspace\test\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\Controller.php on line 274

Any ideas?

Upvotes: 1

Views: 2425

Answers (1)

shacharsol
shacharsol

Reputation: 2342

In your second controller you didn't suffix the method name with Action then, it is not really an end point. your code for the second controller should be:

namespace MO\FrontendBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

 class MetatagsController extends Controller {

  public function metatagsAction() {

      $vm = $this->get("vehicles_manager"); // SERVICE IN QUESTION

      $result = $vm->getVehicleDetail($idVehicle);

  }

}

Upvotes: 2

Related Questions