Set base_path for a package in Symfony2 - symfony

Here's my config.yml file
framework:
#code
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
packages:
blade:
base_path: '/bundle/bladecms'
#code
I'd like to set base_path for blade prefix to be able to use
<img src="{{ asset('images/image.png', 'blade') }}"/>
instead
<img src="{{ asset('bundle/bladecms/images/image.png') }}"/>
So what's a problem? When I use 'base_path' I get
Unrecognized option "base_path" under "framework.templating.packages.blade"
If I change base_path to base_url I get
"/bundle/bladecms" is not a valid URL
How to configure it properly?

You have to set a valid URL (e.g. http://yourhost/bundle/bladecms) .
Make your configuration like this :
framework:
templating:
packages:
blade:
assets_base_urls: '%router.request_context.scheme%://%router.request_context.host%/bundle/bladecms
And use it like follows :
<img src="{{ asset('images/image.png', 'blade') }}"/>
The first parameter router.request_context.scheme is the protocol used by the request (i.e. http, ssl).
The second router.request_context.host is the host of the request.
Like this, your base_url change dynamically depending on the request.
See http://symfony.com/doc/current/reference/configuration/framework.html#assets-base-urls

Related

django cms 'TemplateResponse' object has no attribute '_headers'

I am following the django-cms documentation and so far I only have one template home.html:
{% load cms_tags sekizai_tags %}
<html>
<head>
<title>{% page_attribute "page_title" %}</title>
{% render_block "css" %}
</head>
<body>
{% cms_toolbar %}
{% placeholder "content" %}
{% render_block "js" %}
</body>
</html>
And my url.py looks like this:
from django.contrib import admin
from django.urls import path, re_path, include
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^', include('cms.urls')),
]
I can login and create a page, but when I logout and navigate to `localhost:8000' I get:
AttributeError at /
'TemplateResponse' object has no attribute '_headers'
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 3.2
Exception Type: AttributeError
Exception Value:
'TemplateResponse' object has no attribute '_headers'
Exception Location: /usr/local/lib/python3.9/site-packages/cms/cache/page.py, line 84, in set_page_cache
Python Executable: /usr/local/bin/python
Python Version: 3.9.4
Python Path:
['/app/app',
'/usr/local/lib/python39.zip',
'/usr/local/lib/python3.9',
'/usr/local/lib/python3.9/lib-dynload',
'/usr/local/lib/python3.9/site-packages']
I am not sure if I did something wrong here. I would like to see how the webpage looks like for someone who does not have an account and cannot login.
Support for django 3.2 has been introduced in django-cms 3.9 so upgrade to this version to resolve this error.

Symfony asset function prepends / to http url

I've tried to add webpack to a Symfony 3.1 app. The problem is, when I add HMR, the asset function doesn't resolve correctly
// base.html.twig
<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
// app/config/config_dev.yml
framework:
assets:
base_path: "http://localhost:8080"
Result: <script src="/http://localhost:8080/bundles/fosjsrouting/js/router.js"></script>
How can I tell Symfony to not prepend the starting / if the base_path begins with http://?
You can use base_urls. Protocol/host/port should not be used in base_path for assets.
https://symfony.com/doc/current/reference/configuration/framework.html#assets

symfony2 and facebox ajax twig content not displaying

enter link description here
I am trying about this:
text
I have this in my twig
<a href='{{ path('likes_show_names') }}' rel='facebox'>
And than in controller:
$view= $this->renderView('WallBundle:Statuses:likes_names.html.twig');
return new Response($view);
No error appear the network (chrome) is displaying code get 200. Facebox open the pop up but the connntent.. is missing...
When i check response => preview its displaying: This request has no preview available
What i am doing wrong please?
I put Facebox in my web/ directory. The file structure looks like this:
web/facebox
web/js/jquery.js
Then, on the routing, I set my default and the ajax-called controller:
vendor_some_bundle_homepage:
pattern: /
defaults: { _controller: VendorSomeBundle:Default:index }
vendor_some_bundle_test:
pattern: /test
defaults: { _controller: VendorSomeBundle:Default:ajax }
Next, I created a simple controller for both routes:
<?php
namespace Vendor\SomeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
public function indexAction()
{
return $this->render('VendorSomeBundle:Default:indexTest.html.twig');
}
public function ajaxAction()
{
return $this->render('VendorSomeBundle:Default:ajaxTest.html.twig');
}
}
Then, and I think the most important file for you, the page where there is a link that open facebox :
<!-- indexTest.html.twig -->
<html>
<head>
<link href="{{ asset('facebox/src/facebox.css') }}" media="screen" rel="stylesheet" type="text/css"/>
</head>
<body>
<a href="{{ path('vendor_some_bundle_test') }}" rel='facebox'>click me</a>
<script src="{{ asset('js/jquery.js') }}" type="text/javascript"></script>
<script src="{{ asset('facebox/src/facebox.js') }}" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('a[rel*=facebox]').facebox();
});
</script>
</body>
</html>
Important: you should take care of your assets and of the routing. If there is some errors, they should be written in your app/log/dev.log file, or at last in your apache error.log.
Finally, create the view that will be included:
{# ajaxTest.html.twig #}
This is <em>some</em> remote content
This sample gave me the following result:
Note: there is still some assetic errors (see the close button at the right of the image), because I installed facebox quickly. The point of your question was the access to remote content, and here you have a sample you can follow to find your mistake.

assetic and filters

I try to use the cssembed filter loading my stylesheets (because of the images referenced in the css)
In config.yml
assetic:
debug: %kernel.debug%
use_controller: false
bundles: [ cramifviewkitBundle ]
#java: /usr/bin/java
filters:
cssrewrite: ~
cssembed:
jar: %kernel.root_dir%/Resources/java/cssembed-0.4.5.jar
yui_css:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar
yui_js:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar
I download cssembed-0.4.5.jar and located it in src/my_bundle/Resources/java
in the template I wrote :
{% stylesheets
'#myBundle/Resources/public/css/*'
'#myBundle/Resources/public/JQMenu/css/*'
filter='cssembed'
%}
Everything is fine for the first import, but for the second one, when I look with firebug, I have this :
<link type="text/css" href="/viewkit/web/app_dev.php/css/eb2d30c_part_1_jqueryMenus_1.css" rel="stylesheet">
and when I open it, it is not css but html because there is an error :
<h1>Exception thrown when handling an exception (Symfony\Component\HttpKernel\Exception\FlattenException: Warning: json_encode() [<a href='function.json-encode'>function.json-encode</a>]: Invalid UTF-8 sequence in argument in C:\wamp\www\viewkit\vendor\monolog\monolog\src\Monolog\Formatter\NormalizerFormatter.php line 99)</h1>
<div class="block_exception clear_fix">
<h2><span>1/1</span> <abbr title="ErrorException">ErrorException</abbr>: Warning: json_encode() [<a href='function.json-encode'>function.json-encode</a>]: Invalid UTF-8 sequence in argument in C:\wamp\www\viewkit\vendor\monolog\monolog\src\Monolog\Formatter\NormalizerFormatter.php line 99</h2>
</div>
<div class="block">
<ol class="traces list_exception">
<li> in C:\wamp\www\viewkit\vendor\monolog\monolog\src\Monolog\Formatter\NormalizerFormatter.php line 99</li>
any idea where it could come from ?
Put the cssembed-0.4.5.jar in app/Resources/java directory as you specified in the configuration.

How to combine these assetics in Symfony2?

I'm using ExposeTranslationBundle (expose translations to javascript) and JMSI18nRoutingBundle (expose routes to javascript). This is part of my <head> tag:
{% javascripts filter='?yui_js' output='js/app.js'
'../app/Resources/public/js/jquery-*.js'
'../app/Resources/public/js/jquery/*'
'../app/Resources/public/js/app.js'
'bundles/fosjsrouting/js/router.js'
'bundles/bazingaexposetranslation/js/translation.js' %}
<script src="{{ asset_url }}" ></script>
{% endjavascripts %}
<!-- ExposeTranslationBundle and JMSI18nRoutingBundle -->
<script src="{{ path('fos_js_routing_js',
{"callback": "fos.Router.setData"}) }}"></script>
<script src="{{ url('bazinga_exposetranslation_js') }}"></script>
Is possible to combine the last two <script> imports into first assetic and how?
I thing it is not possible because the FOSJSRouting javascript file is generated by a controller. Internaly the bundles caches the js but in app/cache, so it needs to go through the controller every request. I'm not familiar with the expose translation bundle but I guess it's the same problem here.
There has been an ongoing discussion in the issue tracke of FOSJsRouterBundle on github and there is also a sollution. See the full issue here: https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/issues/22
The workaround is to have a script or command to dump the output to files in web/js directory:
<?php
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
use Symfony\Component\HttpFoundation\Request;
$kernel = new AppKernel('stage', false);
$kernel->loadClassCache();
$response = $kernel->handle(Request::create('/js/routing?callback=fos.Router.setData'));
file_put_contents(__DIR__.'/../web/js/routes.js', $response->getContent());
This is somewhat of a workaround sollution. I've been thinking about implementing a generic bundle wich whome this task can be configured for several other bundles using controllers to output js. The controller actions would have to be configured in a yml file and then a command would have have to be executed at each deployment/modification of routes/strings. But I havn't had time for this... yet ;)
Instead of import, you could happily put it inline, ie:
<script type="text/javascript">
{# BazingaExposeTranslation #}
{% render 'bazinga.exposetranslation.controller:exposeTranslationAction'
with { domain_name: "messages", _locale:app.session.locale, _format: "js" } %}
{# JMSI18nRoutingBundle ... #}
</script>
You need to check the routing file for those bundles.

Resources