8000 Merge branch '5.0' · symfony/symfony-docs@6d0230b · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d0230b

Browse files
committed
Merge branch '5.0'
* 5.0: using slugger for file uploading, instead of long transliterator code
2 parents 8899d34 + f31423d commit 6d0230b

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

controller/upload_file.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,14 @@ Finally, you need to update the code of the controller that handles the form::
129129
use Symfony\Component\HttpFoundation\File\UploadedFile;
130130
use Symfony\Component\HttpFoundation\Request;
131131
use Symfony\Component\Routing\Annotation\Route;
132+
use Symfony\Component\String\Slugger\SluggerInterface;
132133

133134
class ProductController extends AbstractController
134135
{
135136
/**
136137
* @Route("/product/new", name="app_product_new")
137138
*/
138-
public function new(Request $request)
139+
public function new(Request $request, SluggerInterface $slugger)
139140
{
140141
$product = new Product();
141142
$form = $this->createForm(ProductType::class, $product);
@@ -150,7 +151,7 @@ Finally, you need to update the code of the controller that handles the form::
150151
if ($brochureFile) {
151152
$originalFilename = pathinfo($brochureFile->getClientOriginalName(), PATHINFO_FILENAME);
152153
// 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);
154155
$newFilename = $safeFilename.'-'.uniqid().'.'.$brochureFile->guessExtension();
155156

156157
// Move the file to the directory where brochures are stored
@@ -238,20 +239,23 @@ logic to a separate service::
238239

239240
use Symfony\Component\HttpFoundation\File\Exception\FileException;
240241
use Symfony\Component\HttpFoundation\File\UploadedFile;
242+
use Symfony\Component\String\Slugger\SluggerInterface;
241243

242244
class FileUploader
243245
{
244246
private $targetDirectory;
247+
private $slugger;
245248

246-
public function __construct($targetDirectory)
249+
public function __construct($targetDirectory, SluggerInterface $slugger)
247250
{
248251
$this->targetDirectory = $targetDirectory;
252+
$this->slugger = $slugger;
249253
}
250254

251255
public function upload(UploadedFile $file)
252256
{
253257
$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);
255259
$fileName = $safeFilename.'-'.uniqid().'.'.$file->guessExtension();
256260

257261
try {

0 commit comments

Comments
 (0)
0