sonata-project / SonataMediaBundle

Symfony SonataMediaBundle
https://docs.sonata-project.org/projects/SonataMediaBundle
MIT License
451 stars 495 forks source link

Paths & Links relative to host / document root don't work when the App is running on a sub-directory #1674

Closed smilearric closed 4 years ago

smilearric commented 4 years ago

Environment

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.57.0 3.58.0 The missing Symfony Admin Generator
sonata-project/block-bundle              3.18.3 3.18.3 Symfony SonataBlockBundle
sonata-project/cache                     2.0.1  2.0.1  Cache library
sonata-project/core-bundle               3.17.2 3.17.2 Symfony SonataCoreBundle (abandoned)
sonata-project/datagrid-bundle           2.5.0  3.1.1  Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.5.1  1.5.1  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.13.0 3.13.0 Symfony Sonata / Integrate Doctrine ORM into the SonataAdmin...
sonata-project/easy-extends-bundle       2.5.0  2.5.0  Symfony SonataEasyExtendsBundle
sonata-project/exporter                  2.0.1  2.0.1  Lightweight Exporter library
sonata-project/formatter-bundle          3.5.0  4.1.3  Symfony SonataFormatterBundle
sonata-project/intl-bundle               2.5.0  2.7.0  Symfony SonataIntlBundle
sonata-project/media-bundle              3.15.0 3.23.0 Symfony SonataMediaBundle
sonata-project/notification-bundle       3.5.1  3.6.2  Symfony SonataNotificationBundle
sonata-project/user-bundle               4.5.1  4.5.1  Symfony SonataUserBundle

EDIT: Still happening with most recent version of SonataMediaBundle (other versions untouched, fixated in composer.json).

Updating sonata-project/media-bundle (3.15.0 => 3.23.0)

Symfony packages

$ composer show --latest 'symfony/*'
symfony/acl-bundle         v1.0.0  v1.0.1  Symfony AclBundle
symfony/monolog-bundle     v3.3.1  v3.5.0  Symfony MonologBundle
symfony/phpunit-bridge     v3.4.36 v5.0.3  Symfony PHPUnit Bridge
symfony/polyfill-apcu      v1.11.0 v1.13.1 Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-ctype     v1.13.1 v1.13.1 Symfony polyfill for ctype functions
symfony/polyfill-intl-icu  v1.13.1 v1.13.1 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring  v1.13.1 v1.13.1 Symfony polyfill for the Mbstring extension
symfony/polyfill-php56     v1.13.1 v1.13.1 Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70     v1.13.1 v1.13.1 Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-util      v1.13.1 v1.13.1 Symfony utilities for portability of PHP codes
symfony/security-acl       v3.0.4  v3.0.4  Symfony Security Component - ACL (Access Control List)
symfony/swiftmailer-bundle v2.6.7  v3.4.0  Symfony SwiftmailerBundle
symfony/symfony            v3.4.36 v5.0.3  The Symfony PHP framework

PHP version

$ php -v
PHP 7.2.25 (cli) (built: Nov 20 2019 18:42:58) ( ZTS MSVC15 (Visual C++ 2017) x64 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.25, Copyright (c) 1999-2018, by Zend Technologies

Subject

Edit View Screenshot

SonataMediaBundleNonDocumentRootPathIssueEdit

List View Screenshot

SonataMediaBundleNonDocumentRootPathIssueList

Problem

Exept for the Protected URL, other links (such as reference or default_SIZE) links & path to the media are relative to the Document Root and therefore broken when the site does not answer at the Document Root.

Steps to reproduce

A Sonata (Media Bundle) project into a sub-directory of your Webserver DocumentRoot. For some development environment reasons, you don't have a Virtual Host or a directive which would allow your site to answer at your DocRoot.

The site is running on a slug/slug subdirectory (cf. screenshots).

Ex.: http://localhost/ site_slug/another_slug /web/app_dev.php/

Expected results

As for any path in the page and for the Media reference Link, I'd expect the Media paths/links (edit view) or the preview image (list view) to be relative to the main controller's path, not to the Document Root.

Actual results

List view, Network file Response, Chrome developer tools

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /uploads/media/sonatamedia/files/admin/file.png was not found on this server.</p>
</body></html>

Edit view, after a click on the _defaultxs link, Chrome main rendering:

Not Found
The requested URL /uploads/media/sonatamedia/files/default_xs/file.png was not found on this server.
core23 commented 4 years ago

This looks like a config issue, please show your configuration for sonata_media

smilearric commented 4 years ago

Thanks for narrowing down.

doctrine:
    dbal:
        types:
            json: Sonata\Doctrine\Types\JsonType

    orm:
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            SonataMediaBundle: ~
            ApplicationSonataMediaBundle: ~
            FOSUserBundle: ~
            SonataUserBundle: ~
            ApplicationSonataUserBundle: ~
            AppBundle: ~
# https://sonata-project.org/bundles/media/3-x/doc/reference/installation.html#configuration
sonata_media:
    # if you don't use default namespace configuration
    #class:
    #    media:        MyVendor\MediaBundle\Entity\Media
    #    gallery:      MyVendor\MediaBundle\Entity\Gallery
    #    gallery_item: MyVendor\MediaBundle\Entity\GalleryItem
    db_driver: doctrine_orm
    default_context: default
    contexts:
        default:
            download:
                strategy: sonata.media.security.connected_strategy
                mode: http
            providers:
                - sonata.media.provider.file
                - sonata.media.provider.image

            formats:
                xs: { width: 100 , quality: 71}
                sm: { width: 300 , quality: 61}
                md: { width: 600 , quality: 71}
                xl: { width: 1200 , quality: 61}
    providers:
        image:
            resizer: sonata.media.resizer.square
    cdn:
        server:
            path: /uploads/media

    filesystem:
        local:
            directory:  "%kernel.root_dir%/../web/uploads/media"
            create:     false

I've used EasyExtends and the FileSystem looks like this, if that could be a hint:

ApplicationSonataMediaBundle-generated-FS

Did I miss a step such as:

At this point, the bundle is not yet ready. You need to generate the correct entities for the media:
bin/console sonata:easy-extends:generate --dest=src SonataMediaBundle --namespace_prefix=App

In this case, since this is not a Flex install, should I use the --namespace_prefix=App option?

Regards

github-actions[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.