@@ -129,13 +129,14 @@ Finally, you need to update the code of the controller that handles the form::
129
129
use Symfony\Component\HttpFoundation\File\UploadedFile;
130
130
use Symfony\Component\HttpFoundation\Request;
131
131
use Symfony\Component\Routing\Annotation\Route;
132
+ use Symfony\Component\String\Slugger\SluggerInterface;
132
133
133
134
class ProductController extends AbstractController
134
135
{
135
136
/**
136
137
* @Route("/product/new", name="app_product_new")
137
138
*/
138
- public function new(Request $request)
139
+ public function new(Request $request, SluggerInterface $slugger )
139
140
{
140
141
$product = new Product();
141
142
$form = $this->createForm(ProductType::class, $product);
@@ -150,7 +151,7 @@ Finally, you need to update the code of the controller that handles the form::
150
151
if ($brochureFile) {
151
152
$originalFilename = pathinfo($brochureFile->getClientOriginalName(), PATHINFO_FILENAME);
152
153
// this is needed to safely include the file name as part of the URL
153
- $safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
154
+ $safeFilename = $slugger->slug( $originalFilename);
154
155
$newFilename = $safeFilename.'-'.uniqid().'.'.$brochureFile->guessExtension();
155
156
156
157
// Move the file to the directory where brochures are stored
@@ -238,20 +239,23 @@ logic to a separate service::
238
239
239
240
use Symfony\Component\HttpFoundation\File\Exception\FileException;
240
241
use Symfony\Component\HttpFoundation\File\UploadedFile;
242
+ use Symfony\Component\String\Slugger\SluggerInterface;
241
243
242
244
class FileUploader
243
245
{
244
246
private $targetDirectory;
247
+ private $slugger;
245
248
246
- public function __construct($targetDirectory)
249
+ public function __construct($targetDirectory, SluggerInterface $slugger )
247
250
{
248
251
$this->targetDirectory = $targetDirectory;
252
+ $this->slugger = $slugger;
249
253
}
250
254
251
255
public function upload(UploadedFile $file)
252
256
{
253
257
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
254
- $safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
258
+ $safeFilename = $slugger->slug( $originalFilename);
255
259
$fileName = $safeFilename.'-'.uniqid().'.'.$file->guessExtension();
256
260
257
261
try {
0 commit comments