我尝试将Sonata媒体与Sonata Formatter集成在一起,并且效果很好,但是我有两个问题。
我跟踪了奏鸣曲格式程序代码,并在CkeditorAdminController :: uploadAction(https://github.com/sonata-project/SonataFormatterBundle/blob/4.x/src/Controller/CkeditorAdminController.php#L93)中看到控制器未检查表单是否有效,也未检查媒体是否有效。
另一方面,如果在上传时发生错误,则ckeditor仅显示服务器返回的错误页面,但不会再次显示输入文件小部件。
答案 0 :(得分:0)
我终于设法使它成为扩展默认和自定义CkeditorAdminExtension的控制器
namespace MediaBundle\Admin;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\FormatterBundle\Admin\CkeditorAdminExtension;
/**
* Adds browser and upload routes to the Admin.
*
* @author Kévin Dunglas <kevin@les-tilleuls.coop>
*/
// we override routing definitions
class CustomCkeditorAdminExtension extends CkeditorAdminExtension
{
public function configureRoutes(AdminInterface $admin, RouteCollection $collection)
{
$collection->add('ckeditor_browser', 'ckeditor_browser', [
'_controller' => 'MediaBundle:CustomCkeditorAdmin:browser',
]);
$collection->add('ckeditor_upload', 'ckeditor_upload', [
'_controller' => 'MediaBundle:CustomCkeditorAdmin:upload',
]);
}
}
控制器:
namespace MediaBundle\Controller;
use Sonata\FormatterBundle\Controller\CkeditorAdminController;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;
class CustomCkeditorAdminController extends CkeditorAdminController
{
CONST MAX_IMAGE_FILE_SIZE = 200 * 1024; // 200 K
CONST VALID_IMAGE_MIME_TYPES = ['image/pjpeg', 'image/jpeg', 'image/png', 'image/x-png'];
public function uploadAction()
{
$request = $this->get('request_stack')->getCurrentRequest();
if($request->isMethod('POST')){
$file = $file = $request->files->get('upload');
if($file instanceof UploadedFile){
if($file->getSize() > self::MAX_IMAGE_FILE_SIZE){
return new Response('Too big file');
}
if(!in_array($file->getMimeType(), self::VALID_IMAGE_MIME_TYPES)){
return new Response('File is not a valid image');
}
}
}
return parent::uploadAction();
}
}
最后在参数.yml中覆盖扩展参数
parameters:
sonata.formatter.ckeditor.extension.class: "MediaBundle\\Admin\\CustomCkeditorAdminExtension"