I try to use transliterator to transliterate a photos name and I dont know why all the time I see error: Attempted to call function "transliterator_transliterate" from namespace "App\Controller".
here is a code :
<?php
namespace App\Controller;
use App\Entity\Photo;
use App\Form\UploadPhotoType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class IndexController extends AbstractController
{
/**
* @Route("/", name="index")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function index(Request $request)
{
$form = $this->createForm(UploadPhotoType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
if ($this->getUser()) {
/** @var UploadedFile $pictureFileName */
$pictureFileName = $form->get('filename')->getData();
if ($pictureFileName) {
try {
$originalFileName = pathinfo($pictureFileName->getClientOriginalName(), PATHINFO_FILENAME);
$safeFileName = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFileName);
$newFileName = $safeFileName.'-'.uniqid().'.'.$pictureFileName->guessExtension();
$pictureFileName->move('images/hosting', $newFileName);
$entityPhotos = new Photo();
$entityPhotos->setFilename($newFileName);
$entityPhotos->setIsPublic($form->get('is_public')->getData());
$entityPhotos->setUploadedAt(new \DateTime());
$entityPhotos->setUser($this->getUser());
$em->persist($entityPhotos);
$em->flush();
$this->addFlash('success','Dodano zdjęcie!');
} catch (\Exception $e) {
$this->addFlash('error', 'Wystąpił nieoczekiwany błąd!');
}
}
}
}
return $this->render('index/index.html.twig', [
'form' => $form->createView(),
]);
}
}
I dont know how fix it. I am folllowing some course now and I copied the script to look what is wrong. After copy it still doesnt work
This is how I fix this issue on my side.
apt-get install php7.X-intl
vim/etc/php/7.X/cli/php.ini
service apache2 restart
It done the job for me.