注意:正在尝试获取非对象的属性-Symfony 3



我用symfony继续学习。但有一些错误,在这一刻。我下不了决心。我正在使用Symfony3和Postman,我正在创建UserController,并且editAction有一个错误。当我通过POST方法发送令牌(授权(和用户数据(json(时。这是错误消息注意:正在尝试获取非对象的属性

500内部服务器错误-ContextErrorException

http://localhost:8080/curso-fullstack/symfony/web/app_dev.php/user/edit

这是我的UserController代码

    <?php
namespace AppBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyComponentHttpFoundationJsonResponse;
use BackendBundleEntityUser;
class UserController extends Controller {
    public function newAction(Request $request) {
        $helpers = $this->get("app.helpers");
        $json = $request->get("json", null);
        $params = json_decode($json);
        $data = array(
            "status" => "error",
            "code" => 400,
            "msg" => "User not created"
        );
        if ($json != null) {
            $createdAt = new Datetime("now");
            $image = null;
            $role = "user";
            $email = (isset($params->email)) ? $params->email : null;
            $name = (isset($params->name) && ctype_alpha($params->name)) ? $params->name : null;
            $surname = (isset($params->surname) && ctype_alpha($params->surname)) ? $params->surname : null;
            $password = (isset($params->password)) ? $params->password : null;
            $emailConstraint = new AssertEmail();
            $emailConstraint->message = "This email is not valid";
            $validate_email = $this->get("validator")->validate($email, $emailConstraint);
            if ($email != null && count($validate_email) == 0 &&
                    $password != null && $name != null && $surname != null
            ) {
                $user = new User();
                $user->setCreatedAt($createdAt);
                $user->setImage($image);
                $user->setRole($role);
                $user->setEmail($email);
                $user->setName($name);
                $user->setSurname($surname);
                //Cifrar la Password
                $pwd = hash('sha256', $password);
                $user->setPassword($pwd);
                $em = $this->getDoctrine()->getManager();
                $isset_user = $em->getRepository("BackendBundle:User")->findBy(
                        array(
                            "email" => $email
                ));
                if (count($isset_user) == 0) {
                    $em->persist($user);
                    $em->flush();
                    $data["status"] = 'success';
                    $data["code"] = 200;
                    $data["msg"] = 'New user created !!';
                } else {
                    $data = array(
                        "status" => "error",
                        "code" => 400,
                        "msg" => "User not created, duplicated"
                    );
                }
            }
        }
        return $helpers->json($data);
    }
    public function editAction(Request $request) {
        $helpers = $this->get("app.helpers");
        $hash = $request->get("authorization", null);
        $authCheck = $helpers->authCheck($hash);
        if ($authCheck == true) {
            $identity = $helpers->authCheck($hash, true);
            $em = $this->getDoctrine()->getManager();
            $user = $em->getRepository("BackendBundle:User")->findOneBy(array(
                    "id" => $identity->sub
            ));
            $json = $request->get("json", null);
            $params = json_decode($json);
            $data = array(
                "status" => "error",
                "code" => 400,
                "msg" => "User not updated"
            );
            if ($json != null) {
                $createdAt = new Datetime("now");
                $image = null;
                $role = "user";
                $email = (isset($params->email)) ? $params->email : null;
                $name = (isset($params->name) && ctype_alpha($params->name)) ? $params->name : null;
                $surname = (isset($params->surname) && ctype_alpha($params->surname)) ? $params->surname : null;
                $password = (isset($params->password)) ? $params->password : null;
                $emailContraint = new AssertEmail();
                $emailContraint->message = "This email is not valid !!";
                $validate_email = $this->get("validator")->validate($email, $emailContraint);
                if ($email != null && count($validate_email) == 0 &&
                     $name != null && $surname != null
                ) {
                    $user->setCreatedAt($createdAt);
                    //$user->setImage($image);
                    $user->setRole($role);
                    $user->setEmail($email);
                    $user->setName($name);
                    $user->setSurname($surname);
                    if($password != null && !empty($password)){
                        //Cifrar la password
                        $pwd = hash('sha256', $password);
                        $user->setPassword($pwd);
                    }
                    $em = $this->getDoctrine()->getManager();
                    $isset_user = $em->getRepository("BackendBundle:User")->findBy(
                            array(
                                "email" => $email
                    ));
                    if (count($isset_user) == 0 || $identity->email == $email) {
                        $em->persist($user);
                        $em->flush();
                        $data["status"] = 'success';
                        $data["code"] = 200;
                        $data["msg"] = 'User updated !!';
                    } else {
                        $data = array(
                            "status" => "error",
                            "code" => 400,
                            "msg" => "User not updated, duplicated!!"
                        );
                    }
                }
            } else {
                $data = array(
                    "status" => "error",
                    "code" => 400,
                    "msg" => "Authorization not valid"
                );
            }
        }
        return $helpers->json($data);
    }
//    public function uploadImageAction(Request $request) {
//        $helpers = $this->get("app.helpers");
//
//        $hash = $request->get("authorization", null);
//        $authCheck = $helpers->authCheck($hash);
//
//        if ($authCheck) {
//            $identity = $helpers->authCheck($hash, true);
//
//            $em = $this->getDoctrine()->getManager();
//            $user = $em->getRepository("BackendBundle:User")->findOneBy(array(
//                "id" => $identity->sub
//            ));
//
//            //Upload File
//            $file = $request->files->get("image");
//            if (!empty($file) && $file != null) {
//                $ext = $file->guessExtension();
//                $file_name = time() . "." . $ext;
//                $file->move("uploads/users", $file_name);
//
//                $user->setImage($file_name);
//                $em->persist($user);
//                $em->flush();
//
//                $data = array(
//                    "status" => "success",
//                    "code" => 200,
//                    "msg" => "Image for user uploaded success !!"
//                );
//            } else {
//                $data = array(
//                    "status" => "error",
//                    "code" => 400,
//                    "msg" => "Image not uploaded"
//                );
//            }
//        } else {
//            $data = array(
//                "status" => "error",
//                "code" => 400,
//                "msg" => "Authorization not valid"
//            );
//        }
//        return $helpers->json($data);
//    }
}

我希望你能帮我一把

这里是堆栈跟踪

Stack Trace
in srcAppBundleControllerUserController.php at line 89   -
            $em = $this->getDoctrine()->getManager();
            $user = $em->getRepository("BackendBundle:User")->findOneBy(array(
                    "id" => $identity->sub
            ));
            $json = $request->get("json", null);
at ErrorHandler ->handleError ('8', 'Trying to get property of non-object', 'C:xampphtdocscurso-fullstacksymfonysrcAppBundleControllerUserController.php', '89', array('request' => object(Request), 'helpers' => object(Helpers), 'hash' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjcsImVtYWlsIjoiYXlhbmV6QG1haWwuY2wiLCJuYW1lIjoiQWxlamFuZHJvIiwic3VybmFtZSI6InlhbmV6IiwicGFzc3dvcmQiOiI3MThlMzk3ODUxNmQzODc5MjRkOTE5ODBhN2UyMWFmMmY0MzRkZTQ0NTczMTk1MWE2NTg1YmRhMmVhY2VmMDQ2IiwiaW1hZ2UiOm51bGwsImlhdCI6MTQ3NDg4NTczMywiZXhwIjoxNDc1NDkwNTMzfQ.1Lvbm4op7Xvz9oJ3kerPwnapqj-AbPuHqkEjGlFNgc4', 'authCheck' => true, 'identity' => true, 'em' => object(EntityManager))) 
in srcAppBundleControllerUserController.php at line 89   + 
at UserController ->editAction (object(Request)) 
at call_user_func_array (array(object(UserController), 'editAction'), array(object(Request))) 
in vendorsymfonysymfonysrcSymfonyComponentHttpKernelHttpKernel.php at line 144   + 
at HttpKernel ->handleRaw (object(Request), '1') 
in vendorsymfonysymfonysrcSymfonyComponentHttpKernelHttpKernel.php at line 64   + 
at HttpKernel ->handle (object(Request), '1', true) 
in vendorsymfonysymfonysrcSymfonyComponentHttpKernelKernel.php at line 169   + 
at Kernel ->handle (object(Request)) 
in webapp_dev.php at line 30   + 
Logs   - 1 error
INFO - Matched route "user_edit". 
INFO - Populated the TokenStorage with an anonymous Token. 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerDebugHandlersListener::configure". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerDumpListener::configure". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerValidateRequestListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyBundleFrameworkBundleEventListenerSessionListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerFragmentListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerRouterListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerLocaleListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerTranslatorListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentSecurityHttpFirewall::onKernelRequest". 
DEBUG - Notified event "kernel.controller" to listener "SymfonyBundleFrameworkBundleDataCollectorRouterDataCollector::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SymfonyComponentHttpKernelDataCollectorRequestDataCollector::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerControllerListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerParamConverterListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerHttpCacheListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerSecurityListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerTemplateListener::onKernelController". 
CRITICAL - Uncaught PHP Exception SymfonyComponentDebugExceptionContextErrorException: "Notice: Trying to get property of non-object" at C:xampphtdocscurso-fullstacksymfonysrcAppBundleControllerUserController.php line 89 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerDebugHandlersListener::configure". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerDumpListener::configure". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerValidateRequestListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyBundleFrameworkBundleEventListenerSessionListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerFragmentListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerRouterListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerLocaleListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentHttpKernelEventListenerTranslatorListener::onKernelRequest". 
DEBUG - Notified event "kernel.request" to listener "SymfonyComponentSecurityHttpFirewall::onKernelRequest". 
DEBUG - Notified event "kernel.controller" to listener "SymfonyBundleFrameworkBundleDataCollectorRouterDataCollector::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SymfonyComponentHttpKernelDataCollectorRequestDataCollector::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerControllerListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerParamConverterListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerHttpCacheListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerSecurityListener::onKernelController". 
DEBUG - Notified event "kernel.controller" to listener "SensioBundleFrameworkExtraBundleEventListenerTemplateListener::onKernelController". 
Stack Trace (Plain Text)   +

感谢

因此$identity显示为null。检查您的服务app.helpers

相关内容

  • 没有找到相关文章

最新更新