我正在一个symfony项目中构建一个rest API,我有4个彼此相关的实体,例如:
例如,当我想获取资源时,我已经安装了FOSRestBundle来构建GET Web服务:
我收到此错误:
消息:“已检测到循环参考(配置的极限:1)。”,
这是我的控制器:
<?php
namespace API\APIBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\Annotations as Rest;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Serializer;
class CartographyController extends Controller
{
/**
* @Rest\View()
* @Rest\Get("/posts")
* @param Request $request
* @return Response
*/
public function getPostsAction(Request $request)
{
$encoder = new JsonEncoder();
$normalizer = new GetSetMethodNormalizer();
$serializer = new Serializer(array($normalizer), array($encoder));
$posts = $this->get('doctrine.orm.entity_manager')
->getRepository('EvalBundle:Post')
->findAll();
return new Response($serializer->serialize($posts, 'json'));
}
/**
* @Rest\View()
* @Rest\Get("/employments") …
Run Code Online (Sandbox Code Playgroud) 我在angular 2项目中遇到了这个错误:
找不到名称'Response'.
问题来自这项服务:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
@Injectable()
export class CartographyService {
public data;
constructor(private http: Http) {
}
getData() {
return this.http.get('http://localhost:8000/cartography')
.map((res: Response) => res.json());
}
}
Run Code Online (Sandbox Code Playgroud) 我是 Angular2 的新手。我想要一个单一的类或一个配置文件,其中包含我可以在所有服务中注入的所有 API 端点(允许参数等在不同的路由中)。在 Angular2 中这样做的最佳方法是什么?我的意思是,我应该像定义服务时那样定义一个 @Injectable 类(然后将它添加到我的服务的提供者中)。
我发现的问题是,当我将我的 api 部署到客户端部分的服务器上时,我必须更改所有以字符串格式调用的端点,因此如果我有许多端点可以使用,这将是浪费时间。
在此示例中,我使用字符串格式的端点调用服务:
getData() {
return this._http.get('http://localhost:8000/cartography')
.map(function(res) {
const jsonArray = res.json();
const newJsonArr = [];
for ( let i = 0; i < jsonArray.length; i++) {
const object = {
post_name : jsonArray[i].name,
employment_name : jsonArray[i].employment.name,
profession_name : jsonArray[i].employment.profession.name,
family_name : jsonArray[i].employment.profession.family.name
};
newJsonArr.push(object);
}
return newJsonArr;
});
}
Run Code Online (Sandbox Code Playgroud)
所以我正在寻找一种在类或配置文件中将其定义为全局变量的方法。任何帮助请!谢谢 。
我正在开发一个 symfony 5 项目和一个带有安全组件的身份验证系统,我在我的用户实体中添加了一个名为 is_banned 的字段,默认情况下它采用假值,我想放置一个函数来检查用户是否是在登录之前禁止与否,如果是,应用程序将其重定向到主页并显示简单的错误消息!
<?php
namespace App\Entity;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity(fields={"email"}, message="There is already an account with this email")
*/
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\Column(type="boolean")
*/
private $isVerified = false;
/**
* @ORM\Column(type="boolean")
*/
private $is_banned …
Run Code Online (Sandbox Code Playgroud)