froala / KMSFroalaEditorBundle

Symfony bundle for Froala WYSIWYG HTML Rich Text Editor.
https://froala.com/wysiwyg-editor
105 stars 33 forks source link

Upload - Symfony 4 - The "kms_froala_editor.media_manager" service or alias has been removed or inlined when the container was compiled. #54

Closed adrienlamotte closed 6 years ago

adrienlamotte commented 6 years ago

Hello !

When trying to upload a file, the routes for file management seems to be broken. Here is the error message :

The "kms_froala_editor.media_manager" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.


The "kms_froala_editor.media_manager" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

  at vendor\symfony\dependency-injection\Container.php:259
  at Symfony\Component\DependencyInjection\Container->get('kms_froala_editor.media_manager')
     (vendor\symfony\framework-bundle\Controller\ControllerTrait.php:61)
  at Symfony\Bundle\FrameworkBundle\Controller\Controller->get('kms_froala_editor.media_manager')
     (vendor\kms\froala-editor-bundle\Controller\MediaController.php:28)
  at KMS\FroalaEditorBundle\Controller\MediaController->uploadImageAction(object(Request))
     (vendor\symfony\http-kernel\HttpKernel.php:149)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor\symfony\http-kernel\HttpKernel.php:66)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor\symfony\http-kernel\Kernel.php:190)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public\index.php:37)```

I'm using Symfony 4.0

Thank you for you help !
adrienlamotte commented 6 years ago

Fixed it for now using :

# kms/froala-editor-bundle/Resources/config/services.yml
KMS\FroalaEditorBundle\Controller\MediaController:
        public: true
        arguments:
            $mediaManager: '@kms_froala_editor.media_manager'
// kms/froala-editor-bundle/Controller/MediaController.php
private $mediaManager;

public function __construct(MediaManager $mediaManager)
{
    $this->mediaManager = $mediaManager;
}

And removing the lines : $mediaManager = $this->get( "kms_froala_editor.media_manager" );

sguionni commented 6 years ago

Thanks for your contribution, i will push this with the last Froala release.

mjelcic commented 6 years ago

Hi, I've got the same problem, but it doesn't get fixed with this solution that adrienlamotte suggested. My error is:

 The "kms_froala_editor.media_manager" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

Container->get('kms_froala_editor.media_manager')
in vendor\symfony\framework-bundle\Controller\ControllerTrait.php (line 61)
Controller->get('kms_froala_editor.media_manager')
in vendor\kms\froala-editor-bundle\Controller\MediaController.php (line 28)
MediaController->uploadImageAction(object(Request))
in vendor\symfony\http-kernel\HttpKernel.php (line 149)
HttpKernel->handleRaw(object(Request), 1)
in vendor\symfony\http-kernel\HttpKernel.php (line 66)
HttpKernel->handle(object(Request), 1, true)
in vendor\symfony\http-kernel\Kernel.php (line 190)
Kernel->handle(object(Request))
in public\index.php (line 37)

My symfony version is 4.0.8

Can anyone help? Thanks.

adrienlamotte commented 6 years ago

@mjelcic In kms/froala-editor-bundle/Controller/MediaController.php you have to remove the lines : $mediaManager = $this->get( "kms_froala_editor.media_manager" ); and replace $mediaManager by $this->mediaManager

Works fine here with Symfony 4.0.8 :smile:

mjelcic commented 6 years ago

Thank you @adrienlamotte, but It doesn't work again. I am not so experienced so probably I am doing something wrong.

This is my services.yml

services:

# Form type.
    kms_froala_editor.form.type:
        class: "KMS\\FroalaEditorBundle\\Form\\Type\\FroalaEditorType"
        arguments: [ "@kernel", "@service_container", "@kms_froala_editor.option_manager", "@kms_froala_editor.plugin_provider" ]
        tags:
            - { name: "form.type", alias: "froala" }

# Plugin provider.
    kms_froala_editor.option_manager:
        class: "KMS\\FroalaEditorBundle\\Service\\OptionManager"
        arguments: [ "@router" ]

# Plugin provider.
    kms_froala_editor.plugin_provider:
        class: "KMS\\FroalaEditorBundle\\Service\\PluginProvider"

# Media manager.
KMS\FroalaEditorBundle\Controller\MediaController:
        public: true
        arguments:
            $mediaManager: '@kms_froala_editor.media_manager'

# Twig extension.
    kms_froala_editor.froala_extension:
        class: "KMS\\FroalaEditorBundle\\Twig\\FroalaExtension"
        arguments: [ "@service_container" ]
        tags:
            - { name: twig.extension }

In this services.yml file a replaced this code:

     kms_froala_editor.media_manager:
        class: "KMS\\FroalaEditorBundle\\Service\\MediaManager"

with the code that you suggested (you didn't say to replace, but I wasn't sure what to do)

And this is my MediaController.php

<?php

    namespace KMS\FroalaEditorBundle\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;

    /**
     * Media controller.
     * Class MediaController
     * @package KMS\FroalaEditorBundle\Controller
     */
    class MediaController extends Controller
    {
        private $mediaManager;
        public function __construct(MediaManager $mediaManager)
        {
            $this->mediaManager = $mediaManager;
        }

        // -------------------------------------------------------------//
        // --------------------------- METHODS -------------------------//
        // -------------------------------------------------------------//

        /**
         * Upload an image.
         * @param \Symfony\Component\HttpFoundation\Request $p_request
         * @return \Symfony\Component\HttpFoundation\JsonResponse
         */
        public function uploadImageAction(Request $p_request)
        {
            $path         = $p_request->request->get( "path" );
            $folder       = $p_request->request->get( "folder" );
            $rootDir      = $this->get( "kernel" )->getRootDir();
            $basePath     = $p_request->getBasePath();
            // ------------------------- DECLARE ---------------------------//

            // FIXME
//          if( $request->isXmlHttpRequest() == true )
//          {
                return $this->mediaManager->uploadImage( $p_request->files, $rootDir, $basePath, $folder, $path );
//          }
        }

        /**
         * Delete an image.
         * @param \Symfony\Component\HttpFoundation\Request $p_request
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function deleteImageAction(Request $p_request)
        {
            $imageSrc     = $p_request->request->get( "src" );
            $folder       = $p_request->request->get( "folder" );
            $rootDir      = $this->get( "kernel" )->getRootDir();
            // ------------------------- DECLARE ---------------------------//

            $this->mediaManager->deleteImage( $imageSrc, $rootDir, $folder );

            return new Response ();
        }

        /**
         * Load images.
         * @param \Symfony\Component\HttpFoundation\Request $p_request
         * @return \Symfony\Component\HttpFoundation\JsonResponse
         */
        public function loadImagesAction(Request $p_request)
        {
            $path         = $p_request->query->get( "path" );
            $folder       = $p_request->query->get( "folder" );
            $rootDir      = $this->get( "kernel" )->getRootDir();
            $basePath     = $p_request->getBasePath();

            // ------------------------- DECLARE ---------------------------//

            return $this->mediaManager->loadImages( $rootDir, $basePath, $folder, $path );
        }

        /**
         * Upload a file.
         * @param \Symfony\Component\HttpFoundation\Request $p_request
         * @return \Symfony\Component\HttpFoundation\JsonResponse
         */
        public function uploadFileAction(Request $p_request)
        {
            $path         = $p_request->request->get( "path" );
            $folder       = $p_request->request->get( "folder" );
            $rootDir      = $this->get( "kernel" )->getRootDir();
            $basePath     = $p_request->getBasePath();
            // ------------------------- DECLARE ---------------------------//

            // FIXME
//          if( $request->isXmlHttpRequest() == true )
//          {
            return $this->mediaManager->uploadFile( $p_request->files, $rootDir, $basePath, $folder, $path );
//          }
        }

        /**
         * Upload a video.
         * @param \Symfony\Component\HttpFoundation\Request $p_request
         * @return \Symfony\Component\HttpFoundation\JsonResponse
         */
        public function uploadVideoAction(Request $p_request)
        {
            $path         = $p_request->request->get( "path" );
            $folder       = $p_request->request->get( "folder" );
            $rootDir      = $this->get( "kernel" )->getRootDir();
            $basePath     = $p_request->getBasePath();
            // ------------------------- DECLARE ---------------------------//

            // FIXME
//          if( $request->isXmlHttpRequest() == true )
//          {
            return $this->mediaManager->uploadVideo( $p_request->files, $rootDir, $basePath, $folder, $path );
//          }
        }

    }
adrienlamotte commented 6 years ago

In this services.yml file a replaced this code: kms_froala_editor.media_manager: class: "KMS\FroalaEditorBundle\Service\MediaManager" with the code that you suggested (you didn't say to replace, but I wasn't sure what to do)

Do not replace the kms_froala_editor.media_manager as it is still needed by the controller ;) And try to clear the cache too ?

mjelcic commented 6 years ago

@adrienlamotte I put your code to the end of services.yml and it look like this now:

services:

# Form type.
    kms_froala_editor.form.type:
        class: "KMS\\FroalaEditorBundle\\Form\\Type\\FroalaEditorType"
        arguments: [ "@kernel", "@service_container", "@kms_froala_editor.option_manager", "@kms_froala_editor.plugin_provider" ]
        tags:
            - { name: "form.type", alias: "froala" }

# Plugin provider.
    kms_froala_editor.option_manager:
        class: "KMS\\FroalaEditorBundle\\Service\\OptionManager"
        arguments: [ "@router" ]

# Plugin provider.
    kms_froala_editor.plugin_provider:
        class: "KMS\\FroalaEditorBundle\\Service\\PluginProvider"

# Media manager.
    kms_froala_editor.media_manager:
        class: "KMS\\FroalaEditorBundle\\Service\\MediaManager"

# Twig extension.
    kms_froala_editor.froala_extension:
        class: "KMS\\FroalaEditorBundle\\Twig\\FroalaExtension"
        arguments: [ "@service_container" ]
        tags:
            - { name: twig.extension }

    KMS\FroalaEditorBundle\Controller\MediaController:
        public: true
        arguments:
            $mediaManager: '@kms_froala_editor.media_manager'

I cleard the cache too, but again it doesn't work.

The error is different now:

Type error: Argument 1 passed to KMS\FroalaEditorBundle\Controller\MediaController::__construct() must be an instance of KMS\FroalaEditorBundle\Controller\MediaManager, instance of KMS\FroalaEditorBundle\Service\MediaManager given, called in C:\xampp\htdocs\symfony\myproject\var\cache\dev\ContainerCEamtDJ\getMediaControllerService.php on line 15

adrienlamotte commented 6 years ago

@mjelcic check your use statements on top of MediaController.php... You should have : use KMS\FroalaEditorBundle\Service\MediaManager; and not : use KMS\FroalaEditorBundle\Controller\MediaManager;

mjelcic commented 6 years ago

@adrienlamotte Thank you for your help. Looks better, but still it doesn't work. Now the error is: Image cannot be loaded from the passed link. and The requested resource /upload/d260f44127177c057a73787ccc4cb690ddfb6b0f.png was not found on this server. Looks like it finishes upload without error, but nothing is uploaded and then it cannot read the uploaded image. I also noticed that it doesn't get the correct name for the image.

adrienlamotte commented 6 years ago

@mjelcic in your project config/services.yaml where you have your froala config, you can set the upload path, for ex :

kms_froala_editor:
  fileUploadPath: "/public/uploads"

and be sure the folder is writable ?

mjelcic commented 6 years ago

@adrienlamotte It doesn't help.

The requested resource /uploads/img/2c74c79d22a3ef444251724ba071999eaed38eb9.png was not found on this server.

In my config/services.yaml I have this code:

kms_froala_editor:
  language: "en_gb"
  pluginsDisabled: [ "spell_checker", "fullscreen" ]
  fileUploadPath: "uploads/file"
  fileUploadFolder: "uploads/file"
  imageUploadFolder: "uploads/img"
  imageUploadPath: "uploads/img"

I tried with different folders and they were all writable, but it doesn't help. Seems to me that it isn't problem with path, but the problem is that it doesn't get the correct image name.

Is my kms/froala-editor-bundle/Resources/config/services.yml same like yours now (I posted it few posts ago)?

adrienlamotte commented 6 years ago

Hum... Check the path inside kms/froala-editor-bundle/Service/MediaManager.php around line 334 :

private function obtainFolder( $p_rootDir, $p_folder )
{
    return $p_rootDir . "/../public/" . $p_folder;
}
mjelcic commented 6 years ago

Now it works. Thank you very much. You helped me a lot.

The previous code was:

        private function obtainFolder( $p_rootDir, $p_folder )
        {
            // ------------------------- DECLARE ---------------------------//

            // TODO: use web directory specified by user if different.
            return $p_rootDir . "/../web/" . $p_folder;
        }

I just put your code:

private function obtainFolder( $p_rootDir, $p_folder )
{
    return $p_rootDir . "/../public/" . $p_folder;
}

and it works.

adrienlamotte commented 6 years ago

Glad it works ! Yes the web folder was renamed to public in SF4, I forgot I made that change too...

mjelcic commented 6 years ago

I just noticed that it created web folder, and all the time it was uploading in it, but I was only checking public folder :-)

L0kidor commented 6 years ago

Hi, As mentionned above, since SF4 renamed "web" folder to "public", froala doesn't upload images or files in the right directory. I tested it by creating a "web" directory, and the files are correctly uploaded.

I looked in "kms/froala-editor-bundle/Service/MediaManager.php" as mentionned by adrienlamotte but the "obtainfolder" function is now :

private function obtainFolder( $p_rootDir, $p_publicDir, $p_folder )
        {
            // ------------------------- DECLARE ---------------------------//

            return sprintf('%s/..%s/%s', $p_rootDir, $p_publicDir, $p_folder);
        }

Anyway changing it right there isn't a good solution as a composer update will overwrite any edit.

What is the right way to specify that the "public" folder have to be used instead of "web" ?

Thx

adrienlamotte commented 6 years ago

@L0kidor Hello, this was fixed in the latest version. Now you can configure the path in your conf file :

kms_froala_editor:
  publicDir: "/public"
L0kidor commented 6 years ago

Thx !!

Sorry for missing that in the readme !