Php 如何在Symfony 4中通过GET请求访问属性?
我尝试打开一个页面,例如:Php 如何在Symfony 4中通过GET请求访问属性?,php,symfony,oop,attributes,doctrine,Php,Symfony,Oop,Attributes,Doctrine,我尝试打开一个页面,例如: http://project:8888/products 在那一页上,我喜欢用slug来显示标题: <h1>{{page.slug}}</h1> {{page.slug} 一个问题是,打开页面需要很长时间。另一个问题是,存在错误消息,无法访问属性slug MyController.php: <?php namespace App\Controller; use App\Entity\User; use App\Entity\Pa
http://project:8888/products
在那一页上,我喜欢用slug来显示标题:
<h1>{{page.slug}}</h1>
{{page.slug}
一个问题是,打开页面需要很长时间。另一个问题是,存在错误消息,无法访问属性slug
MyController.php:
<?php
namespace App\Controller;
use App\Entity\User;
use App\Entity\Pages;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Omines\DataTablesBundle\Controller\DataTablesTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
class DataTableController extends Controller
{
/**
* @Route("/{slug}", name="page")
*/
use DataTablesTrait;
public function showPage($slug)
{
$pages = $this->getDoctrine()->getRepository(Pages::class)->findAll();
$page = $this->getDoctrine()->getRepository(Pages::class)->find($slug);
$table = $this->getDoctrine()->getRepository(User::class)->findAll();
$serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
$json_string = $serializer->serialize($table, 'json');
$file = 'data/data.json';
file_put_contents($file, $json_string);
return $this->render('list.html.twig', ['pages' => $pages, 'page' => $page]);
}
}
调用find($slug)将无法工作。“查找”始终查找id,因此它将找不到该页。您需要findOneBy(['slug'=>$slug])页面
的内容是什么?$page
为空。在您的数据库中似乎找不到包含slug产品的页面。要长时间打开该页面,您可以查看symfony profiler中的性能
,试图找出原因。find($slug)不起作用。“查找”始终查找id。因此它将找不到该页。你需要findOneBy(['slug'=>$slug])@FuceBads请将你的评论作为答案发布,因为这是这里的实际解决方案。
{% extends 'base.html.twig' %}
{% block title %}Symfony{% endblock %}
{% block body %}
<div class="wrapper">
{{ include('inc/navbar.html.twig') }}
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper" style="min-height: 866px;">
<section class="content-header">
<h1>{{page.slug}}</h1>
</section>
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box">
<!-- /.box-header -->
<div class="box-body">
<table id="example" class="display" width="100%" cellspacing="0">
<thead>
<tr>
<th>ID</th>
<th>E-Mail</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>
</section>
</div>
<!-- /.content-wrapper -->
</div>
<!-- ./wrapper -->
{% endblock %}
{% block javascripts %}
{% endblock %}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\PagesRepository")
*/
class Pages
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $template;
/**
* @ORM\Column(type="string", length=255)
*/
private $icon;
/**
* @ORM\Column(type="string", length=255)
*/
private $slug;
public function getId()
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getTemplate(): ?string
{
return $this->template;
}
public function setTemplate(string $template): self
{
$this->template = $template;
return $this;
}
public function getIcon(): ?string
{
return $this->icon;
}
public function setIcon(string $icon): self
{
$this->icon = $icon;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
}