![]() |
![]() |
![]()
Post
#1
|
|
Grupa: Zarejestrowani Postów: 577 Pomógł: 6 Dołączył: 21.07.2008 Ostrzeżenie: (0%) ![]() ![]() |
Symfony. Jak załadować własne funkcje do kontrolera i/lub widoku?
Mam tak: 1. Dodałem plik w katalogi src/Bundle/Twig/AppFunctions.php (funkcja dla testu) Kod <?php namespace Bundle\Twig; class AppFunctions { public function getTName() { return 'Test Name'; } } W tym pliku chciałbym umieścić wszystkie dodatkowe funkcje jakie będą potrzebne na stronach projektu 2. W services.yaml dodałem kod Kod # My Functions app.twig_functions: class: Bundle\Twig\AppFunctions public: false tags: { name: twig.functions } 3. W pliku IndexController.php Kod <?php namespace App\Controller; use App\Entity\Users; use App\Entity\Articles; use App\Bundle\Twig\AppFunctions; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; class IndexController extends AbstractController { /** * @Route({"pl": "/", "en": "/"}, name="index") */ public function index() { $em = $this->getDoctrine()->getManager(); $indexArticles = $em->getRepository(Articles::class)->findBy(['index' => true], ['uploaded_at' => "DESC"], $max = 4, $first = null); return $this->render('index/index.html.twig', [ 'indexArticles' => $indexArticles ]); } } Pytanie jak dodać własne funkcje, które mają formatować dane widoku z tablicy $indexArticles a ) formatowanie opisu b ) funkcja generująca SEO link i inne funkcje używane na podstronach projektu. |
|
|
![]() |
![]()
Post
#2
|
|
Grupa: Moderatorzy Postów: 36 559 Pomógł: 6315 Dołączył: 27.12.2004 ![]() |
Dawno nie pisalem w symfony tego typu rzeczy, ale czy to nie jest to czego szukasz?
https://symfony.com/doc/current/templating/..._extension.html |
|
|
![]() ![]() |
![]() |
Aktualny czas: 5.10.2025 - 16:57 |