Symfony 4 Как взять параметр со страницы
У меня есть страница
<style>
.IMG img { max-width: 500px; max-height: 500px; width: 100% }
</style>
<h2>Article {{ article.title }}</h2>
<div class="card bg-success text-white">
<div class="card-body">
<div>Title: {{ article.title }}</div>
<div>Author: {{ article.author }}</div>
<br>
<div>Time: {{ article.created|date("F jS \\a\\t g:ia") }}</div>
<br>
<div>Content: <br> {{ article.content }}</div>
{% if(article.image) %}
<div class="IMG">File:<br> <img src="{{ asset('/uploads/' ~ article.image)}}" alt="кукапук"> </div>
{% endif %}
<button><a class="nav-link" href="{{ path('subscribers', {'article': article.id }) }}">Подписаться</a></button>
<div class="text-white bg-white"><a href="{{ path('update_article', {'id': article.id }) }}">Edit</a></div>
<div class="text-white bg-white"><a href="{{ path('article_delete', {'article': article.id }) }}">Delete</a></div>
</div>
</div>
На странице есть кнопка, <button><a class="nav-link" href="{{ path('subscribers', {'article': article.id }) }}">Подписаться</a></button>, которая относиться к другому контроллеру, как взять параметр id с этой страницы и перенести его в контроллер. То есть мне нужно взять параметр "article.id" и впихнуть его в контроллер
Контроллер в который мне нужно записать id
<?php
namespace App\Controller;
use App\Entity\Article;
use App\Entity\Subscribers;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class SubscribersController extends AbstractController
{
/**
* @Route("/subscribers/", name="subscribers")
*/
public function sub()
{
$article= new Article();
$em =$this->getDoctrine()->getManager();
$post= $this->getDoctrine()->getRepository(Article::class)->find($article->getId());
$sub=new Subscribers();
$Us= $this->getUser();
$em->getRepository(User::class)->find($Us->getId());
$sub->setUsers($Us);
$sub->setAuthor($article->getUser());
$em->persist($sub);
$em->flush();
return $this->render('subscribers/index.html.twig', [
'controller_name' => 'SubscribersController'
]);
}
}
Мне нужно, чтобы я мог записать в setAuthor article.id ,который был взят со страницы выше
Контроллер странице на которой у меня кнопка
<?php
namespace App\Controller;
use App\Entity\Article;
use App\Entity\Subscribers;
use App\Entity\User;
use App\Form\ArticleType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\File\Exception\FileException;;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Filesystem\Filesystem;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
class ArticleController extends AbstractController
{
/**
* @Route("/articles", name="articles")
*/
public function index()
{
$em = $this->getDoctrine()->getManager();
$articles = $em->getRepository(Article::class)->findBy([], ['id' => 'DESC']);
return $this->render('articles/index.html.twig', [
'articles' => $articles,
'controller_name' => 'ArticlesController',
]);
}
/**
* @Route("/article/single/{article}", name="single_article")
*/
public function single(Article $article)
{
return $this->render('articles/single.html.twig', [
'article' => $article,
]);
}
/**
* @Route("/article/create", name="create_article")
*/
public function create(Request $request)
{
$user= new User();
$article = new Article();
$form = $this->createForm(ArticleType::class, $article);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$article = $form->getData();
/**
* @var UploadedFile $file
*/
$file=$form->get('Img')->getData();
if($file) {
if ($file) {
$filename = md5(uniqid()) . '.' . $file->guessExtension();
try {
$file->move(
$this->getParameter('uploads_directory'), $filename
);
} catch (FileException $e) {
echo $e;
}
$article->setImage($filename);
}
}
$article->setCreated(new \DateTime('now'));
$em = $this->getDoctrine()->getManager();
$Us= $this->getUser();
$em->getRepository(User::class)->find($Us->getId());
$article->setUser($Us);
$article->setAuthor($Us->getUsername());
$em->persist($article);
$em->flush();
return $this->redirectToRoute('articles');
}
return $this->render('articles/form.html.twig', [
'form' => $form->createView()
]);
}
/**
* @Route("/article/update/{id}", name="update_article")
*/
public function update(Request $request, Article $article)
{
$form = $this->createForm(ArticleType::class, $article, [
'action' => $this->generateUrl('update_article', [
'article' => $article->getId()
]),
'method' => 'POST',
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/**
* @var UploadedFile $file
*/
$file=$form->get('Img')->getData();
if($file) {
if ($file) {
$filename = md5(uniqid()) . '.' . $file->guessExtension();
try {
$file->move(
$this->getParameter('uploads_directory'), $filename
);
} catch (FileException $e) {
echo $e;
}
$article->setImage($filename);
}
}
$article = $form->getData();
$article->setUpdatedAt(new \DateTime('now'));
$em = $this->getDoctrine()->getManager();
$em->flush();
return $this->redirectToRoute('articles');
}
return $this->render('articles/form.html.twig', [
'form' => $form->createView()
]);
}
/**
* @Route("/article/delete/{article}", name="article_delete")
*/
public function delete(Article $article)
{
$em = $this->getDoctrine()->getManager();
$em->remove($article);
$em->flush();
return $this->redirectToRoute('articles');
}
}