错误:在非对象上调用成员函数has()

时间:2015-07-27 10:38:12

标签: php symfony

我已经阅读了很多这方面的主题,我似乎无法找到解决问题的方法。 错误是FatalErrorException:错误:在第291行的vendor / symfony / symfony / src / Symfony / Bundle / FrameworkBundle / Controller / Controller.php中的非对象上调用成员函数has()。

public function getDoctrine()
{
    if (!$this->container->has('doctrine')) {
        throw new \LogicException('The DoctrineBundle is not   registered in your application.');
    }

}

这是主控制器

namespace Acme\IndexBundle\Controller;

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

use Acme\IndexBundle\Entity\SlotmachineSpin;
use Acme\IndexBundle\Entity\SlotmachineReels;
use Acme\IndexBundle\Entity\SlotmachinePrizes;
use Acme\IndexBundle\Slots\UsersSlots;
use Acme\IndexBundle\Slots\SlotsMachineSlots;


class SpinController extends Controller  {


    public function indexAction(Request $request)
    {
        $session = $request->getSession();
        $slotsmachine = new SlotsMachineSlots();
        //$request = Request::createFromGlobals();
        $_machineName = $request->request->get('machine_name');
        $machineName = $slotsmachine->GetMachineName((isset($_machineName)?$_machineName : "default" ));
        $_bet = $request->request->get('bet');
        $bet = (isset($_bet) ? $_bet : $slotsmachine->MinBet($machineName)); // Should always be set, but just in case.
        $bet = min(max($slotsmachine->MinBet($machineName), $bet), $slotsmachine->MaxBet($machineName));
        $_windowID = $request->request->get('windowID');
        $windowID = (isset($_windowID) ? $_windowID : "");

        // Validate
        $error = "";
        $userID = UsersSlots::LoggedUserID();

        try { //DB::BeginTransaction();
            $em = $this->getDoctrine()->getManager();
            $em->getConnection()->beginTransaction();

            if (!$userID) {
                $error = 'loggedOut';
            } else if(!UsersSlots::HasEnoughCredits($userID, $bet)) {
                $error = "You don't have enough credits for this bet";
            }

            if ($error != "") {
                echo json_encode(array('success'=>false, 'error'=>$error));
                return;
            }

            // Do the charging, spinning and crediting
            UsersSlots::DeductCredits($userID, $bet);

            UsersController::IncrementSlotMachineSpins($userID);
            $data = SlotsMachineSlots::Spin($userID, $machineName, $bet, $windowID);

            if ($data['prize'] != null) {
                UsersSlots::IncreaseCredits($userID, $data['prize']['payoutCredits']);
                UsersSlots::IncreaseWinnings($userID, $data['prize']['payoutWinnings']);
                $data['lastWin'] = $data['prize']['payoutWinnings'];
            }

            $data['success'] = true;

            $userData = UsersSlots::GetUserData($userID);
            $data['credits'] = (float) $userData['credits'];
            $data['dayWinnings'] = (float) $userData['day_winnings'];
            $data['lifetimeWinnings'] = (float) $userData['lifetime_winnings'];

            echo json_encode($data);

            $em->getConnection()->commit();
        } catch (Exception $e) {
                $em->getConnection()->rollback(); 
                throw $e; 
            }

        // Sample responses that allow you to test your CSS and JS
        // Comment the entire try/catch block above, and uncomment one of these at a time.

        // Regular spin, no prize
        //echo json_encode(array('success' => true, 'reels' => array(1, 2.5, 3), 'prize' => null, 'credits' => 99, 'dayWinnings' => 10, 'lifetimeWinnings' => 500));

        // Prize, pays credits only
        //echo json_encode(array('success' => true, 'reels' => array(1, 2.5, 3), 'prize' => array('id' => 1, 'payoutCredits' => 10, 'payoutWinnings' => 0), 'credits' => 19, 'dayWinnings' => 00, 'lifetimeWinnings' => 500));

        // Prize, pays winnings only
        //echo json_encode(array('success' => true, 'reels' => array(1, 2.5, 3), 'prize' => array('id' => 2, 'payoutCredits' => 0, 'payoutWinnings' => 100), 'credits' => 9, 'dayWinnings' => 100, 'lifetimeWinnings' => 600));

        // Error (logged out)
        //echo json_encode(array('success' => false, 'error' => 'loggedOut'));

        // Error (other)
        //echo json_encode(array('success' => false, 'error' => 'You do not have enough credits for this spin'));
        //return new Response(json_encode(array('spinData' => $spinData)));
    }


}

这是正在使用的服务。

acme.controller.spin:
    class: Imaginer\IndexBundle\Controller\SpinController
    calls:
        - [setContainer, ["@service_container"]]

我确信问题是学说容器不存在,这就是我遇到问题的原因。

感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:-1)

Sundar的回应在我的案例中起作用。我遇到了同样的问题,现在可以了。

无论如何都无法获得控制器方法,您可以在__construct中为您提供所需的服务,并在服务参数中提供它们,例如:

in yml:

funny.controller.service:
        class: AppBundle\Controller\FunnyController
        arguments: ["@doctrine.orm.entity_manager"]
控制器类中的

use Doctrine\ORM\EntityManager;

/**
* @Route("/funny", service="funny.controller.service")
*/
class FunnyController extends Controller
{
 private $em;

 public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

以后使用$this->em代替$this->getDoctrine()->getManager();

答案 1 :(得分:-3)

问题是容器没有注入控制器。

通常symfony会自动执行此操作,如果您正在扩展Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller,它本身会扩展Symfony \ Component \ DependencyInjection \ ContainerAware。

 use Symfony\Bundle\FrameworkBundle\Controller\Controller;

 class YourController extends Controller

使用setter注入将容器注入控制器(如果没有明确定义为服务),使用容器作为参数调用方法setContainer()。

现在,当您将控制器配置为服务时,需要将setContainer调用添加到服务配置中。

services:
  database_controller:
    class:  Fuel\FormBundle\Controller\DatabaseController
    calls:
        - [setContainer, ["@service_container"]]

之后清除缓存。

CREDITS:nifr:)