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';
}
}
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 }
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
]);
}
}
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.