Search code examples
phpsymfony5

Attempted to call function from namespace "App\Controller"


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


Solution

  • This is how I fix this issue on my side.

    1. First verify what version of PHP you use using phpinfo();
    2. Install the corresponding intl extension: apt-get install php7.X-intl
    3. Edit you php.ini: vim/etc/php/7.X/cli/php.ini
    4. Search for the line ;extension=intl
    5. Uncomment it by removing the ";"
    6. Restart Apache 2: service apache2 restart

    It done the job for me.