Asset manifest file "manifest.json" does not exist - symfony

On a symfony5 + webpack encore application I can't access the url of an image in a controller.
Webpack is working and i access the img in twig. (with versionning)
the manifest.json file exist, and I have build the assets
$package = new Package(new JsonManifestVersionStrategy('C:\wamp64\www\svn\athena\public\build\manifest.json'));
echo $package->getUrl('build/images/tree/families.png');
=> This is working with the direct file address of the manifest.
but with :
$package = new Package(new JsonManifestVersionStrategy('manifest.json'));
or
$package = new Package(new JsonManifestVersionStrategy( '/build/manifest.json'));
=> I have the symfony exception : Asset manifest file "manifest.json" does not exist.
How can i make the manifest.json file address being relative ?
Thanks
assets.yaml
framework:
assets:
json_manifest_path: '%kernel.project_dir%/public/build/manifest.json'

Well, the path is supposed to be absolute...
You can inject the Packages service, that will be automatically configured:
use Symfony\Component\Asset\Packages;
public function controllerAction(Packages $packages)
{
$url = $packages->getUrl('build/images/tree/families.png');
}

Related

How to get Symfony to read env variables from another file other than .env?

Symfony isn't reading the environmental variables from the config / parameters even though they exist in the running docker container environment.
Symfony keeps complaining if specifically the .env file isn't used.
How do I get Symfony to read the environment itself, or a file named other than .env?
Some more information:
The issue arises in ./symfony/dependency-injection/EnvVarProcessor.php:96 when it's trying to load files from the config/packages/doctrine.yaml for use in the Doctrine ORM.
1. Reading env variable from docker env variable
As you know, symfony can read env variable only those are prefixed by SYMFONY__
In the DockerFile :
ENV SYMFONY__MY_ENV_VAR myvalue // here it's not forced to prefix it by SYMFONY__
In the apache file conf where you configure your virtual host for example :
SetEnv SYMFONY__MY_ENV_VAR ${SYMFONY__MY_ENV_VAR} // ${SYMFONY__MY_ENV_VAR} refer to the previous variable declared in docker file
in you parameters.yml
parameters:
my_env_value: '%my.env_var%'
Symfony will replace dot with the two underscore to find the env var without the keyword symfony__.
Example:
SYMFONY__MY_ENV_VAR ==> %my_env_var%
So in your application you need just to inject the container to get the parameter like this way :
$this->container->getParameter('my_env_value');
2. Override .env file with another filename
The point here is to modify your bootstrap.php file that looks like :
<?php
use Symfony\Component\Dotenv\Dotenv;
require dirname(__DIR__).'/vendor/autoload.php';
// Load cached env vars if the .env.local.php file exists
// Run "composer dump-env prod" to create it (requires symfony/flex >=1.2)
if (is_array($env = #include dirname(__DIR__).'/.env.local.php')) {
$_SERVER += $env;
$_ENV += $env;
} elseif (!class_exists(Dotenv::class)) {
throw new RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
} else {
// load all the .env files
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env'); // just make the file name that you want instead of .env
}
$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = ($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null) ?: 'dev';
$_SERVER['APP_DEBUG'] = $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? 'prod' !== $_SERVER['APP_ENV'];
$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = (int) $_SERVER['APP_DEBUG'] || filter_var($_SERVER['APP_DEBUG'], FILTER_VALIDATE_BOOLEAN) ? '1' : '0';

Need help understanding install of DOMPDF with Composer and Symfony

I'm having significant amount of troubles trying figure out how to install and use DOMPDF with Composer and Symfony. It is a project written a few years ago and I am completely new to Composer, Symfony and DOMPDF...somebody else told me we were using Composer and Symfony.
I've installed DOMPDF using the puTTy command line interface by:
1. going to the folder where my composer.json is (vendor)
2. running the command "Install dompdf/dompdf" It completed successfully with no errors.
3. Then running the command composer "require dompdf/dompdf" which also completed successfully with no errors.
Then I get lost/confused...
I see instructions that say to edit composer.json with :
{
"require": {
"squizlabs/php_codesniffer": "2.0.*",
}
So here is the new contents of composer.json
{
"require": {
"spipu/html2pdf": "^5.0",
"dompdf/dompdf": "^0.8.1"
}
I've tried using DOMPDF by putting the following line in my PHP code and all I get is a blank page
use Dompdf\Dompdf;
There is also an autoload.php which looks like this:
//autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit********************************::getLoader();
Following the rabbit hole, autoload_real.php looks like this:
// autoload_real.php #generated by Composer
class ComposerAutoloaderInit987ec9019a1b2f978bf00ce76684ede0
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit987ec9019a1b2f978bf00ce76684ede0', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInit987ec9019a1b2f978bf00ce76684ede0', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require_once __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit987ec9019a1b2f978bf00ce76684ede0::getInitializer($loader));
} else {
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = require __DIR__ . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
}
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit987ec9019a1b2f978bf00ce76684ede0::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire987ec9019a1b2f978bf00ce76684ede0($fileIdentifier, $file);
}
return $loader;
}}function composerRequire987ec9019a1b2f978bf00ce76684ede0($fileIdentifier, $file){if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;}}
Additionally, I have 14 folders in the vendor file, which I assume are all packages used with Composer, but I don't see them being required in the composer.json file, and, after installing DOMPDF, I don't see a vendor folder for it. I'd like to remove them, but I don't know what is and is not being used.
I've read the "getting started guides" and I still don't know what I've done wrong.
I'm sure I will have a thousand more questions...thank you for your patience.
a) your composer.json file should be in the root of your project directory and not in the vendor directory.
b) just typing the command
composer require dompdf/dompdf
in your project directory should be enough to install the libraries in the vendor directory AND to add the line
"dompdf/dompdf": "^0.8.1"
in your composer.json.
c) Follow instructions on https://github.com/dompdf/dompdf how to use dompdf.
d) In general when working with composer you have to include the vendor/autoload.php file but in the Symfony framework that will already happen in the app/autoload.php.
require 'vendor/autoload.php';
e) dompdf does use namespaces. Little example:
require "../vendor/autoload.php"; // change path if you need to
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
f) Lot of packages that can be installed with composer use other packages too. If you take a look at the composer.json from dompdf you will see that it requires some other packages which composer will automaticly install for you.
[edit]
Your composer.json is in the root directory of your project. Same for your vendor directory. In your vendor directory there will be a composer directory, that is normal. But execute any commands from your root directory. structure:
- [project root] (execute commands here)
|
|- composer.json
|- [vendor]
|
|- [composer]
If you got stuck just delete the whole vendor directory and composer.json file and again use the command
composer require dompdf/dompdf

symfony translation resource path

I can't make my translator to load a resource yams file.
I have a file admin.ru.yml inside AppBundle/Resources/config/translations/
I have in my other bundle the following lines
$translator = new Translator('ru_RU', new MessageSelector());
$translator->addLoader('yaml', new FileLoader());
$translator->addResource('yaml', 'admin.ru.yml', 'ru_RU', 'admin');
$tt = $translator->trans('Category', array(), 'admin');
It does not load the yml file.
I even have specified in my app/config.yml file of the whole application
translator:
paths:
- '%kernel.root_dir%/../src/AppBundle/Resources/config/translations'
but with no result. I tried many paths but can't find the right way. Any suggestions of what am I doing wrong?
you have to rename your file #AppBundle.ru.yml, the file path is not necessary

Symfony SAAS multitenant (DB by tenant). Sharing my implementation

I people! First: sorry about my english (I'm improving it...). Thanks for edit de post to corret it :)
I'm implementing a SaaS app multitenant on symfony. I wish share with the community what is the way that I've used (mae by not the best, but it works fine...).
THE CONCEPT:
One code (one app).
A DB for each tenant
A CSS for each tenant
Each client have a subdomain like (www.tenant1.myapp.com, www.tenant2.myapp.com)
FIRST: ARRANGING THE CONFIGURATION
I've arranged the configuration files in folders like this:
-app
---config
-----tenantA
--------config.yml
--------config_dev.yml
--------config_prod.yml
--------parameters.yml
-----tenantB
--------config.yml
--------config_dev.yml
--------config_prod.yml
--------parameters.yml
-----services.yml
-----security.yml
DISPATCHER
On AppKernel I've modiffied the registerContainerConfiguration action. It loads de general configuration (DB parameters, twig globals, etc....).
Original:
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
}
New (I used the subdomain to set what is the environment that symfony have to load:
public function registerContainerConfiguration(LoaderInterface $loader)
{
$url= $_SERVER['HTTP_HOST'];
$partes = explode('.',$url);
$tenant= $partes[0]; // this get de subdomain. If the access is www.tenant1.myapp.com this returns tenant1.
$loader->load($this->getRootDir().'/config/'.$tenant.'/config_'.$this->getEnvironment().'.yml');
}
This function goes to the tenant folder and load the config file with his parameters.yml
ARRANGING CACHE AND LOG FOLDERS
I've modified the AppKernel including the functions getCacheDir and getLogDir in order to set the correct folder to save the files:
public function getCacheDir()
{
return $this->rootDir . '/cache/' . $tenant. '/' . $this->environment;
}
public function getLogDir()
{
return $this->rootDir . '/logs/' . $tenant. '/' . $this->environment;
}
This save the files like:
-cache
---tenant1
-----dev
-----prod
---tenant2
-----dev
-----prod
-logs
---tenant1
-----dev.log
-----prod.log
----tenant2
------dev.log
------prod.log
CSS BY TENANT
I've arranged my css files by tenant like this:
--web
---css
----tenant1.css
----tenant2.css
----tenantN.css
Then, in the config.yml of each tenant (remind the arranged folders on app->config) I used a global tiwg with the name of the css theme of the tenant:
twig:
globals:
app_css: tenant1.css
Then, in the tiwg where i've declared the css files and include in the base twig I call the css by:
{% set cssload = "css/" ~ app_css ~ ".css" %}
<link rel="stylesheet" href="/{{cssload}}">
As the cache is saved in diferents folders, the CSS of tenant1 don't affects the cache and the css of tenant2.
Finish: I don't know if this is a good practice, but it works fine, and it's another solution.
Thanks everybody for the help of this days!!!!!

PHP Fatal error: Class 'Application\Sonata\MediaBundle\ApplicationSonataMediaBundle' not found in /var/www/znata.com/app/AppKernel.php on line 47

i followed this doc to install SonataMediaBundle but i got this error:
PHP Fatal error: Class 'Application\Sonata\MediaBundle\ApplicationSonataMediaBundle' not found in /var/www/znata.com/app/AppKernel.php on line 47
After using the sonata command t generate the app:
php app/console sonata:easy-extends:generate SonataMediaBundle
new directory was generated under:
apps/Application/Sonata/MediaBundle
everything was done but when i registred the generated application in my AppKernel.php i got that error.
public function registerbundles()
{
return array(
...
new Application\Sonata\MediaBundle\ApplicationSonataMediaBundle(),
...
);
}
Have you any idea how to fix this issue ?
By default project root directory is not in the autoload path, only "src" dir.
You can use
php app/console sonata:easy-extends:generate --dest=src SonataMediaBundle
to generate bundle in the src or simple copy it to the src.
After debug this problem, i found that the namspace Application is not registred.
In SF2.0, the documentation said that we should register this namespace like:
<?php
$loader->registerNamespaces(array(
...
'Application' => __DIR__,
'Imagine' => __DIR__.'/../vendor/imagine/lib',
'Gaufrette' => __DIR__.'/../vendor/gaufrette/src',
'Buzz' => __DIR__.'/../vendor/buzz/lib',
...
));
but in SF2.1 they did talked about this.
So i registred the namespace Application in autoload.php and it works fine.
so, the autoload.php look like this:
<?php
// file: app/autoload.php
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__.'/../vendor/autoload.php';
//custom for Application
$loader->add("Application", __DIR__);
// intl
if (!function_exists('intl_get_error_code')) {
require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
$loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
}
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
return $loader;
With this new config everything is fine.But in SF2.0, they talked also about "Imagine", "Guffrette" and "Buzz" namespaces. So perhapes, when using them, we should register them also like Application namespace.
I hope that this helps you.
Using composer I did this in composer.json:
"autoload": {
"psr-0": {
"": "src/",
"Application": "app/"
}
},
I added the mapping "Application": "app/".
And then run
composer update
This generated extra autoloading needed.
inside your composer.json file, have:
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"Application\\": "src/Application"
},
}
do a simple:
composer dump-autoload
to re-generate the autoload files.
new Application\Sonata\MediaBundle\MediaBundle(),
or
new Application\Sonata\MediaBundle\SonataMediaBundle(),
As skonsoft mentioned, you need to load the namespaces in autoload.php. I had the same issue with FOQ.Elastica and resolved it by adding the following.
$loader->add('FOQ', __DIR__.'/../vendor/bundles');
You can also use your app namespace prefix so the package falls under your namespace
bin/console sonata:easy-extends:generate --dest=src SonataMediaBundle --namespace_prefix=App

Resources