Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Symfony własne funkcje, kontroler, widok
Malinaa
post
Post #1





Grupa: Zarejestrowani
Postów: 573
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.


--------------------
I welcome you on the Internet >>> Design by Malina
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 20:00